diff options
Diffstat (limited to 'fedi/live_federation/main.rs')
-rw-r--r-- | fedi/live_federation/main.rs | 70 |
1 files changed, 70 insertions, 0 deletions
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 @@ | |||
1 | use crate::{ | ||
2 | database::Database, | ||
3 | http::{http_get_user, http_post_user_inbox, webfinger}, | ||
4 | objects::{person::DbUser, post::DbPost}, | ||
5 | utils::generate_object_id, | ||
6 | }; | ||
7 | use activitypub_federation::config::{FederationConfig, FederationMiddleware}; | ||
8 | use axum::{ | ||
9 | routing::{get, post}, | ||
10 | Router, | ||
11 | }; | ||
12 | use error::Error; | ||
13 | use std::{ | ||
14 | net::ToSocketAddrs, | ||
15 | sync::{Arc, Mutex}, | ||
16 | }; | ||
17 | use tracing::log::{info, LevelFilter}; | ||
18 | |||
19 | mod activities; | ||
20 | mod database; | ||
21 | mod error; | ||
22 | #[allow(clippy::diverging_sub_expression, clippy::items_after_statements)] | ||
23 | mod http; | ||
24 | mod objects; | ||
25 | mod utils; | ||
26 | |||
27 | const DOMAIN: &str = "example.com"; | ||
28 | const LOCAL_USER_NAME: &str = "alison"; | ||
29 | const BIND_ADDRESS: &str = "localhost:8003"; | ||
30 | |||
31 | #[tokio::main] | ||
32 | async fn main() -> Result<(), Error> { | ||
33 | env_logger::builder() | ||
34 | .filter_level(LevelFilter::Warn) | ||
35 | .filter_module("activitypub_federation", LevelFilter::Info) | ||
36 | .filter_module("live_federation", LevelFilter::Info) | ||
37 | .format_timestamp(None) | ||
38 | .init(); | ||
39 | |||
40 | info!("Setup local user and database"); | ||
41 | let local_user = DbUser::new(DOMAIN, LOCAL_USER_NAME)?; | ||
42 | let database = Arc::new(Database { | ||
43 | users: Mutex::new(vec![local_user]), | ||
44 | }); | ||
45 | |||
46 | info!("Setup configuration"); | ||
47 | let config = FederationConfig::builder() | ||
48 | .domain(DOMAIN) | ||
49 | .app_data(database) | ||
50 | .build() | ||
51 | .await?; | ||
52 | |||
53 | info!("Listen with HTTP server on {BIND_ADDRESS}"); | ||
54 | let config = config.clone(); | ||
55 | let app = Router::new() | ||
56 | .route("/:user", get(http_get_user)) | ||
57 | .route("/:user/inbox", post(http_post_user_inbox)) | ||
58 | .route("/.well-known/webfinger", get(webfinger)) | ||
59 | .layer(FederationMiddleware::new(config)); | ||
60 | |||
61 | let addr = BIND_ADDRESS | ||
62 | .to_socket_addrs()? | ||
63 | .next() | ||
64 | .expect("Failed to lookup domain name"); | ||
65 | axum::Server::bind(&addr) | ||
66 | .serve(app.into_make_service()) | ||
67 | .await?; | ||
68 | |||
69 | Ok(()) | ||
70 | } | ||