Create target/static before compiling

This commit is contained in:
Joscha 2023-08-03 15:17:22 +02:00
parent 658c523e9c
commit 2dc13cc841
2 changed files with 57 additions and 2 deletions

40
build.rs Normal file
View file

@ -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());
}

View file

@ -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
}