lockwatch/lockwatch/src/main.rs
finga f85a4d7e87 Lay out the structure of the project
Parse arguments which can be used to configure the server. Create a
WASM client stub and make the server capable of serving it. Document
the current state in the readem.
2022-09-17 18:49:41 +02:00

54 lines
1.3 KiB
Rust

use anyhow::Result;
use axum::Router;
use axum_extra::routing::SpaRouter;
use clap::{ArgAction, Parser};
use log::info;
use std::{
env,
net::{IpAddr, SocketAddr},
};
use tower::ServiceBuilder;
use tower_http::trace::TraceLayer;
#[derive(Parser)]
#[clap(about, author, version)]
struct Cli {
/// The address to listen on
#[clap(short, long, default_value = "::1")]
address: IpAddr,
/// The port to listen on
#[clap(short, long, default_value = "3000")]
port: u16,
/// Path to the directory containing the statically served files
#[clap(short, long, default_value = "dist")]
static_dir: String,
/// Set the log level. Multiple -v options increase the verbosity
#[clap(short, action = ArgAction::Count)]
verbosity: u8,
}
#[tokio::main]
async fn main() -> Result<()> {
let cli = Cli::parse();
if env::var("RUST_LOG").is_err() {
env::set_var("RUST_LOG", format!("{},hyper=info,mio=info", cli.verbosity));
}
tracing_subscriber::fmt::init();
let app = Router::new()
.merge(SpaRouter::new("/client", cli.static_dir))
.layer(ServiceBuilder::new().layer(TraceLayer::new_for_http()));
let addr = SocketAddr::from((cli.address, cli.port));
info!("listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await?;
Ok(())
}