Move server stuff into API folder

This commit is contained in:
Michael Mikovsky
2025-11-29 14:10:05 -07:00
parent a10bdce38f
commit c83e2f1527
8 changed files with 42 additions and 41 deletions
@@ -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(
@@ -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<Response<Body>, A
let (_, token) = (header.next(), header.next());
let token_data = match decode_jwt(token.unwrap().to_string()) {
let token_data: TokenData<Cliams> = match decode_jwt(token.unwrap().to_string()) {
Ok(data) => data,
Err(_) => {
return Err(AuthError {
+19
View File
@@ -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());
+3 -20
View File
@@ -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;
+2 -12
View File
@@ -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;
}