From 2dc13cc8411d2d17f4e4765932050b5388e4a0d2 Mon Sep 17 00:00:00 2001 From: Joscha Date: Thu, 3 Aug 2023 15:17:22 +0200 Subject: [PATCH] Create target/static before compiling --- build.rs | 40 ++++++++++++++++++++++++++++++++++++++++ src/main.rs | 19 +++++++++++++++++-- 2 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 build.rs diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..9da51b0 --- /dev/null +++ b/build.rs @@ -0,0 +1,40 @@ +use std::{ + fs, + path::{Path, PathBuf}, + process::Command, +}; + +const STATIC_DIR: &str = "static"; +const STATIC_OUT_DIR: &str = "target/static"; + +fn copy_recursively(path: &Path) { + let from = PathBuf::new().join(STATIC_DIR).join(path); + let to = PathBuf::new().join(STATIC_OUT_DIR).join(path); + + println!("cargo:rerun-if-changed={}", from.display()); + + if from.is_file() { + if from.extension() == Some("ts".as_ref()) { + return; + } + fs::create_dir_all(to.parent().unwrap()).unwrap(); + fs::copy(from, to).unwrap(); + } else if from.is_dir() { + for entry in from.read_dir().unwrap() { + copy_recursively(&path.join(entry.unwrap().file_name())); + } + } else { + panic!("Unexpected file type at {}", from.display()); + } +} + +fn main() { + // Since remove_dir_all fails if the directory doesn't exist, we ensure it + // exists before deleting it. This way, we can use the remove_dir_all Result + // to ensure the directory was deleted successfully. + fs::create_dir_all(STATIC_OUT_DIR).unwrap(); + fs::remove_dir_all(STATIC_OUT_DIR).unwrap(); + + Command::new("tsc").output().unwrap(); + copy_recursively("".as_ref()); +} diff --git a/src/main.rs b/src/main.rs index e7a11a9..a263a35 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,18 @@ -fn main() { - println!("Hello, world!"); +use axum::{routing::get, Router}; + +async fn run() -> anyhow::Result<()> { + let app = Router::new().route("/", get(|| async { "Hello, world!" })); + + axum::Server::bind(&"0.0.0.0:8000".parse().unwrap()) + .serve(app.into_make_service()) + .await?; + + Ok(()) +} + +#[tokio::main] +async fn main() -> anyhow::Result<()> { + // Rust-analyzer struggles analyzing code in this function, so the actual + // code lives in a different function. + run().await }