Refactor handlers and OpenAPI documentation for improved readability and consistency
Some checks failed
lint / lint (push) Has been cancelled
test / unit (push) Has been cancelled
test / integration (push) Has been cancelled
lint / lint (pull_request) Failing after 6m49s
test / unit (pull_request) Successful in 16m24s
test / integration (pull_request) Failing after 17m7s

- Reorganized imports in health, notifications, social, thoughts, and users handlers for clarity.
- Updated function signatures in handlers to improve readability by aligning parameters.
- Enhanced JSON response formatting in notifications and thoughts handlers.
- Improved error handling in user-related functions.
- Refactored OpenAPI documentation to maintain consistent formatting and structure.
- Cleaned up unnecessary code and comments across various files.
- Ensured consistent use of `Arc` for shared state in AppState and WorkerHandlers.
This commit is contained in:
2026-05-14 16:28:57 +02:00
parent 004bfb427b
commit 10c4a66de5
47 changed files with 2406 additions and 723 deletions

View File

@@ -17,12 +17,9 @@ impl Config {
pub fn from_env() -> Self {
dotenvy::dotenv().ok();
Self {
database_url: std::env::var("DATABASE_URL")
.expect("DATABASE_URL is required"),
jwt_secret: std::env::var("JWT_SECRET")
.expect("JWT_SECRET is required"),
base_url: std::env::var("BASE_URL")
.unwrap_or_else(|_| "http://localhost:3000".into()),
database_url: std::env::var("DATABASE_URL").expect("DATABASE_URL is required"),
jwt_secret: std::env::var("JWT_SECRET").expect("JWT_SECRET is required"),
base_url: std::env::var("BASE_URL").unwrap_or_else(|_| "http://localhost:3000".into()),
nats_url: std::env::var("NATS_URL").ok(),
port: std::env::var("PORT")
.ok()
@@ -36,7 +33,9 @@ impl Config {
.unwrap_or(true),
host: std::env::var("HOST").unwrap_or_else(|_| "0.0.0.0".into()),
cors_origins: std::env::var("CORS_ORIGINS").unwrap_or_else(|_| "*".into()),
rate_limit: std::env::var("RATE_LIMIT").ok().and_then(|v| v.parse().ok()),
rate_limit: std::env::var("RATE_LIMIT")
.ok()
.and_then(|v| v.parse().ok()),
}
}
}

View File

@@ -1,6 +1,6 @@
use std::sync::Arc;
use async_trait::async_trait;
use sqlx::PgPool;
use std::sync::Arc;
use activitypub::ThoughtsObjectHandler;
use activitypub_base::{ApFederationConfig, FederationData};
@@ -23,7 +23,9 @@ struct NoOpEventPublisher;
#[async_trait]
impl EventPublisher for NoOpEventPublisher {
async fn publish(&self, _e: &DomainEvent) -> Result<(), DomainError> { Ok(()) }
async fn publish(&self, _e: &DomainEvent) -> Result<(), DomainError> {
Ok(())
}
}
pub async fn build(cfg: &Config) -> Infrastructure {
@@ -58,7 +60,10 @@ pub async fn build(cfg: &Config) -> Infrastructure {
// 3. ActivityPub federation
let fed_data = FederationData::new(
Arc::new(PostgresFederationRepository::new(pool.clone())),
Arc::new(PostgresApUserRepository::new(pool.clone(), cfg.base_url.clone())),
Arc::new(PostgresApUserRepository::new(
pool.clone(),
cfg.base_url.clone(),
)),
Arc::new(ThoughtsObjectHandler::new(
Arc::new(PgActivityPubRepository::new(pool.clone())),
&cfg.base_url,
@@ -74,22 +79,31 @@ pub async fn build(cfg: &Config) -> Infrastructure {
// 4. Application state
let state = AppState {
users: Arc::new(postgres::user::PgUserRepository::new(pool.clone())),
thoughts: Arc::new(postgres::thought::PgThoughtRepository::new(pool.clone())),
likes: Arc::new(postgres::like::PgLikeRepository::new(pool.clone())),
boosts: Arc::new(postgres::boost::PgBoostRepository::new(pool.clone())),
follows: Arc::new(postgres::follow::PgFollowRepository::new(pool.clone())),
blocks: Arc::new(postgres::block::PgBlockRepository::new(pool.clone())),
tags: Arc::new(postgres::tag::PgTagRepository::new(pool.clone())),
api_keys: Arc::new(postgres::api_key::PgApiKeyRepository::new(pool.clone())),
top_friends: Arc::new(postgres::top_friend::PgTopFriendRepository::new(pool.clone())),
notifications: Arc::new(postgres::notification::PgNotificationRepository::new(pool.clone())),
remote_actors: Arc::new(postgres::remote_actor::PgRemoteActorRepository::new(pool.clone())),
feed: Arc::new(postgres::feed::PgFeedRepository::new(pool.clone())),
search: Arc::new(postgres_search::PgSearchRepository::new(pool.clone())),
auth: Arc::new(auth::JwtAuthService::new(cfg.jwt_secret.clone(), 86400 * 30)),
hasher: Arc::new(auth::Argon2PasswordHasher),
events: event_publisher,
users: Arc::new(postgres::user::PgUserRepository::new(pool.clone())),
thoughts: Arc::new(postgres::thought::PgThoughtRepository::new(pool.clone())),
likes: Arc::new(postgres::like::PgLikeRepository::new(pool.clone())),
boosts: Arc::new(postgres::boost::PgBoostRepository::new(pool.clone())),
follows: Arc::new(postgres::follow::PgFollowRepository::new(pool.clone())),
blocks: Arc::new(postgres::block::PgBlockRepository::new(pool.clone())),
tags: Arc::new(postgres::tag::PgTagRepository::new(pool.clone())),
api_keys: Arc::new(postgres::api_key::PgApiKeyRepository::new(pool.clone())),
top_friends: Arc::new(postgres::top_friend::PgTopFriendRepository::new(
pool.clone(),
)),
notifications: Arc::new(postgres::notification::PgNotificationRepository::new(
pool.clone(),
)),
remote_actors: Arc::new(postgres::remote_actor::PgRemoteActorRepository::new(
pool.clone(),
)),
feed: Arc::new(postgres::feed::PgFeedRepository::new(pool.clone())),
search: Arc::new(postgres_search::PgSearchRepository::new(pool.clone())),
auth: Arc::new(auth::JwtAuthService::new(
cfg.jwt_secret.clone(),
86400 * 30,
)),
hasher: Arc::new(auth::Argon2PasswordHasher),
events: event_publisher,
};
Infrastructure { state, fed_config }

View File

@@ -1,10 +1,6 @@
mod config;
mod factory;
use std::net::SocketAddr;
use std::sync::Arc;
use tower_http::cors::{AllowOrigin, CorsLayer};
use tracing_subscriber::EnvFilter;
use activitypub_base::{
actor_handler::actor_handler,
followers_handler::{followers_handler, following_handler},
@@ -13,6 +9,10 @@ use activitypub_base::{
outbox::outbox_handler,
webfinger::webfinger_handler,
};
use std::net::SocketAddr;
use std::sync::Arc;
use tower_http::cors::{AllowOrigin, CorsLayer};
use tracing_subscriber::EnvFilter;
#[tokio::main]
async fn main() {
@@ -41,14 +41,32 @@ async fn main() {
};
let ap_router = axum::Router::new()
.route("/.well-known/webfinger", axum::routing::get(webfinger_handler))
.route("/.well-known/nodeinfo", axum::routing::get(nodeinfo_well_known_handler))
.route(
"/.well-known/webfinger",
axum::routing::get(webfinger_handler),
)
.route(
"/.well-known/nodeinfo",
axum::routing::get(nodeinfo_well_known_handler),
)
.route("/nodeinfo/2.0", axum::routing::get(nodeinfo_handler))
.route("/users/{username}", axum::routing::get(actor_handler))
.route("/users/{username}/inbox", axum::routing::post(inbox_handler))
.route("/users/{username}/outbox", axum::routing::get(outbox_handler))
.route("/users/{username}/followers", axum::routing::get(followers_handler))
.route("/users/{username}/following", axum::routing::get(following_handler))
.route(
"/users/{username}/inbox",
axum::routing::post(inbox_handler),
)
.route(
"/users/{username}/outbox",
axum::routing::get(outbox_handler),
)
.route(
"/users/{username}/followers",
axum::routing::get(followers_handler),
)
.route(
"/users/{username}/following",
axum::routing::get(following_handler),
)
.layer(infra.fed_config.middleware());
let base = presentation::routes::router()
@@ -77,8 +95,7 @@ async fn main() {
let limiter = governor_conf.limiter().clone();
tokio::spawn(async move {
let mut interval =
tokio::time::interval(std::time::Duration::from_secs(60));
let mut interval = tokio::time::interval(std::time::Duration::from_secs(60));
loop {
interval.tick().await;
limiter.retain_recent();