api routes cleanup

This commit is contained in:
Felix Ableitner 2023-11-16 10:25:54 +01:00
parent 7e75c77219
commit b72bab1c6e
4 changed files with 32 additions and 22 deletions

View File

@ -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<String>) -> MyResult<Json<DbArticle>> {
async fn api_get_article(Path(title): Path<String>) -> MyResult<Json<DbArticle>> {
let instance = DbInstance::new("localhost")?;
let article = DbArticle::new(title, "dummy".to_string(), instance.ap_id)?;
Ok(Json(article))

View File

@ -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<String>,
data: Data<DatabaseHandle>,
) -> MyResult<FederationJson<WithContext<Person>>> {

View File

@ -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

View File

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