diff --git a/unshell-gui/src/auth/mod.rs b/unshell-gui/src/auth/mod.rs index 244efa1..f87e3f7 100644 --- a/unshell-gui/src/auth/mod.rs +++ b/unshell-gui/src/auth/mod.rs @@ -103,9 +103,6 @@ impl Auth { ui.horizontal(|ui| { if ui.button("Login").clicked() { - // Try to - ui.ctx().request_repaint_after(Duration::from_millis(500)); - let state = self.auth_state.clone(); crate::httpPost( diff --git a/unshell-server/src/app.rs b/unshell-server/src/api/app.rs similarity index 55% rename from unshell-server/src/app.rs rename to unshell-server/src/api/app.rs index d623dc7..59f01b2 100644 --- a/unshell-server/src/app.rs +++ b/unshell-server/src/api/app.rs @@ -1,4 +1,3 @@ -use crate::{auth, structs::CurrentUser}; use axum::{ Extension, Router, extract::Path, @@ -6,13 +5,26 @@ use axum::{ response::IntoResponse, routing::{get, post}, }; +use tokio::net::TcpListener; use unshell_lib::info; -pub async fn app() -> Router { - Router::new().route("/auth", post(auth::sign_in)).route( +use crate::api::{auth, structs::CurrentUser}; + +pub async fn start_api(address: &str) { + let listener = TcpListener::bind(address) + .await + .expect("Unable to start listener"); + + info!("Listening on {}", listener.local_addr().unwrap()); + + let app = Router::new().route("/auth", post(auth::sign_in)).route( "/api/{*path}", get(protected).layer(middleware::from_fn(auth::authorize)), - ) + ); + + axum::serve(listener, app) + .await + .expect("Error serving application"); } pub async fn protected( diff --git a/unshell-server/src/auth.rs b/unshell-server/src/api/auth.rs similarity index 97% rename from unshell-server/src/auth.rs rename to unshell-server/src/api/auth.rs index 00f0dbf..3acbdb6 100644 --- a/unshell-server/src/auth.rs +++ b/unshell-server/src/api/auth.rs @@ -10,7 +10,7 @@ use jsonwebtoken::{Header, TokenData, Validation, decode, encode}; use serde_json::{Value, json}; use unshell_lib::info; -use crate::{ +use crate::api::{ EXPIRE_DURATION, JWT_DECODING_KEY, JWT_ENCODING_KEY, structs::{AuthError, Cliams, CurrentUser, SignInData}, }; @@ -57,7 +57,7 @@ pub async fn authorize(mut req: Request, next: Next) -> Result, A let (_, token) = (header.next(), header.next()); - let token_data = match decode_jwt(token.unwrap().to_string()) { + let token_data: TokenData = match decode_jwt(token.unwrap().to_string()) { Ok(data) => data, Err(_) => { return Err(AuthError { diff --git a/unshell-server/src/api/mod.rs b/unshell-server/src/api/mod.rs new file mode 100644 index 0000000..07b7168 --- /dev/null +++ b/unshell-server/src/api/mod.rs @@ -0,0 +1,19 @@ +use chrono::Duration; +use jsonwebtoken::{DecodingKey, EncodingKey}; +use static_init::dynamic; +extern crate unshell_lib; + +pub mod app; +mod auth; +mod structs; +mod userdata; + +static EXPIRE_DURATION: Duration = Duration::hours(12); + +#[dynamic] +static JWT_SECRET: String = std::env::var("JWT_SECRET").expect("JWT_SECRET must be set"); + +#[dynamic] +static JWT_ENCODING_KEY: EncodingKey = EncodingKey::from_secret(JWT_SECRET.as_bytes()); +#[dynamic] +static JWT_DECODING_KEY: DecodingKey = DecodingKey::from_secret(JWT_SECRET.as_bytes()); diff --git a/unshell-server/src/structs.rs b/unshell-server/src/api/structs.rs similarity index 100% rename from unshell-server/src/structs.rs rename to unshell-server/src/api/structs.rs diff --git a/unshell-server/src/userdata.rs b/unshell-server/src/api/userdata.rs similarity index 100% rename from unshell-server/src/userdata.rs rename to unshell-server/src/api/userdata.rs diff --git a/unshell-server/src/lib.rs b/unshell-server/src/lib.rs index 2c11b43..3f25ad4 100644 --- a/unshell-server/src/lib.rs +++ b/unshell-server/src/lib.rs @@ -1,21 +1,4 @@ -#![macro_use] +// #![macro_use] -use chrono::Duration; -use jsonwebtoken::{DecodingKey, EncodingKey}; -use static_init::dynamic; -extern crate unshell_lib; - -pub mod app; -mod auth; -mod structs; -mod userdata; - -static EXPIRE_DURATION: Duration = Duration::seconds(10); - -#[dynamic] -static JWT_SECRET: String = std::env::var("JWT_SECRET").expect("JWT_SECRET must be set"); - -#[dynamic] -static JWT_ENCODING_KEY: EncodingKey = EncodingKey::from_secret(JWT_SECRET.as_bytes()); -#[dynamic] -static JWT_DECODING_KEY: DecodingKey = DecodingKey::from_secret(JWT_SECRET.as_bytes()); +mod api; +pub use api::app::start_api; diff --git a/unshell-server/src/main.rs b/unshell-server/src/main.rs index dcdf226..5e20193 100644 --- a/unshell-server/src/main.rs +++ b/unshell-server/src/main.rs @@ -2,21 +2,11 @@ use axum; use tokio::net::TcpListener; use unshell_lib::info; -use unshell_server::app; +use unshell_server::start_api; #[tokio::main] async fn main() { unshell_lib::logger::PrettyLogger::init(); - let listener = TcpListener::bind("127.0.0.1:3000") - .await - .expect("Unable to start listener"); - - info!("Listening on {}", listener.local_addr().unwrap()); - - let app = app::app().await; - - axum::serve(listener, app) - .await - .expect("Error serving application"); + start_api("localhost:3000").await; }