Skip to content
Clone

refactor(webapi): refresh crate across router, health, openapi and services

zhenyi committed
parent 4fd870f commit faf14af Browse files

6 files changed

+2 −34

File tree 6
Modified lib/webapi/config.rs View lib/webapi/config.rs
@@ -11,10 +11,6 @@
1111use storage::config::StorageSettings;
1212use user::UserConfig;
1313
14/// Bundle of all settings required to construct the full service stack.
15///
16/// Every field provides a sensible `Default`, so the whole bundle can be
17/// derived from environment/config sources and then overridden piecewise.
1814#[derive(Clone, Default)]
1915pub struct AppConfig {
2016 pub db: AppDatabaseSettings,
Modified lib/webapi/health.rs View lib/webapi/health.rs
@@ -2,14 +2,12 @@
22use serde::Serialize;
33use utoipa::ToSchema;
44
5/// Response body for the liveness/readiness probes.
65#[derive(Debug, Serialize, ToSchema)]
76pub struct HealthStatus {
87 pub status: &'static str,
98 pub service: &'static str,
109}
1110
12/// Liveness probe: the process is up and able to serve requests.
1311#[utoipa::path(
1412 get,
1513 path = "/health/live",
@@ -25,7 +23,6 @@
2523 })
2624}
2725
28/// Readiness probe: the service stack is fully initialised.
2926#[utoipa::path(
3027 get,
3128 path = "/health/ready",
@@ -41,7 +38,6 @@
4138 })
4239}
4340
44/// Convenience for probes that want a non-200 signal instead of a JSON body.
4541pub fn not_ready() -> (StatusCode, Json<HealthStatus>) {
4642 (
4743 StatusCode::SERVICE_UNAVAILABLE,
Modified lib/webapi/lib.rs View lib/webapi/lib.rs
@@ -1,9 +1,3 @@
1//! Unified web API entry crate.
2//!
3//! Composes every domain router (auth, issues, project, repo, space, user)
4//! behind a single router under [`router::API_PREFIX`], alongside health
5//! probes and the merged OpenAPI document.
6
71pub mod config;
82pub mod health;
93pub mod openapi;
Modified lib/webapi/openapi.rs View lib/webapi/openapi.rs
@@ -4,11 +4,6 @@
44use once_cell::sync::Lazy;
55use crate::{health, router::API_PREFIX};
66
7/// Root OpenAPI document that only carries the health endpoints.
8///
9/// The per-domain documents are merged at runtime by [`openapi`] so that every
10/// path keeps its absolute form (e.g. `/issues/{channel_id}`) before the
11/// unified [`API_PREFIX`] is applied.
127#[derive(OpenApi)]
138#[openapi(
149 info(
@@ -20,7 +15,6 @@
2015)]
2116struct WebapiApiDoc;
2217
23/// Build the merged OpenAPI document with every path prefixed by [`API_PREFIX`].
2418pub fn openapi() -> utoipa::openapi::OpenApi {
2519 let mut doc = export::openapi_concat!(
2620 WebapiApiDoc,
Modified lib/webapi/router.rs View lib/webapi/router.rs
@@ -2,10 +2,8 @@
22
33use crate::{health, openapi, services::AppServices};
44
5/// Every API route is mounted below this prefix.
65pub const API_PREFIX: &str = "/api/v1";
76
8/// The unified total router: health probes, OpenAPI docs and every domain router.
97pub fn router(services: AppServices) -> Router {
108 Router::new()
119 .nest(
Modified lib/webapi/services.rs View lib/webapi/services.rs
@@ -20,7 +20,6 @@
2020
2121use crate::config::AppConfig;
2222
23/// Error raised while constructing the service stack.
2423#[derive(Debug, Error)]
2524pub enum WebapiError {
2625 #[error("database: {0}")]
@@ -33,15 +32,12 @@
3332 Queue(#[from] queue::QueueError),
3433 #[error("storage: {0}")]
3534 Storage(#[from] storage::StorageError),
36 /// Auth routes require a working email transport (verification codes,
37 /// password reset, email reclaim). Refuse to boot without one.
38 #[error("email service is required (auth depends on it); enable email in config")]
35 #[error("email service is required (auth depends on it); enable email in config")]
3936 EmailRequired,
4037}
4138
4239pub type WebapiResult<T> = Result<T, WebapiError>;
4340
44/// All services built from an [`AppConfig`], ready to be handed to the router.
4541pub struct AppServices {
4642 pub db: Db,
4743 pub kv: Arc<RedisCache>,
@@ -58,13 +54,7 @@
5854}
5955
6056impl AppServices {
61 /// Build the full service stack from configuration.
62 ///
63 /// The queue is optional (its settings may disable it); when it is
64 /// disabled the email service cannot be built either, and because the
65 /// auth service requires a concrete email transport this fails startup
66 /// with [`WebapiError::EmailRequired`].
67 pub async fn build(cfg: &AppConfig) -> WebapiResult<Self> {
57 pub async fn build(cfg: &AppConfig) -> WebapiResult<Self> {
6858 tracing::info!("building service stack");
6959
7060 let db = Db::from_config(&cfg.db).await?;