feat: add environment configuration for database and authentication, update router setup

This commit is contained in:
2025-09-06 01:55:59 +02:00
parent 3dd6c0f64b
commit 6e63dca513
12 changed files with 36 additions and 18 deletions

View File

@@ -25,9 +25,8 @@ pub async fn federate_thought(
return;
}
let base_url = "http://localhost:3000"; // Replace in production
let thought_url = format!("{}/thoughts/{}", base_url, thought.id);
let author_url = format!("{}/users/{}", base_url, author.username);
let thought_url = format!("{}/thoughts/{}", &state.base_url, thought.id);
let author_url = format!("{}/users/{}", &state.base_url, author.username);
// Construct the "Create" activity containing the "Note" object
let activity = json!({
@@ -59,7 +58,7 @@ pub async fn federate_thought(
let client = reqwest::Client::new();
for follower in followers {
let inbox_url = format!("{}/users/{}/inbox", base_url, follower.username);
let inbox_url = format!("{}/users/{}/inbox", &state.base_url, follower.username);
tracing::info!("Federating post {} to {}", thought.id, inbox_url);
let res = client.post(&inbox_url).json(&activity).send().await;

View File

@@ -9,8 +9,11 @@ use app::state::AppState;
use crate::routers::create_router;
// TODO: middleware, logging, authentication
pub fn setup_router(conn: DatabaseConnection) -> Router {
create_router(AppState { conn })
pub fn setup_router(conn: DatabaseConnection, config: &Config) -> Router {
create_router(AppState {
conn,
base_url: config.base_url.clone(),
})
}
pub fn setup_config() -> Config {

View File

@@ -220,8 +220,7 @@ async fn get_user_by_param(
// This is the logic from `user_actor_get`.
match get_user_by_username(&state.conn, &username).await {
Ok(Some(user)) => {
let base_url = "http://localhost:3000";
let user_url = format!("{}/users/{}", base_url, user.username);
let user_url = format!("{}/users/{}", &state.base_url, user.username);
let actor = json!({
"@context": [
"https://www.w3.org/ns/activitystreams",
@@ -272,13 +271,12 @@ async fn user_outbox_get(
let thoughts = get_thoughts_by_user(&state.conn, user.id).await?;
// Format the outbox as an ActivityPub OrderedCollection
let base_url = "http://localhost:3000";
let outbox_url = format!("{}/users/{}/outbox", base_url, username);
let outbox_url = format!("{}/users/{}/outbox", &state.base_url, username);
let items: Vec<Value> = thoughts
.into_iter()
.map(|thought| {
let thought_url = format!("{}/thoughts/{}", base_url, thought.id);
let author_url = format!("{}/users/{}", base_url, thought.author_username);
let thought_url = format!("{}/thoughts/{}", &state.base_url, thought.id);
let author_url = format!("{}/users/{}", &state.base_url, thought.author_username);
json!({
"id": format!("{}/activity", thought_url),
"type": "Create",

View File

@@ -45,8 +45,7 @@ pub async fn webfinger(
_ => return Err((axum::http::StatusCode::NOT_FOUND, "User not found")),
};
let base_url = "http://localhost:3000";
let user_url = Url::parse(&format!("{}/users/{}", base_url, user.username)).unwrap();
let user_url = Url::parse(&format!("{}/users/{}", &state.base_url, user.username)).unwrap();
let response = WebFingerResponse {
subject: query.resource,