use std::sync::{Arc, Mutex};
use crate::config::RuntimeConfig;
use crate::webfinger::webfinger;
use crate::{actor::actor, note::note};
use axum::{Router, http::StatusCode, routing::get};
use feder_core::{FederConfig, FederCore};
use feder_vocab::{Actor, Note, Reference};
#[derive(Clone)]
pub struct AppState {
pub core: Arc<Mutex<FederCore>>,
pub local_actor: Actor,
pub username: String,
pub handle_host: String,
pub notes: Vec<Note>,
}
impl AppState {
pub fn from_config(config: &RuntimeConfig) -> Self {
let mut actor = Actor::person(
config.actor_id.clone(),
config.inbox.clone(),
config.outbox.clone(),
);
actor.preferred_username = Some(config.preferred_username.clone());
actor.name = Some(config.username.clone());
let mut note = Note::new(config.note_id.clone());
note.attributed_to = Some(Reference::id(config.actor_id.clone()));
note.content =
Some("Hello, World! This is Feder, a portable AP core for many runtimes.".to_string());
let notes = vec![note];
let core = FederCore::new(FederConfig::new(actor.clone()));
Self {
core: Arc::new(Mutex::new(core)),
local_actor: actor,
username: config.username.clone(),
handle_host: config.handle_host.clone(),
notes,
}
}
}
pub fn build_router(config: &RuntimeConfig) -> Router {
let state = AppState::from_config(config);
Router::new()
.route("/healthz", get(healthz))
.route("/.well-known/webfinger", get(webfinger))
.route("/users/{username}", get(actor))
.route("/notes/{id}", get(note))
.with_state(state)
}
async fn healthz() -> StatusCode {
StatusCode::NO_CONTENT
}