add openapi routes to aide template

This commit is contained in:
fa-sharp
2026-02-20 04:57:27 -05:00
parent 2efaa86440
commit 2c807d62bc
4 changed files with 75 additions and 6 deletions

View File

@@ -1,9 +1,6 @@
name: Dependency Check name: Dependency Check
on: on:
schedule:
# Run every Monday at 9:00 AM UTC
- cron: "0 9 * * 1"
workflow_dispatch: # Allow manual triggering workflow_dispatch: # Allow manual triggering
env: env:

View File

@@ -10,6 +10,7 @@ aide = { version = "0.16.0-alpha.1", features = [
"axum", "axum",
"axum-json", "axum-json",
"axum-query", "axum-query",
"swagger"
] } ] }
{% endif %} {% endif %}
anyhow = "1.0.101" anyhow = "1.0.101"

View File

@@ -1,6 +1,11 @@
{% if include_aide %}
use axum::extract::{Json, Query};
use schemars::JsonSchema;
use serde::Deserialize;
{% endif %}
use crate::state::AppState; use crate::state::AppState;
/// `/api/hello` {% if include_aide == false %}
pub fn routes() -> axum::Router<AppState> { pub fn routes() -> axum::Router<AppState> {
axum::Router::new() axum::Router::new()
.route("/", axum::routing::get(hello_handler)) .route("/", axum::routing::get(hello_handler))
@@ -14,3 +19,36 @@ async fn hello_handler() -> String {
async fn post_handler() -> String { async fn post_handler() -> String {
"Post handler!".to_string() "Post handler!".to_string()
} }
{% else %}
pub fn routes() -> aide::axum::ApiRouter<AppState> {
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<HelloQuery>) -> String {
format!("Hello, {}!", query.name)
}
async fn post_handler(Json(body): Json<PostBody>) -> 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 %}

View File

@@ -4,11 +4,44 @@ use crate::state::AppState;
pub mod hello; pub mod hello;
/// Adds all API routes to the server /// Adds all API routes to the server under `/api`
pub fn plugin() -> AdHocPlugin<AppState> { pub fn plugin() -> AdHocPlugin<AppState> {
AdHocPlugin::new().on_setup(|router, _state| { 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) Ok(router)
{% endif %}
}) })
} }