diff options
author | fx <[email protected]> | 2023-10-08 23:28:10 +0200 |
---|---|---|
committer | fx <[email protected]> | 2023-10-08 23:28:10 +0200 |
commit | 920496c85bdf0d017eaf837cbacd136d7d828669 (patch) | |
tree | 78e0cd32933d214fd16ad18a333cf7f1ade5a754 /src | |
parent | 88bedad2d7c061b707e83d80aa6f0e51817586df (diff) | |
download | webol-920496c85bdf0d017eaf837cbacd136d7d828669.tar webol-920496c85bdf0d017eaf837cbacd136d7d828669.tar.gz webol-920496c85bdf0d017eaf837cbacd136d7d828669.zip |
base web server
Diffstat (limited to 'src')
-rw-r--r-- | src/auth.rs | 3 | ||||
-rw-r--r-- | src/config.rs | 0 | ||||
-rw-r--r-- | src/main.rs | 43 | ||||
-rw-r--r-- | src/routes/mod.rs | 1 | ||||
-rw-r--r-- | src/routes/start.rs | 29 |
5 files changed, 76 insertions, 0 deletions
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 @@ | |||
1 | pub fn auth(secret: &str) -> bool { | ||
2 | secret == "aaa" | ||
3 | } | ||
diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/config.rs | |||
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 @@ | |||
1 | use axum::{Router, routing::post}; | ||
2 | use time::util::local_offset; | ||
3 | use tracing::{info, level_filters::LevelFilter}; | ||
4 | use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*}; | ||
5 | use crate::routes::start::start; | ||
6 | |||
7 | mod auth; | ||
8 | mod routes; | ||
9 | |||
10 | #[tokio::main] | ||
11 | async fn main() { | ||
12 | |||
13 | unsafe { local_offset::set_soundness(local_offset::Soundness::Unsound); } | ||
14 | let time_format = | ||
15 | time::macros::format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"); | ||
16 | let loc = LocalTime::new(time_format); | ||
17 | |||
18 | tracing_subscriber::registry() | ||
19 | .with(fmt::layer() | ||
20 | .with_timer(loc) | ||
21 | ) | ||
22 | .with( | ||
23 | EnvFilter::builder() | ||
24 | .with_default_directive(LevelFilter::INFO.into()) | ||
25 | .from_env_lossy(), | ||
26 | ) | ||
27 | .init(); | ||
28 | |||
29 | let version = env!("CARGO_PKG_VERSION"); | ||
30 | |||
31 | info!("Starting webol v{}", version); | ||
32 | |||
33 | // build our application with a single route | ||
34 | let app = Router::new() | ||
35 | .route("/start", post(start)); | ||
36 | |||
37 | // run it with hyper on localhost:3000 | ||
38 | axum::Server::bind(&"0.0.0.0:3000".parse().unwrap()) | ||
39 | .serve(app.into_make_service()) | ||
40 | .await | ||
41 | .unwrap(); | ||
42 | } | ||
43 | |||
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 @@ | |||
1 | use axum::headers::HeaderMap; | ||
2 | use axum::Json; | ||
3 | use serde::{Deserialize, Serialize}; | ||
4 | use serde_json::{json, Value}; | ||
5 | use crate::auth::auth; | ||
6 | |||
7 | pub async fn start(headers: HeaderMap, Json(payload): Json<StartPayload>) -> Json<Value> { | ||
8 | let mut res = StartResponse { id: payload.id, boot: false }; | ||
9 | if let Some(secret) = headers.get("authorization") { | ||
10 | if !auth(secret.to_str().unwrap()) { Json(json!(res)) } else { | ||
11 | res.boot = true; | ||
12 | Json(json!(res)) | ||
13 | } | ||
14 | } else { | ||
15 | Json(json!(res)) | ||
16 | } | ||
17 | } | ||
18 | |||
19 | #[derive(Deserialize)] | ||
20 | pub struct StartPayload { | ||
21 | id: String, | ||
22 | _test: Option<bool>, | ||
23 | } | ||
24 | |||
25 | #[derive(Serialize)] | ||
26 | struct StartResponse { | ||
27 | id: String, | ||
28 | boot: bool, | ||
29 | } | ||