Skip to main content

feder_runtime_server/
app.rs

1// Feder: A portable ActivityPub core for many runtimes.
2// Copyright (C) 2026 Feder contributors
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU Affero General Public License as published by
6// the Free Software Foundation, version 3.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11// GNU Affero General Public License for more details.
12//
13// You should have received a copy of the GNU Affero General Public License
14// along with this program.  If not, see <https://www.gnu.org/licenses/>.
15
16use 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    // TODO(#25): Replace this seeded preview note with durable runtime storage.
32    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        // TODO(#25): Replace this seeded preview note with durable runtime storage.
46        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}