add openapi routes to aide template
This commit is contained in:
@@ -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<AppState> {
|
||||
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<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 %}
|
||||
|
||||
@@ -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<AppState> {
|
||||
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 %}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user