feat: add jwt auth

This commit is contained in:
acx
2024-07-18 15:39:09 +00:00
parent 7270399f35
commit 952a37892d
10 changed files with 527 additions and 38 deletions

View File

@@ -4,25 +4,32 @@ use axum::{
// Json,
Router,
};
use axum::http::Method;
use serde::{Deserialize, Serialize};
use tower::ServiceBuilder;
use tower_http::cors::{Any, CorsLayer};
use tower_http::trace::TraceLayer;
use tracing::info;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
// Project modules
mod category;
mod middleware;
mod model;
mod util;
// Passed App State
#[derive(Clone)]
pub struct AppState{
pub struct AppState {
db: deadpool_diesel::postgres::Pool,
}
#[tokio::main]
async fn main() {
dotenvy::dotenv().unwrap();
tracing_subscriber::registry().with(tracing_subscriber::fmt::layer()).init();
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer())
.init();
// initialize db connection
let db_url = std::env::var("DATABASE_URL").unwrap();
@@ -31,14 +38,22 @@ async fn main() {
.build()
.unwrap();
let shared_state = AppState {db: pool,};
let shared_state = AppState { db: pool };
// Register routers
let cors_layer = CorsLayer::new()
.allow_methods([Method::GET, Method::POST])
.allow_origin(Any);
let global_layer = ServiceBuilder::new()
.layer(TraceLayer::new_for_http())
.layer(cors_layer);
let app = Router::new()
// V1 apis
.nest("/api/v1/category", category::handler::get_nest_handlers())
.with_state(shared_state);
.nest("/api/v1/v2", category::handler::get_nest_handlers())
.with_state(shared_state)
.layer(global_layer);
let listener = tokio::net::TcpListener::bind("0.0.0.0:8987").await.unwrap();
info!("starting server on 0.0.0.0:8987");