diff --git a/src/api.rs b/src/api.rs index 07e7b66..e7e58df 100644 --- a/src/api.rs +++ b/src/api.rs @@ -2,11 +2,16 @@ use crate::error::MyResult; use crate::federation::objects::article::DbArticle; use crate::federation::objects::instance::DbInstance; use axum::extract::Path; -use axum::Json; +use axum::routing::get; +use axum::{Json, Router}; use axum_macros::debug_handler; +pub fn api_routes() -> Router { + Router::new().route("/article/:title", get(api_get_article)) +} + #[debug_handler] -pub async fn api_get_article(Path(title): Path) -> MyResult> { +async fn api_get_article(Path(title): Path) -> MyResult> { let instance = DbInstance::new("localhost")?; let article = DbArticle::new(title, "dummy".to_string(), instance.ap_id)?; Ok(Json(article)) diff --git a/src/federation/routes.rs b/src/federation/routes.rs index 0c8bcee..400fd49 100644 --- a/src/federation/routes.rs +++ b/src/federation/routes.rs @@ -8,10 +8,18 @@ use activitypub_federation::protocol::context::WithContext; use activitypub_federation::traits::Object; use axum::extract::path::Path; use axum::response::IntoResponse; +use axum::routing::{get, post}; +use axum::Router; use axum_macros::debug_handler; +pub fn federation_routes() -> Router { + Router::new() + .route("/:user/inbox", post(http_post_user_inbox)) + .route("/:user", get(http_get_user)) +} + #[debug_handler] -pub async fn http_get_user( +async fn http_get_user( Path(name): Path, data: Data, ) -> MyResult>> { diff --git a/src/lib.rs b/src/lib.rs index 43981aa..b8fae9b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,15 +2,11 @@ use crate::utils::generate_object_id; use tracing::log::LevelFilter; use activitypub_federation::config::FederationMiddleware; -use axum::{ - routing::{get, post}, - Router, Server, -}; +use axum::{Router, Server}; -use crate::api::api_get_article; +use crate::api::api_routes; use crate::error::MyResult; -use crate::federation::routes::http_get_user; -use crate::federation::routes::http_post_user_inbox; +use crate::federation::routes::federation_routes; use federation::federation_config; use std::net::ToSocketAddrs; use tracing::info; @@ -33,11 +29,8 @@ pub async fn start(hostname: &str) -> MyResult<()> { info!("Listening with axum on {hostname}"); let config = config.clone(); let app = Router::new() - // federation routes - .route("/:user/inbox", post(http_post_user_inbox)) - .route("/:user", get(http_get_user)) - // api routes - .route("/api/v1/article/:title", get(api_get_article)) + .nest("", federation_routes()) + .nest("/api/v1", api_routes()) .layer(FederationMiddleware::new(config)); let addr = hostname diff --git a/tests/test.rs b/tests/test.rs index 20e4447..6f07315 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -1,9 +1,11 @@ extern crate fediwiki; + +use fediwiki::error::MyResult; use fediwiki::federation::objects::article::DbArticle; use fediwiki::start; #[tokio::test] -async fn test_get_article() { +async fn test_get_article() -> MyResult<()> { let hostname = "localhost:8131"; let handle = tokio::task::spawn(async { start(hostname).await.unwrap(); @@ -11,18 +13,17 @@ async fn test_get_article() { let title = "Manu_Chao"; let res: DbArticle = reqwest::get(format!("http://{hostname}/api/v1/article/{title}")) - .await - .unwrap() + .await? .json() - .await - .unwrap(); + .await?; assert_eq!(title, res.title); assert!(res.local); handle.abort(); + Ok(()) } #[tokio::test] -async fn test_follow_instance() { +async fn test_follow_instance() -> MyResult<()> { let hostname_alpha = "localhost:8131"; let hostname_beta = "localhost:8132"; let handle_alpha = tokio::task::spawn(async { @@ -32,6 +33,9 @@ async fn test_follow_instance() { start(hostname_beta).await.unwrap(); }); + // TODO + handle_alpha.abort(); handle_beta.abort(); -} \ No newline at end of file + Ok(()) +}