Skip to content

Commit

Permalink
feat: feature gate apidocs
Browse files Browse the repository at this point in the history
  • Loading branch information
sunng87 committed Nov 28, 2024
1 parent 8bdef77 commit 797dfad
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 15 deletions.
5 changes: 4 additions & 1 deletion src/servers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ edition.workspace = true
license.workspace = true

[features]
default = []
dashboard = []
mem-prof = ["dep:common-mem-prof"]
pprof = ["dep:common-pprof"]
testing = []
apidocs = ["dep:aide"]


[lints]
workspace = true

[dependencies]
ahash = "0.8"
aide = { version = "0.9", features = ["axum"] }
aide = { version = "0.9", features = ["axum"], optional = true }
api.workspace = true
arrow.workspace = true
arrow-flight.workspace = true
Expand Down
76 changes: 62 additions & 14 deletions src/servers/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,22 @@ use std::net::SocketAddr;
use std::sync::Mutex as StdMutex;
use std::time::Duration;

use aide::axum::{routing as apirouting, ApiRouter, IntoApiResponse};
use aide::openapi::{Info, OpenApi, Server as OpenAPIServer};
use aide::OperationOutput;
#[cfg(feature = "apidocs")]
use aide::{
axum::{routing as apirouting, ApiRouter, IntoApiResponse},
openapi::{Info, OpenApi, Server as OpenAPIServer},
OperationOutput,
};
use async_trait::async_trait;
use auth::UserProviderRef;
#[cfg(feature = "apidocs")]
use axem::Extension;
use axum::error_handling::HandleErrorLayer;
use axum::extract::DefaultBodyLimit;
use axum::response::{Html, IntoResponse, Json, Response};
use axum::{middleware, routing, BoxError, Extension, Router};
#[cfg(feature = "apidocs")]
use axum::response::Html;
use axum::response::{IntoResponse, Json, Response};
use axum::{middleware, routing, BoxError, Router};
use common_base::readable_size::ReadableSize;
use common_base::Plugins;
use common_error::status_code::StatusCode;
Expand Down Expand Up @@ -420,6 +427,7 @@ impl IntoResponse for HttpResponse {
}
}

#[cfg(feature = "apidocs")]
impl OperationOutput for HttpResponse {
type Inner = Response;
}
Expand Down Expand Up @@ -466,10 +474,12 @@ impl From<JsonResponse> for HttpResponse {
}
}

#[cfg(feature = "apidocs")]
async fn serve_api(Extension(api): Extension<OpenApi>) -> impl IntoApiResponse {
Json(api)
}

#[cfg(feature = "apidocs")]
async fn serve_docs() -> Html<String> {
Html(include_str!("http/redoc.html").to_owned())
}
Expand All @@ -490,12 +500,14 @@ pub struct HttpServerBuilder {
options: HttpOptions,
plugins: Plugins,
user_provider: Option<UserProviderRef>,
#[cfg(feature = "apidocs")]
api: OpenApi,
router: Router,
}

impl HttpServerBuilder {
pub fn new(options: HttpOptions) -> Self {
#[cfg(feature = "apidocs")]
let api = OpenApi {
info: Info {
title: "GreptimeDB HTTP API".to_string(),
Expand All @@ -513,11 +525,13 @@ impl HttpServerBuilder {
options,
plugins: Plugins::default(),
user_provider: None,
#[cfg(feature = "apidocs")]
api,
router: Router::new(),
}
}

#[cfg(feature = "apidocs")]
pub fn with_sql_handler(
mut self,
sql_handler: ServerSqlQueryHandlerRef,
Expand All @@ -526,9 +540,30 @@ impl HttpServerBuilder {
let sql_router = HttpServer::route_sql(ApiState {
sql_handler,
script_handler,
})
.finish_api(&mut self.api)
.layer(Extension(self.api.clone()));
});

let sql_router = sql_router
.finish_api(&mut self.api)
.layer(Extension(self.api.clone()));

Self {
router: self
.router
.nest(&format!("/{HTTP_API_VERSION}"), sql_router),
..self
}
}

#[cfg(not(feature = "apidocs"))]
pub fn with_sql_handler(
self,
sql_handler: ServerSqlQueryHandlerRef,
script_handler: Option<ScriptHandlerRef>,
) -> Self {
let sql_router = HttpServer::route_sql(ApiState {
sql_handler,
script_handler,
});

Self {
router: self
Expand Down Expand Up @@ -635,11 +670,10 @@ impl HttpServerBuilder {
Self { plugins, ..self }
}

pub fn with_greptime_config_options(mut self, opts: String) -> Self {
pub fn with_greptime_config_options(self, opts: String) -> Self {
let config_router = HttpServer::route_config(GreptimeOptionsConfigState {
greptime_config_options: opts,
})
.finish_api(&mut self.api);
});

Self {
router: self.router.nest("", config_router),
Expand Down Expand Up @@ -791,6 +825,7 @@ impl HttpServer {
.with_state(log_state)
}

#[cfg(feature = "apidocs")]
fn route_sql<S>(api_state: ApiState) -> ApiRouter<S> {
ApiRouter::new()
.api_route(
Expand All @@ -810,6 +845,19 @@ impl HttpServer {
.with_state(api_state)
}

#[cfg(not(feature = "apidocs"))]
fn route_sql<S>(api_state: ApiState) -> Router<S> {
Router::new()
.route("/sql", routing::get(handler::sql).post(handler::sql))
.route(
"/promql",
routing::get(handler::promql).post(handler::promql),
)
.route("/scripts", routing::post(script::scripts))
.route("/run-script", routing::post(script::run_script))
.with_state(api_state)
}

/// Route Prometheus [HTTP API].
///
/// [HTTP API]: https://prometheus.io/docs/prometheus/latest/querying/api/
Expand Down Expand Up @@ -902,9 +950,9 @@ impl HttpServer {
.with_state(otlp_handler)
}

fn route_config<S>(state: GreptimeOptionsConfigState) -> ApiRouter<S> {
ApiRouter::new()
.route("/config", apirouting::get(handler::config))
fn route_config<S>(state: GreptimeOptionsConfigState) -> Router<S> {
Router::new()
.route("/config", routing::get(handler::config))
.with_state(state)
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/servers/src/http/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::collections::HashMap;
use std::sync::Arc;
use std::time::Instant;

#[cfg(feature = "apidocs")]
use aide::transform::TransformOperation;
use axum::extract::{Json, Query, State};
use axum::response::{IntoResponse, Response};
Expand Down Expand Up @@ -277,6 +278,7 @@ pub async fn promql(
.into_response()
}

#[cfg(feature = "apidocs")]
pub(crate) fn sql_docs(op: TransformOperation) -> TransformOperation {
op.response::<200, Json<HttpResponse>>()
}
Expand Down

0 comments on commit 797dfad

Please sign in to comment.