activitypub-federation-rust/examples/live_federation/main.rs

72 lines
1.9 KiB
Rust
Raw Normal View History

#![allow(clippy::unwrap_used)]
2023-03-08 03:31:36 +05:30
use crate::{
database::Database,
http::{http_get_user, http_post_user_inbox, webfinger},
objects::{person::DbUser, post::DbPost},
utils::generate_object_id,
};
2023-03-17 02:11:29 +05:30
use activitypub_federation::config::{FederationConfig, FederationMiddleware};
2023-03-08 03:31:36 +05:30
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]
2023-03-08 03:31:36 +05:30
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?;
2023-03-08 03:31:36 +05:30
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))
2023-03-17 02:11:29 +05:30
.layer(FederationMiddleware::new(config));
2023-03-08 03:31:36 +05:30
let addr = BIND_ADDRESS
.to_socket_addrs()?
.next()
.expect("Failed to lookup domain name");
let listener = tokio::net::TcpListener::bind(addr).await?;
axum::serve(listener, app.into_make_service()).await?;
2023-03-08 03:31:36 +05:30
Ok(())
}