1
0
Fork 0
forked from alemi/upub

chore: better routing structure

This commit is contained in:
əlemi 2024-04-09 04:35:57 +02:00
parent bb26ff763d
commit 4c2eb7b990
Signed by: alemi
GPG key ID: A4895B84D311642C
3 changed files with 53 additions and 7 deletions

View file

@ -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")
},
}
}

View file

@ -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<crate::server::Context> {
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::<false>))
.route("/users/:id/followers/page", get(ap::user::following::page::<false>))
.route("/users/:id/following", get(ap::user::following::get::<true>))
.route("/users/:id/following/page", get(ap::user::following::page::<true>))
// 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<String>;
}

View file

@ -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();
}