Move router creation to lib to allow easier testing.
This commit is contained in:
parent
8c666c3f78
commit
352a85eaef
2 changed files with 32 additions and 29 deletions
28
src/lib.rs
28
src/lib.rs
|
@ -1,7 +1,12 @@
|
|||
#[macro_use]
|
||||
extern crate justerror;
|
||||
|
||||
use axum::{middleware, routing::get, Router};
|
||||
use axum_login::SqliteStore;
|
||||
use generic_handlers::{handle_slash, handle_slash_redir};
|
||||
use login::{get_login, get_logout, post_login, post_logout};
|
||||
use signup::{get_create_user, handle_signup_success, post_create_user};
|
||||
use sqlx::SqlitePool;
|
||||
pub use users::User;
|
||||
use uuid::Uuid;
|
||||
|
||||
|
@ -14,3 +19,26 @@ pub mod users;
|
|||
pub(crate) mod util;
|
||||
|
||||
pub type AuthContext = axum_login::extractors::AuthContext<Uuid, User, SqliteStore<User>>;
|
||||
|
||||
pub async fn app(db_pool: SqlitePool, secret: &[u8]) -> Router {
|
||||
let session_layer = db::session_layer(db_pool.clone(), secret).await;
|
||||
let auth_layer = db::auth_layer(db_pool.clone(), secret).await;
|
||||
|
||||
Router::new()
|
||||
.route("/", get(handle_slash).post(handle_slash))
|
||||
.route("/signup", get(get_create_user).post(post_create_user))
|
||||
.route(
|
||||
"/signup_success/:id",
|
||||
get(handle_signup_success).post(handle_signup_success),
|
||||
)
|
||||
.route("/login", get(get_login).post(post_login))
|
||||
.route("/logout", get(get_logout).post(post_logout))
|
||||
.fallback(handle_slash_redir)
|
||||
.layer(middleware::from_fn_with_state(
|
||||
db_pool.clone(),
|
||||
users::handle_update_last_seen,
|
||||
))
|
||||
.layer(auth_layer)
|
||||
.layer(session_layer)
|
||||
.with_state(db_pool)
|
||||
}
|
||||
|
|
33
src/main.rs
33
src/main.rs
|
@ -1,15 +1,8 @@
|
|||
use std::net::SocketAddr;
|
||||
|
||||
use axum::{middleware, routing::get, Router};
|
||||
use rand_core::{OsRng, RngCore};
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
use witch_watch::{
|
||||
db,
|
||||
generic_handlers::{handle_slash, handle_slash_redir},
|
||||
login::{get_login, get_logout, post_login, post_logout},
|
||||
signup::{get_create_user, handle_signup_success, post_create_user},
|
||||
users,
|
||||
};
|
||||
use witch_watch::db;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
|
@ -30,29 +23,11 @@ async fn main() {
|
|||
bytes
|
||||
};
|
||||
|
||||
let session_layer = db::session_layer(pool.clone(), &secret).await;
|
||||
let auth_layer = db::auth_layer(pool.clone(), &secret).await;
|
||||
|
||||
let app = Router::new()
|
||||
.route("/", get(handle_slash).post(handle_slash))
|
||||
.route("/signup", get(get_create_user).post(post_create_user))
|
||||
.route(
|
||||
"/signup_success/:id",
|
||||
get(handle_signup_success).post(handle_signup_success),
|
||||
)
|
||||
.route("/login", get(get_login).post(post_login))
|
||||
.route("/logout", get(get_logout).post(post_logout))
|
||||
.fallback(handle_slash_redir)
|
||||
.route_layer(middleware::from_fn_with_state(
|
||||
pool.clone(),
|
||||
users::handle_update_last_seen,
|
||||
))
|
||||
.layer(auth_layer)
|
||||
.layer(session_layer)
|
||||
.with_state(pool);
|
||||
let app = witch_watch::app(pool, &secret).await;
|
||||
|
||||
let addr = ([127, 0, 0, 1], 3000);
|
||||
tracing::debug!("binding to 0.0.0.0:3000");
|
||||
tracing::debug!("binding to {addr:?}");
|
||||
|
||||
axum::Server::bind(&SocketAddr::from(addr))
|
||||
.serve(app.into_make_service())
|
||||
.await
|
||||
|
|
Loading…
Reference in a new issue