From 920496c85bdf0d017eaf837cbacd136d7d828669 Mon Sep 17 00:00:00 2001 From: fx Date: Sun, 8 Oct 2023 23:28:10 +0200 Subject: base web server --- src/auth.rs | 3 +++ src/config.rs | 0 src/main.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ src/routes/mod.rs | 1 + src/routes/start.rs | 29 +++++++++++++++++++++++++++++ 5 files changed, 76 insertions(+) create mode 100644 src/auth.rs create mode 100644 src/config.rs create mode 100644 src/main.rs create mode 100644 src/routes/mod.rs create mode 100644 src/routes/start.rs (limited to 'src') diff --git a/src/auth.rs b/src/auth.rs new file mode 100644 index 0000000..b1ad76d --- /dev/null +++ b/src/auth.rs @@ -0,0 +1,3 @@ +pub fn auth(secret: &str) -> bool { + secret == "aaa" +} diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..60f2214 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,43 @@ +use axum::{Router, routing::post}; +use time::util::local_offset; +use tracing::{info, level_filters::LevelFilter}; +use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*}; +use crate::routes::start::start; + +mod auth; +mod routes; + +#[tokio::main] +async fn main() { + + unsafe { local_offset::set_soundness(local_offset::Soundness::Unsound); } + let time_format = + time::macros::format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"); + let loc = LocalTime::new(time_format); + + tracing_subscriber::registry() + .with(fmt::layer() + .with_timer(loc) + ) + .with( + EnvFilter::builder() + .with_default_directive(LevelFilter::INFO.into()) + .from_env_lossy(), + ) + .init(); + + let version = env!("CARGO_PKG_VERSION"); + + info!("Starting webol v{}", version); + + // build our application with a single route + let app = Router::new() + .route("/start", post(start)); + + // run it with hyper on localhost:3000 + axum::Server::bind(&"0.0.0.0:3000".parse().unwrap()) + .serve(app.into_make_service()) + .await + .unwrap(); +} + diff --git a/src/routes/mod.rs b/src/routes/mod.rs new file mode 100644 index 0000000..78d4375 --- /dev/null +++ b/src/routes/mod.rs @@ -0,0 +1 @@ +pub mod start; \ No newline at end of file diff --git a/src/routes/start.rs b/src/routes/start.rs new file mode 100644 index 0000000..cda6352 --- /dev/null +++ b/src/routes/start.rs @@ -0,0 +1,29 @@ +use axum::headers::HeaderMap; +use axum::Json; +use serde::{Deserialize, Serialize}; +use serde_json::{json, Value}; +use crate::auth::auth; + +pub async fn start(headers: HeaderMap, Json(payload): Json) -> Json { + let mut res = StartResponse { id: payload.id, boot: false }; + if let Some(secret) = headers.get("authorization") { + if !auth(secret.to_str().unwrap()) { Json(json!(res)) } else { + res.boot = true; + Json(json!(res)) + } + } else { + Json(json!(res)) + } +} + +#[derive(Deserialize)] +pub struct StartPayload { + id: String, + _test: Option, +} + +#[derive(Serialize)] +struct StartResponse { + id: String, + boot: bool, +} -- cgit v1.2.3