feder_runtime_server/
app.rs1use std::sync::{Arc, Mutex};
17
18use crate::config::RuntimeConfig;
19use crate::webfinger::webfinger;
20use crate::{actor::actor, note::note};
21use axum::{Router, http::StatusCode, routing::get};
22use feder_core::{FederConfig, FederCore};
23use feder_vocab::{Actor, Note, Reference};
24
25#[derive(Clone)]
26pub struct AppState {
27 pub core: Arc<Mutex<FederCore>>,
28 pub local_actor: Actor,
29 pub username: String,
30 pub handle_host: String,
31 pub notes: Vec<Note>,
33}
34
35impl AppState {
36 pub fn from_config(config: &RuntimeConfig) -> Self {
37 let mut actor = Actor::person(
38 config.actor_id.clone(),
39 config.inbox.clone(),
40 config.outbox.clone(),
41 );
42 actor.preferred_username = Some(config.preferred_username.clone());
43 actor.name = Some(config.username.clone());
44
45 let mut note = Note::new(config.note_id.clone());
47 note.attributed_to = Some(Reference::id(config.actor_id.clone()));
48 note.content =
49 Some("Hello, World! This is Feder, a portable AP core for many runtimes.".to_string());
50 let notes = vec![note];
51
52 let core = FederCore::new(FederConfig::new(actor.clone()));
53
54 Self {
55 core: Arc::new(Mutex::new(core)),
56 local_actor: actor,
57 username: config.username.clone(),
58 handle_host: config.handle_host.clone(),
59 notes,
60 }
61 }
62}
63
64pub fn build_router(config: &RuntimeConfig) -> Router {
65 let state = AppState::from_config(config);
66
67 Router::new()
68 .route("/healthz", get(healthz))
69 .route("/.well-known/webfinger", get(webfinger))
70 .route("/users/{username}", get(actor))
71 .route("/notes/{id}", get(note))
72 .with_state(state)
73}
74
75async fn healthz() -> StatusCode {
76 StatusCode::NO_CONTENT
77}