Skip to content

Commit

Permalink
API doc changes
Browse files Browse the repository at this point in the history
  • Loading branch information
RubberDuckShobe committed Nov 2, 2024
1 parent 7776ff4 commit 61488bf
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
39 changes: 35 additions & 4 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use axum::{Json, Router};
use serde::Serialize;
use utoipa::{openapi::OpenApi, OpenApi as OpenApiTrait, ToSchema};
use utoipa::{
openapi::{
security::{HttpAuthScheme, HttpBuilder, SecurityScheme},
OpenApi,
},
Modify, OpenApi as OpenApiTrait, ToSchema,
};
use utoipa_axum::{router::OpenApiRouter, routes};

use crate::{
Expand All @@ -14,12 +20,32 @@ mod rivals;
mod songs;

#[derive(OpenApiTrait)]
#[openapi(servers((url = "/api")), security(
#[openapi(
modifiers(&SecurityAddon),
servers((url = "/api")), security(
(),
("token_jwt" = [])
))]
pub struct ApiDoc;

struct SecurityAddon;

impl Modify for SecurityAddon {
fn modify(&self, openapi: &mut utoipa::openapi::OpenApi) {
if let Some(components) = openapi.components.as_mut() {
components.add_security_scheme(
"token_jwt",
SecurityScheme::Http(
HttpBuilder::new()
.scheme(HttpAuthScheme::Bearer)
.bearer_format("JWT")
.build(),
),
)
}
}
}

pub fn routes() -> (Router<AppState>, OpenApi) {
OpenApiRouter::with_openapi(ApiDoc::openapi())
.routes(routes!(health_check))
Expand All @@ -37,12 +63,17 @@ struct HealthCheck {
radio_status: String,
}

/// Get health of the API.
/// Get health of the server
#[utoipa::path(
method(get),
path = "/healthCheck",
responses(
(status = OK, description = "Success", body = HealthCheck, content_type = "application/json")
(status = OK, description = "Success",
body = HealthCheck, content_type = "application/json",
examples = (json!(r#"{ "status": "ok", "radioStatus": "2 song(s)" }"#))),
(status = OK, description = "Server works, but Radio is broken",
body = HealthCheck, content_type = "application/json",
example = json!(r#"{ "status": "ok", "radioStatus": "error" }"#)),
)
)]
async fn health_check() -> Result<Json<HealthCheck>, RouteError> {
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![warn(
#![warn(
clippy::nursery,
clippy::correctness,
clippy::style,
Expand Down Expand Up @@ -194,7 +194,7 @@ fn make_router(state: AppState) -> Router {
.nest("//as_steamlogin", routes_steam_doubleslash()) // for that one edge case
.nest("/as", routes_as(&state.config.radio.cgr_location))
.nest("/api", api_router)
.merge(Scalar::with_url("/api-docs", openapi))
.merge(Scalar::with_url("/api/docs", openapi))
.layer(session_layer)
.layer(
// TAKEN FROM: https://github.com/tokio-rs/axum/blob/d1fb14ead1063efe31ae3202e947ffd569875c0b/examples/error-handling/src/main.rs#L60-L77
Expand Down

0 comments on commit 61488bf

Please sign in to comment.