From 13afd5be8774113e8b7eb4680faeed20515c82eb Mon Sep 17 00:00:00 2001 From: alemi Date: Sat, 23 Mar 2024 20:24:14 +0100 Subject: [PATCH] chore: split server context and router --- src/main.rs | 3 ++- src/router.rs | 35 +++++++++++++++++++++++++++++++++++ src/server.rs | 32 -------------------------------- 3 files changed, 37 insertions(+), 33 deletions(-) create mode 100644 src/router.rs diff --git a/src/main.rs b/src/main.rs index b2f6545d..b27ec408 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ pub mod migrations; pub mod activitystream; pub mod activitypub; pub mod server; +pub mod router; pub mod errors; use clap::{Parser, Subcommand}; @@ -73,7 +74,7 @@ async fn main() { .await.expect("error connecting to db"); match args.command { - CliCommand::Serve => server::serve(db, args.domain) + CliCommand::Serve => router::serve(db, args.domain) .await, CliCommand::Migrate => migrations::Migrator::up(&db, None) diff --git a/src/router.rs b/src/router.rs new file mode 100644 index 00000000..b1e041df --- /dev/null +++ b/src/router.rs @@ -0,0 +1,35 @@ +use axum::{routing::{get, post}, Router}; +use sea_orm::DatabaseConnection; +use crate::activitypub as ap; + +pub async fn serve(db: DatabaseConnection, domain: String) { + // build our application with a single route + let app = Router::new() + // core server inbox/outbox, maybe for feeds? TODO do we need these? + .route("/", get(ap::view)) + // TODO shared inboxes and instance stream will come later, just use users *boxes for now + // .route("/inbox", post(ap::inbox)) + // .route("/outbox", get(ap::outbox)) + // .well-known and discovery + .route("/.well-known/webfinger", get(ap::well_known::webfinger)) + .route("/.well-known/host-meta", get(ap::well_known::host_meta)) + .route("/.well-known/nodeinfo", get(ap::well_known::nodeinfo_discovery)) + .route("/nodeinfo/:version", get(ap::well_known::nodeinfo)) + // actor routes + .route("/users/:id", get(ap::user::view)) + .route("/users/:id/inbox", post(ap::user::inbox)) + .route("/users/:id/outbox", get(ap::user::outbox)) + .route("/users/:id/followers", get(ap::user::follow___::)) + .route("/users/:id/following", get(ap::user::follow___::)) + // specific object routes + .route("/activities/:id", get(ap::activity::view)) + .route("/objects/:id", get(ap::object::view)) + .with_state(crate::server::Context::new(db, domain)); + + // run our app with hyper, listening globally on port 3000 + let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); + + axum::serve(listener, app) + .await + .unwrap(); +} diff --git a/src/server.rs b/src/server.rs index 9647781d..7574cf08 100644 --- a/src/server.rs +++ b/src/server.rs @@ -69,35 +69,3 @@ impl Context { } } } - -pub async fn serve(db: DatabaseConnection, domain: String) { - // build our application with a single route - let app = Router::new() - // core server inbox/outbox, maybe for feeds? TODO do we need these? - .route("/", get(ap::view)) - // TODO shared inboxes and instance stream will come later, just use users *boxes for now - // .route("/inbox", post(ap::inbox)) - // .route("/outbox", get(ap::outbox)) - // .well-known and discovery - .route("/.well-known/webfinger", get(ap::well_known::webfinger)) - .route("/.well-known/host-meta", get(ap::well_known::host_meta)) - .route("/.well-known/nodeinfo", get(ap::well_known::nodeinfo_discovery)) - .route("/nodeinfo/:version", get(ap::well_known::nodeinfo)) - // actor routes - .route("/users/:id", get(ap::user::view)) - .route("/users/:id/inbox", post(ap::user::inbox)) - .route("/users/:id/outbox", get(ap::user::outbox)) - .route("/users/:id/followers", get(ap::user::follow___::)) - .route("/users/:id/following", get(ap::user::follow___::)) - // specific object routes - .route("/activities/:id", get(ap::activity::view)) - .route("/objects/:id", get(ap::object::view)) - .with_state(Context::new(db, domain)); - - // run our app with hyper, listening globally on port 3000 - let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); - - axum::serve(listener, app) - .await - .unwrap(); -}