mirror of
https://github.com/Nutomic/ibis.git
synced 2024-11-26 03:21:09 +00:00
api routes cleanup
This commit is contained in:
parent
7e75c77219
commit
b72bab1c6e
4 changed files with 32 additions and 22 deletions
|
@ -2,11 +2,16 @@ use crate::error::MyResult;
|
||||||
use crate::federation::objects::article::DbArticle;
|
use crate::federation::objects::article::DbArticle;
|
||||||
use crate::federation::objects::instance::DbInstance;
|
use crate::federation::objects::instance::DbInstance;
|
||||||
use axum::extract::Path;
|
use axum::extract::Path;
|
||||||
use axum::Json;
|
use axum::routing::get;
|
||||||
|
use axum::{Json, Router};
|
||||||
use axum_macros::debug_handler;
|
use axum_macros::debug_handler;
|
||||||
|
|
||||||
|
pub fn api_routes() -> Router {
|
||||||
|
Router::new().route("/article/:title", get(api_get_article))
|
||||||
|
}
|
||||||
|
|
||||||
#[debug_handler]
|
#[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 instance = DbInstance::new("localhost")?;
|
||||||
let article = DbArticle::new(title, "dummy".to_string(), instance.ap_id)?;
|
let article = DbArticle::new(title, "dummy".to_string(), instance.ap_id)?;
|
||||||
Ok(Json(article))
|
Ok(Json(article))
|
||||||
|
|
|
@ -8,10 +8,18 @@ use activitypub_federation::protocol::context::WithContext;
|
||||||
use activitypub_federation::traits::Object;
|
use activitypub_federation::traits::Object;
|
||||||
use axum::extract::path::Path;
|
use axum::extract::path::Path;
|
||||||
use axum::response::IntoResponse;
|
use axum::response::IntoResponse;
|
||||||
|
use axum::routing::{get, post};
|
||||||
|
use axum::Router;
|
||||||
use axum_macros::debug_handler;
|
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]
|
#[debug_handler]
|
||||||
pub async fn http_get_user(
|
async fn http_get_user(
|
||||||
Path(name): Path<String>,
|
Path(name): Path<String>,
|
||||||
data: Data<DatabaseHandle>,
|
data: Data<DatabaseHandle>,
|
||||||
) -> MyResult<FederationJson<WithContext<Person>>> {
|
) -> MyResult<FederationJson<WithContext<Person>>> {
|
||||||
|
|
17
src/lib.rs
17
src/lib.rs
|
@ -2,15 +2,11 @@ use crate::utils::generate_object_id;
|
||||||
use tracing::log::LevelFilter;
|
use tracing::log::LevelFilter;
|
||||||
|
|
||||||
use activitypub_federation::config::FederationMiddleware;
|
use activitypub_federation::config::FederationMiddleware;
|
||||||
use axum::{
|
use axum::{Router, Server};
|
||||||
routing::{get, post},
|
|
||||||
Router, Server,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::api::api_get_article;
|
use crate::api::api_routes;
|
||||||
use crate::error::MyResult;
|
use crate::error::MyResult;
|
||||||
use crate::federation::routes::http_get_user;
|
use crate::federation::routes::federation_routes;
|
||||||
use crate::federation::routes::http_post_user_inbox;
|
|
||||||
use federation::federation_config;
|
use federation::federation_config;
|
||||||
use std::net::ToSocketAddrs;
|
use std::net::ToSocketAddrs;
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
|
@ -33,11 +29,8 @@ pub async fn start(hostname: &str) -> MyResult<()> {
|
||||||
info!("Listening with axum on {hostname}");
|
info!("Listening with axum on {hostname}");
|
||||||
let config = config.clone();
|
let config = config.clone();
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
// federation routes
|
.nest("", federation_routes())
|
||||||
.route("/:user/inbox", post(http_post_user_inbox))
|
.nest("/api/v1", api_routes())
|
||||||
.route("/:user", get(http_get_user))
|
|
||||||
// api routes
|
|
||||||
.route("/api/v1/article/:title", get(api_get_article))
|
|
||||||
.layer(FederationMiddleware::new(config));
|
.layer(FederationMiddleware::new(config));
|
||||||
|
|
||||||
let addr = hostname
|
let addr = hostname
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
extern crate fediwiki;
|
extern crate fediwiki;
|
||||||
|
|
||||||
|
use fediwiki::error::MyResult;
|
||||||
use fediwiki::federation::objects::article::DbArticle;
|
use fediwiki::federation::objects::article::DbArticle;
|
||||||
use fediwiki::start;
|
use fediwiki::start;
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_get_article() {
|
async fn test_get_article() -> MyResult<()> {
|
||||||
let hostname = "localhost:8131";
|
let hostname = "localhost:8131";
|
||||||
let handle = tokio::task::spawn(async {
|
let handle = tokio::task::spawn(async {
|
||||||
start(hostname).await.unwrap();
|
start(hostname).await.unwrap();
|
||||||
|
@ -11,18 +13,17 @@ async fn test_get_article() {
|
||||||
|
|
||||||
let title = "Manu_Chao";
|
let title = "Manu_Chao";
|
||||||
let res: DbArticle = reqwest::get(format!("http://{hostname}/api/v1/article/{title}"))
|
let res: DbArticle = reqwest::get(format!("http://{hostname}/api/v1/article/{title}"))
|
||||||
.await
|
.await?
|
||||||
.unwrap()
|
|
||||||
.json()
|
.json()
|
||||||
.await
|
.await?;
|
||||||
.unwrap();
|
|
||||||
assert_eq!(title, res.title);
|
assert_eq!(title, res.title);
|
||||||
assert!(res.local);
|
assert!(res.local);
|
||||||
handle.abort();
|
handle.abort();
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_follow_instance() {
|
async fn test_follow_instance() -> MyResult<()> {
|
||||||
let hostname_alpha = "localhost:8131";
|
let hostname_alpha = "localhost:8131";
|
||||||
let hostname_beta = "localhost:8132";
|
let hostname_beta = "localhost:8132";
|
||||||
let handle_alpha = tokio::task::spawn(async {
|
let handle_alpha = tokio::task::spawn(async {
|
||||||
|
@ -32,6 +33,9 @@ async fn test_follow_instance() {
|
||||||
start(hostname_beta).await.unwrap();
|
start(hostname_beta).await.unwrap();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
|
||||||
handle_alpha.abort();
|
handle_alpha.abort();
|
||||||
handle_beta.abort();
|
handle_beta.abort();
|
||||||
|
Ok(())
|
||||||
}
|
}
|
Loading…
Reference in a new issue