diff --git a/.github/workflows/dependency-check.yml b/.github/workflows/dependency-check.yml index fccc0da..3f0de8f 100644 --- a/.github/workflows/dependency-check.yml +++ b/.github/workflows/dependency-check.yml @@ -1,9 +1,6 @@ name: Dependency Check on: - schedule: - # Run every Monday at 9:00 AM UTC - - cron: "0 9 * * 1" workflow_dispatch: # Allow manual triggering env: diff --git a/Cargo.toml.liquid b/Cargo.toml.liquid index 2fdf95c..4b42145 100644 --- a/Cargo.toml.liquid +++ b/Cargo.toml.liquid @@ -10,6 +10,7 @@ aide = { version = "0.16.0-alpha.1", features = [ "axum", "axum-json", "axum-query", + "swagger" ] } {% endif %} anyhow = "1.0.101" diff --git a/src/routes/hello.rs b/src/routes/hello.rs index ad9df18..5007be0 100644 --- a/src/routes/hello.rs +++ b/src/routes/hello.rs @@ -1,6 +1,11 @@ +{% if include_aide %} +use axum::extract::{Json, Query}; +use schemars::JsonSchema; +use serde::Deserialize; +{% endif %} use crate::state::AppState; -/// `/api/hello` +{% if include_aide == false %} pub fn routes() -> axum::Router { axum::Router::new() .route("/", axum::routing::get(hello_handler)) @@ -14,3 +19,36 @@ async fn hello_handler() -> String { async fn post_handler() -> String { "Post handler!".to_string() } +{% else %} +pub fn routes() -> aide::axum::ApiRouter { + aide::axum::ApiRouter::new() + .api_route( + "/", + aide::axum::routing::get_with(hello_handler, |op| op.summary("Greet user")), + ) + .api_route( + "/", + aide::axum::routing::post_with(post_handler, |op| op.summary("Relay message")), + ) +} + +async fn hello_handler(Query(query): Query) -> String { + format!("Hello, {}!", query.name) +} + +async fn post_handler(Json(body): Json) -> String { + format!("Received message: {}", body.message) +} + +#[derive(Debug, Clone, Deserialize, JsonSchema)] +struct HelloQuery { + /// The name of the person to greet + name: String, +} + +#[derive(Debug, Clone, Deserialize, JsonSchema)] +struct PostBody { + /// The message to relay + message: String, +} +{% endif %} diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 3c17435..5274597 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -4,11 +4,44 @@ use crate::state::AppState; pub mod hello; -/// Adds all API routes to the server +/// Adds all API routes to the server under `/api` pub fn plugin() -> AdHocPlugin { AdHocPlugin::new().on_setup(|router, _state| { - let router = router.nest("/api/hello", hello::routes()); + {% if include_aide == false %} + let api_routes = axum::Router::new().nest("/hello", hello::routes()); + + Ok(router.nest("/api", api_routes)) + {% else %} + // Build API routes + let api_router = aide::axum::ApiRouter::new().nest("/hello", hello::routes()); + + // OpenAPI configuration + let mut openapi = aide::openapi::OpenApi { + info: aide::openapi::Info { + title: "{{project-name}}".to_string(), + version: env!("CARGO_PKG_VERSION").to_string(), + description: Some("{{project-description}}".to_string()), + ..Default::default() + }, + servers: vec![aide::openapi::Server { + url: "/api".to_string(), + ..Default::default() + }], + ..Default::default() + }; + + // Add API routes to the router under `/api` and also merge them into the OpenAPI docs + let router = router.nest("/api", api_router.finish_api(&mut openapi)); + + // Add OpenAPI documentation routes + let openapi_json = serde_json::to_string_pretty(&openapi).unwrap(); + let openapi_route = axum::routing::get(|| async move { openapi_json }); + let swagger_route = aide::swagger::Swagger::new("/api/openapi.json").axum_route(); + let router = router + .route("/api/openapi.json", openapi_route) + .route("/api/docs", swagger_route.into()); Ok(router) + {% endif %} }) }