From af2f09ea4cbb97d3ee91e30bb58e85508989d63a Mon Sep 17 00:00:00 2001 From: clarkzjw Date: Wed, 26 Jul 2023 12:37:38 -0700 Subject: add example from https://github.com/LemmyNet/activitypub-federation-rust --- fedi/live_federation/main.rs | 70 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 fedi/live_federation/main.rs (limited to 'fedi/live_federation/main.rs') diff --git a/fedi/live_federation/main.rs b/fedi/live_federation/main.rs new file mode 100644 index 0000000..4326226 --- /dev/null +++ b/fedi/live_federation/main.rs @@ -0,0 +1,70 @@ +use crate::{ + database::Database, + http::{http_get_user, http_post_user_inbox, webfinger}, + objects::{person::DbUser, post::DbPost}, + utils::generate_object_id, +}; +use activitypub_federation::config::{FederationConfig, FederationMiddleware}; +use axum::{ + routing::{get, post}, + Router, +}; +use error::Error; +use std::{ + net::ToSocketAddrs, + sync::{Arc, Mutex}, +}; +use tracing::log::{info, LevelFilter}; + +mod activities; +mod database; +mod error; +#[allow(clippy::diverging_sub_expression, clippy::items_after_statements)] +mod http; +mod objects; +mod utils; + +const DOMAIN: &str = "example.com"; +const LOCAL_USER_NAME: &str = "alison"; +const BIND_ADDRESS: &str = "localhost:8003"; + +#[tokio::main] +async fn main() -> Result<(), Error> { + env_logger::builder() + .filter_level(LevelFilter::Warn) + .filter_module("activitypub_federation", LevelFilter::Info) + .filter_module("live_federation", LevelFilter::Info) + .format_timestamp(None) + .init(); + + info!("Setup local user and database"); + let local_user = DbUser::new(DOMAIN, LOCAL_USER_NAME)?; + let database = Arc::new(Database { + users: Mutex::new(vec![local_user]), + }); + + info!("Setup configuration"); + let config = FederationConfig::builder() + .domain(DOMAIN) + .app_data(database) + .build() + .await?; + + info!("Listen with HTTP server on {BIND_ADDRESS}"); + let config = config.clone(); + let app = Router::new() + .route("/:user", get(http_get_user)) + .route("/:user/inbox", post(http_post_user_inbox)) + .route("/.well-known/webfinger", get(webfinger)) + .layer(FederationMiddleware::new(config)); + + let addr = BIND_ADDRESS + .to_socket_addrs()? + .next() + .expect("Failed to lookup domain name"); + axum::Server::bind(&addr) + .serve(app.into_make_service()) + .await?; + + Ok(()) +} -- cgit v1.2.3