From 4c2eb7b99096614dec1c6fef82f4e5f1c247994e Mon Sep 17 00:00:00 2001 From: alemi Date: Tue, 9 Apr 2024 04:35:57 +0200 Subject: [PATCH] chore: better routing structure --- src/main.rs | 18 +++++++++++++--- src/routes/activitypub/mod.rs | 37 +++++++++++++++++++++++++++++++- src/routes/activitypub/router.rs | 5 ++--- 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 54688976..f66a06b6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -84,9 +84,6 @@ async fn main() { .await.expect("error connecting to db"); match args.command { - CliCommand::Serve => routes::activitypub::router::serve(db, args.domain) - .await, - #[cfg(feature = "migrations")] CliCommand::Migrate => migrations::Migrator::up(&db, None) .await.expect("error applying migrations"), @@ -97,6 +94,21 @@ async fn main() { CliCommand::Fetch { uri, save } => fetch(&db, &uri, save) .await.expect("error fetching object"), + + CliCommand::Serve => { + let ctx = server::Context::new(db, args.domain) + .await.expect("failed creating server context"); + + let router = routes::activitypub::router().with_state(ctx); + + // run our app with hyper, listening locally on port 3000 + let listener = tokio::net::TcpListener::bind("127.0.0.1:3000") + .await.expect("could not bind tcp socket"); + + axum::serve(listener, router) + .await + .expect("failed serving application") + }, } } diff --git a/src/routes/activitypub/mod.rs b/src/routes/activitypub/mod.rs index 7e726a74..e56e26aa 100644 --- a/src/routes/activitypub/mod.rs +++ b/src/routes/activitypub/mod.rs @@ -9,7 +9,7 @@ pub mod router; pub mod jsonld; pub use jsonld::JsonLD; -use axum::{extract::State, http::StatusCode, response::IntoResponse, Json}; +use axum::{extract::State, http::StatusCode, response::IntoResponse, routing::{get, post}, Json, Router}; use rand::Rng; use sea_orm::{ColumnTrait, Condition, EntityTrait, QueryFilter}; @@ -18,6 +18,41 @@ use crate::{model, server::Context, url}; use self::jsonld::LD; +pub fn router() -> Router { + use crate::routes::activitypub as ap; // TODO use self ? + + 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", get(ap::inbox::get)) + // .route("/inbox", post(ap::inbox::post)) + // .route("/outbox", get(ap::outbox::get)) + // .route("/outbox", get(ap::outbox::post)) + // AUTH routes + .route("/auth", post(ap::auth)) + // .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::post)) + .route("/users/:id/inbox", get(ap::user::inbox::get)) + .route("/users/:id/inbox/page", get(ap::user::inbox::page)) + .route("/users/:id/outbox", post(ap::user::outbox::post)) + .route("/users/:id/outbox", get(ap::user::outbox::get)) + .route("/users/:id/outbox/page", get(ap::user::outbox::page)) + .route("/users/:id/followers", get(ap::user::following::get::)) + .route("/users/:id/followers/page", get(ap::user::following::page::)) + .route("/users/:id/following", get(ap::user::following::get::)) + .route("/users/:id/following/page", get(ap::user::following::page::)) + // specific object routes + .route("/activities/:id", get(ap::activity::view)) + .route("/objects/:id", get(ap::object::view)) +} + pub trait Addressed : Object { fn addressed(&self) -> Vec; } diff --git a/src/routes/activitypub/router.rs b/src/routes/activitypub/router.rs index 45adeac3..46164c0a 100644 --- a/src/routes/activitypub/router.rs +++ b/src/routes/activitypub/router.rs @@ -2,7 +2,7 @@ use axum::{routing::{get, post}, Router}; use sea_orm::DatabaseConnection; use crate::routes::activitypub as ap; -pub async fn serve(db: DatabaseConnection, domain: String) { +pub async fn serve(db: DatabaseConnection, domain: String) -> std::io::Result<()> { // build our application with a single route let app = Router::new() // core server inbox/outbox, maybe for feeds? TODO do we need these? @@ -39,9 +39,8 @@ pub async fn serve(db: DatabaseConnection, domain: String) { ); // run our app with hyper, listening locally on port 3000 - let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await.unwrap(); + let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await?; axum::serve(listener, app) .await - .unwrap(); }