feat: add redoc UI support

This commit is contained in:
Ning Sun
2022-10-28 11:48:33 +08:00
parent fc771e4d57
commit df5df3ed1d
2 changed files with 34 additions and 1 deletions

View File

@@ -11,6 +11,7 @@ use aide::axum::routing as apirouting;
use aide::axum::{ApiRouter, IntoApiResponse};
use aide::openapi::{Info, OpenApi};
use async_trait::async_trait;
use axum::response::Html;
use axum::Extension;
use axum::{error_handling::HandleErrorLayer, response::Json, routing, BoxError, Router};
use common_query::Output;
@@ -204,10 +205,14 @@ async fn shutdown_signal() {
.expect("failed to install CTRL+C signal handler");
}
async fn serve_api(Extension(api): Extension<OpenApi>) -> impl IntoApiResponse {
async fn serve_api(Extension(api): Extension<Arc<OpenApi>>) -> impl IntoApiResponse {
Json(api)
}
async fn serve_docs() -> Html<String> {
Html(include_str!("http/redoc.html").to_owned())
}
impl HttpServer {
pub fn new(sql_handler: SqlQueryHandlerRef) -> Self {
Self {
@@ -269,6 +274,7 @@ impl HttpServer {
.api_route("/scripts", apirouting::post(handler::scripts))
.api_route("/run-script", apirouting::post(handler::run_script))
.route("/private/api.json", apirouting::get(serve_api))
.route("/private/docs", apirouting::get(serve_docs))
.finish_api(&mut api)
.layer(Extension(Arc::new(api)));

View File

@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<head>
<title>Redoc</title>
<!-- needed for adaptive design -->
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link
href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700"
rel="stylesheet"
/>
<!--
Redoc doesn't change outer page styles
-->
<style>
body {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<redoc spec-url="/v1/private/api.json"></redoc>
<script src="https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js"></script>
</body>
</html>