Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .env-example
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
DATABASE_URL="mysql://username:password@host:port/database_name"
DATABASE_URL="mysql://username:password@host:port/database_name"
APP_ENVIRONMENT="dev"
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use axum::http::header::{AUTHORIZATION, CONTENT_TYPE};
use axum::http::{HeaderName, HeaderValue};
use f5a_services::routes;
use f5a_services::shared::context::AppContext;
use f5a_services::shared::env::AppEnvironment;
use f5a_services::shared::trace::RequestIdSpan;
use sea_orm::Database;
use std::sync::Arc;
Expand Down Expand Up @@ -44,7 +45,12 @@ async fn main() {
conn: Arc::new(conn),
};

let router = routes::router()
let app_env: AppEnvironment = env::var("APP_ENVIRONMENT")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or_default();

let router = routes::router(app_env)
.with_state(app_ctx)
.layer(service_layers)
.layer(
Expand Down
10 changes: 7 additions & 3 deletions src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use utoipa::OpenApi;
use utoipa_axum::{router::OpenApiRouter, routes};
use utoipa_swagger_ui::SwaggerUi;

use crate::{root, shared::context::AppContext, users};
use crate::{root, shared::context::AppContext, shared::env::AppEnvironment, users};

#[derive(OpenApi)]
#[openapi(
Expand Down Expand Up @@ -36,7 +36,7 @@ Currently no authentication required (internal service).
)]
pub struct ApiDoc;

pub fn router() -> axum::Router<AppContext> {
pub fn router(app_env: AppEnvironment) -> axum::Router<AppContext> {
let (router, api) = OpenApiRouter::with_openapi(ApiDoc::openapi())
.routes(routes!(root::http::handlers::root_handler))
.routes(routes!(
Expand All @@ -51,5 +51,9 @@ pub fn router() -> axum::Router<AppContext> {
))
.split_for_parts();

router.merge(SwaggerUi::new("/swagger-ui").url("/apidoc/openapi.json", api))
if app_env.is_swagger_ui_enabled() {
router.merge(SwaggerUi::new("/swagger-ui").url("/apidoc/openapi.json", api))
} else {
router
}
}
27 changes: 27 additions & 0 deletions src/shared/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::str::FromStr;

#[derive(Debug, Clone, PartialEq, Default)]
pub enum AppEnvironment {
#[default]
Dev,
Prod,
}

impl AppEnvironment {
pub fn is_swagger_ui_enabled(&self) -> bool {
matches!(self, Self::Dev)
}
}

impl FromStr for AppEnvironment {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"dev" => Ok(Self::Dev),
"prod" => Ok(Self::Prod),
_ => Err(format!(
"invalid APP_ENVIRONMENT: {s}, expected 'dev' or 'prod'"
)),
}
}
}
1 change: 1 addition & 0 deletions src/shared/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod context;
pub mod env;
pub mod error;
pub mod pagination;
pub mod response;
Expand Down
2 changes: 1 addition & 1 deletion src/shared/response/bad_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl IntoFields for ValidationErrors {
Field::new(&field_name, field_message, &error.code)
}));

fields.sort_by(|a, b| a.field.to_lowercase().cmp(&b.field.to_lowercase()));
fields.sort_by_key(|a| a.field.to_lowercase());

fields
}
Expand Down
3 changes: 2 additions & 1 deletion tests/setup/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::sync::Arc;

use f5a_services::shared::env::AppEnvironment;
use f5a_services::{routes, shared::context::AppContext};
use sea_orm::{ConnectionTrait, Database, DatabaseConnection};

Expand Down Expand Up @@ -28,7 +29,7 @@ impl TestContext {
}

pub fn configure(&self) -> axum::Router {
routes::router().with_state(AppContext {
routes::router(AppEnvironment::Dev).with_state(AppContext {
conn: Arc::clone(&self.db),
})
}
Expand Down
2 changes: 1 addition & 1 deletion tests/users/detail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async fn test_read_user_detail() {
let value = res.into_value::<UserDetail>().await;
assert_eq!(value.id, 1);
assert_eq!(value.username, "idesoftd");
assert_eq!(value.disabled, true);
assert!(value.disabled);
}

#[tokio::test]
Expand Down
4 changes: 2 additions & 2 deletions tests/users/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use sea_orm::{ActiveModelTrait, ActiveValue, ConnectionTrait, DbErr};
pub async fn insert_idesoft_user(
conn: &impl ConnectionTrait,
) -> Result<schemas::user::ActiveModel, DbErr> {
let created_at = NaiveDate::from_ymd_opt(2026, 03, 19)
let created_at = NaiveDate::from_ymd_opt(2026, 3, 19)
.and_then(|date| date.and_hms_opt(10, 10, 10))
.unwrap();

Expand All @@ -23,7 +23,7 @@ pub async fn insert_idesoft_user(
pub async fn insert_blue_bird_user(
conn: &impl ConnectionTrait,
) -> Result<schemas::user::ActiveModel, DbErr> {
let created_at = NaiveDate::from_ymd_opt(2026, 03, 19)
let created_at = NaiveDate::from_ymd_opt(2026, 3, 19)
.and_then(|date| date.and_hms_opt(10, 10, 10))
.unwrap();

Expand Down
6 changes: 3 additions & 3 deletions tests/users/paginated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async fn it_reads_paginated_users() {
assert_eq!(value.len(), 1);
assert_eq!(value[0].username, "chameleon");
assert_eq!(value[0].id, 3);
assert_eq!(value[0].disabled, false);
assert!(!value[0].disabled);

// assert for page 1
let app: axum::Router = ctx.configure();
Expand All @@ -47,7 +47,7 @@ async fn it_reads_paginated_users() {
assert_eq!(value.len(), 1);
assert_eq!(value[0].username, "bluebird");
assert_eq!(value[0].id, 2);
assert_eq!(value[0].disabled, false);
assert!(!value[0].disabled);

// assert for page 2
let app: axum::Router = ctx.configure();
Expand All @@ -61,7 +61,7 @@ async fn it_reads_paginated_users() {
assert_eq!(value.len(), 1);
assert_eq!(value[0].username, "idesoftd");
assert_eq!(value[0].id, 1);
assert_eq!(value[0].disabled, true);
assert!(value[0].disabled);

// assert for page 3
let app: axum::Router = ctx.configure();
Expand Down
Loading