chore: split server context and router

This commit is contained in:
əlemi 2024-03-23 20:24:14 +01:00
parent d36ac82dd9
commit 13afd5be87
Signed by: alemi
GPG key ID: A4895B84D311642C
3 changed files with 37 additions and 33 deletions

View file

@ -3,6 +3,7 @@ pub mod migrations;
pub mod activitystream; pub mod activitystream;
pub mod activitypub; pub mod activitypub;
pub mod server; pub mod server;
pub mod router;
pub mod errors; pub mod errors;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
@ -73,7 +74,7 @@ async fn main() {
.await.expect("error connecting to db"); .await.expect("error connecting to db");
match args.command { match args.command {
CliCommand::Serve => server::serve(db, args.domain) CliCommand::Serve => router::serve(db, args.domain)
.await, .await,
CliCommand::Migrate => migrations::Migrator::up(&db, None) CliCommand::Migrate => migrations::Migrator::up(&db, None)

35
src/router.rs Normal file
View file

@ -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___::<false>))
.route("/users/:id/following", get(ap::user::follow___::<true>))
// 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();
}

View file

@ -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___::<false>))
.route("/users/:id/following", get(ap::user::follow___::<true>))
// 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();
}