initial commit
This commit is contained in:
commit
caec0b8ac9
27 changed files with 4786 additions and 0 deletions
15
crates/canvane/Cargo.toml
Normal file
15
crates/canvane/Cargo.toml
Normal file
|
@ -0,0 +1,15 @@
|
|||
[package]
|
||||
name = "canvane"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
axum = "0.7.5"
|
||||
color-eyre = "0.6.3"
|
||||
copy_dir = "0.1.3"
|
||||
eyre = "0.6.12"
|
||||
haku.workspace = true
|
||||
tokio = { version = "1.39.2", features = ["full"] }
|
||||
tower-http = { version = "0.5.2", features = ["fs"] }
|
||||
tracing = "0.1.40"
|
||||
tracing-subscriber = { version = "0.3.18", features = ["fmt"] }
|
23
crates/canvane/src/live_reload.rs
Normal file
23
crates/canvane/src/live_reload.rs
Normal file
|
@ -0,0 +1,23 @@
|
|||
use std::time::Duration;
|
||||
|
||||
use axum::{routing::get, Router};
|
||||
use tokio::time::sleep;
|
||||
|
||||
pub fn router<S>() -> Router<S> {
|
||||
Router::new()
|
||||
.route("/stall", get(stall))
|
||||
.route("/back-up", get(back_up))
|
||||
.with_state(())
|
||||
}
|
||||
|
||||
async fn stall() -> String {
|
||||
loop {
|
||||
// Sleep for a day, I guess. Just to uphold the connection forever without really using any
|
||||
// significant resources.
|
||||
sleep(Duration::from_secs(60 * 60 * 24)).await;
|
||||
}
|
||||
}
|
||||
|
||||
async fn back_up() -> String {
|
||||
"".into()
|
||||
}
|
70
crates/canvane/src/main.rs
Normal file
70
crates/canvane/src/main.rs
Normal file
|
@ -0,0 +1,70 @@
|
|||
use std::{
|
||||
fs::{copy, create_dir_all, remove_dir_all},
|
||||
path::Path,
|
||||
};
|
||||
|
||||
use axum::Router;
|
||||
use copy_dir::copy_dir;
|
||||
use eyre::Context;
|
||||
use tokio::net::TcpListener;
|
||||
use tower_http::services::{ServeDir, ServeFile};
|
||||
use tracing::{info, info_span};
|
||||
use tracing_subscriber::fmt::format::FmtSpan;
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
mod live_reload;
|
||||
|
||||
struct Paths<'a> {
|
||||
target_dir: &'a Path,
|
||||
}
|
||||
|
||||
fn build(paths: &Paths<'_>) -> eyre::Result<()> {
|
||||
let _span = info_span!("build").entered();
|
||||
|
||||
_ = remove_dir_all(paths.target_dir);
|
||||
create_dir_all(paths.target_dir).context("cannot create target directory")?;
|
||||
copy_dir("static", paths.target_dir.join("static")).context("cannot copy static directory")?;
|
||||
|
||||
create_dir_all(paths.target_dir.join("static/wasm"))
|
||||
.context("cannot create static/wasm directory")?;
|
||||
copy(
|
||||
"target/wasm32-unknown-unknown/wasm-dev/haku_wasm.wasm",
|
||||
paths.target_dir.join("static/wasm/haku.wasm"),
|
||||
)
|
||||
.context("cannot copy haku.wasm file")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
color_eyre::install().unwrap();
|
||||
tracing_subscriber::fmt()
|
||||
.with_span_events(FmtSpan::ACTIVE)
|
||||
.init();
|
||||
|
||||
let paths = Paths {
|
||||
target_dir: Path::new("target/site"),
|
||||
};
|
||||
|
||||
match build(&paths) {
|
||||
Ok(()) => (),
|
||||
Err(error) => eprintln!("{error:?}"),
|
||||
}
|
||||
|
||||
let app = Router::new()
|
||||
.route_service(
|
||||
"/",
|
||||
ServeFile::new(paths.target_dir.join("static/index.html")),
|
||||
)
|
||||
.nest_service("/static", ServeDir::new(paths.target_dir.join("static")));
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
let app = app.nest("/dev/live-reload", live_reload::router());
|
||||
|
||||
let listener = TcpListener::bind("0.0.0.0:8080")
|
||||
.await
|
||||
.expect("cannot bind to port");
|
||||
info!("listening on port 8080");
|
||||
axum::serve(listener, app).await.expect("cannot serve app");
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue