added serving openapi options
This commit is contained in:
@@ -87,7 +87,21 @@ pub async fn start_server() {
|
||||
|
||||
// build the axum app and run the server...
|
||||
info!("Starting application...");
|
||||
let app: Router = routes::get_root_router(Arc::new(get_app_state(&db_connection, &settings)));
|
||||
let mut app: Router =
|
||||
routes::get_root_router(Arc::new(get_app_state(&db_connection, &settings)));
|
||||
|
||||
if settings.server.serve_openapi {
|
||||
info!("Enabling OpenAPI documentation endpoint at /openapi.json");
|
||||
app = app.route(
|
||||
"/openapi.json",
|
||||
axum::routing::get(|| async {
|
||||
use utoipa::OpenApi;
|
||||
let doc = routes::ApiDoc::openapi();
|
||||
doc.to_pretty_json()
|
||||
.expect("Failed to serialize OpenAPI doc to JSON")
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
let address = format!("{}:{}", settings.server.address, settings.server.port);
|
||||
info!("Starting server at http://{}", address);
|
||||
|
||||
@@ -3,6 +3,7 @@ pub(crate) const LOGGING_UTC_KEY: &str = "LOGGING.UTC";
|
||||
//
|
||||
pub(crate) const SERVER_ADDRESS_KEY: &str = "SERVER.ADDRESS";
|
||||
pub(crate) const SERVER_PORT_KEY: &str = "SERVER.PORT";
|
||||
pub(crate) const SERVER_SERVE_OPENAPI_KEY: &str = "SERVER.SERVE_OPENAPI";
|
||||
//
|
||||
pub(crate) const DATABASE_URL_KEY: &str = "DATABASE.URL";
|
||||
pub(crate) const DATABASE_MAX_CONNECTIONS_KEY: &str = "DATABASE.MAX_CONNECTIONS";
|
||||
|
||||
@@ -3,6 +3,8 @@ use std::net::IpAddr;
|
||||
use config::{Config, ConfigError};
|
||||
use tracing::warn;
|
||||
|
||||
use crate::configs::key::SERVER_SERVE_OPENAPI_KEY;
|
||||
|
||||
use super::{
|
||||
FromConfig,
|
||||
key::{SERVER_ADDRESS_KEY, SERVER_PORT_KEY},
|
||||
@@ -12,6 +14,7 @@ use super::{
|
||||
pub struct ServerSettings {
|
||||
pub address: IpAddr,
|
||||
pub port: u16,
|
||||
pub serve_openapi: bool,
|
||||
}
|
||||
|
||||
impl FromConfig for ServerSettings {
|
||||
@@ -43,6 +46,17 @@ impl FromConfig for ServerSettings {
|
||||
);
|
||||
DEFAULT_PORT
|
||||
}) as u16,
|
||||
|
||||
serve_openapi: _config
|
||||
.get_bool(SERVER_SERVE_OPENAPI_KEY)
|
||||
.unwrap_or_else(|err| {
|
||||
const DEFAULT_SERVE_OPENAPI: bool = false;
|
||||
warn!(
|
||||
"{} not set or invalid in configuration, defaulting to {}. Error: {}",
|
||||
SERVER_SERVE_OPENAPI_KEY, DEFAULT_SERVE_OPENAPI, err
|
||||
);
|
||||
DEFAULT_SERVE_OPENAPI
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user