Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add OpenAPI/Swagger-ui #487

Merged
merged 1 commit into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ unexpected_cfgs = { level = "warn", check-cfg = ['cfg(nightly)'] }

[features]

default = ["resolve-cli", "dns-over-tls", "dns-over-https", "dns-over-quic", "dns-over-h3", "dnssec", "service", "nft", "nom-recipes-all", "self-update" ]
default = ["common", "self-update" ]

homebrew = ["resolve-cli", "dns-over-tls", "dns-over-https", "dns-over-quic", "dns-over-h3", "dnssec", "service", "nft", "nom-recipes-all" ]
homebrew = [ "common" ]

common = ["resolve-cli", "dns-over-tls", "dns-over-https", "dns-over-quic", "dns-over-h3", "dnssec", "service", "nft", "nom-recipes-all", "swagger-ui-cdn" ]

nom-recipes-all =["nom-recipes-ip"]

Expand Down Expand Up @@ -75,6 +77,13 @@ nft = ["dep:which", "dep:either"]

dnssec = [ "hickory-proto/dnssec-ring", "rustls/ring"]


swagger-ui-cdn = []

swagger-ui-embed = [
"dep:utoipa-swagger-ui"
]

experimental = ["experimental-trie", "experimental-phf"]

experimental-trie = []
Expand Down Expand Up @@ -114,6 +123,9 @@ axum = { version = "0.8.1" }
hyper = { version = "1.1.0", default-features = false }
hyper-util = { version = "0.1.3", features = ["http2"]}
tower = { version = "0.5.2", default-features = false }
utoipa = { git = "https://github.com/mokeyish/utoipa.git", rev = "smartdns.1", package = "utoipa", features = ["axum_extras"] }
utoipa-axum = { git = "https://github.com/mokeyish/utoipa.git", rev = "smartdns.1", package = "utoipa-axum", features = []}
utoipa-swagger-ui = { git = "https://github.com/mokeyish/utoipa.git", rev = "smartdns.1", package = "utoipa-swagger-ui", optional = true, default-features = false, features = ["axum"] }

# serde
serde = { version = "1.0", features = ["derive"]}
Expand Down
7 changes: 4 additions & 3 deletions src/api/address.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::sync::Arc;

use axum::{extract::State, response::IntoResponse, routing::get, Json, Router};

use super::openapi::{http::get, routes, IntoRouter};
use super::{IntoDataListPayload, ServeState, StatefulRouter};
use axum::{extract::State, response::IntoResponse, Json};

pub fn routes() -> StatefulRouter {
Router::new().route("/addresses", get(addresses))
routes![addresses].into_router()
}

#[get("/addresses")]
async fn addresses(State(state): State<Arc<ServeState>>) -> impl IntoResponse {
Json(
state
Expand Down
6 changes: 4 additions & 2 deletions src/api/audit.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use std::sync::Arc;

use axum::{extract::State, response::IntoResponse, routing::get, Json, Router};
use super::openapi::{http::get, routes, IntoRouter};
use axum::{extract::State, response::IntoResponse, Json};

use super::{ServeState, StatefulRouter};

pub fn routes() -> StatefulRouter {
Router::new().route("/audits/config", get(audit_config))
routes![audit_config,].into_router()
}

#[get("/audits/config")]
async fn audit_config(State(state): State<Arc<ServeState>>) -> impl IntoResponse {
Json(state.app.cfg().await.audit_config()).into_response()
}
25 changes: 14 additions & 11 deletions src/api/cache.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
use std::sync::Arc;

use axum::{
extract::State,
http::StatusCode,
response::IntoResponse,
routing::{get, post},
Json, Router,
use super::openapi::{
http::{get, post},
routes, IntoRouter,
};

use super::{IntoDataListPayload, ServeState, StatefulRouter};
use crate::log;
use axum::{extract::State, http::StatusCode, response::IntoResponse, Json};

pub fn routes() -> StatefulRouter {
Router::new()
.route("/caches", get(caches))
.route("/caches/config", get(cache_config))
.route("/caches/flush", post(flush_cache))
let route1 = routes![flush_cache, caches].into_router();
let route2 = routes![cache_config].into_router();
route1.merge(route2)

// routes![flush_cache, caches, cache_config].into_router()
}

#[get("/caches")]
async fn caches(State(state): State<Arc<ServeState>>) -> impl IntoResponse {
Json(
(if let Some(c) = state.app.cache().await {
Expand All @@ -28,13 +28,16 @@ async fn caches(State(state): State<Arc<ServeState>>) -> impl IntoResponse {
)
}

#[post("/caches/flush")]
async fn flush_cache(State(state): State<Arc<ServeState>>) -> StatusCode {
if let Some(c) = state.app.cache().await {
c.clear().await;
}
log::info!("flushed cache");
StatusCode::NO_CONTENT
}

#[get("/caches/config")]
async fn cache_config(State(state): State<Arc<ServeState>>) -> impl IntoResponse {
Json(state.app.cfg().await.cache_config()).into_response()
}
6 changes: 4 additions & 2 deletions src/api/forward.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use std::sync::Arc;

use axum::{extract::State, response::IntoResponse, routing::get, Json, Router};
use axum::{extract::State, response::IntoResponse, Json};

use super::openapi::{http::get, routes, IntoRouter};
use super::{IntoDataListPayload, ServeState, StatefulRouter};

pub fn routes() -> StatefulRouter {
Router::new().route("/forwards", get(forwards))
routes![forwards].into_router()
}

#[get("/forwards")]
async fn forwards(State(state): State<Arc<ServeState>>) -> impl IntoResponse {
Json(
state
Expand Down
6 changes: 4 additions & 2 deletions src/api/listener.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use std::sync::Arc;

use axum::{extract::State, response::IntoResponse, routing::get, Json, Router};
use axum::{extract::State, response::IntoResponse, Json};

use super::openapi::{http::get, routes, IntoRouter};
use super::{IntoDataListPayload, ServeState, StatefulRouter};

pub fn routes() -> StatefulRouter {
Router::new().route("/listeners", get(listeners))
routes![listeners].into_router()
}

#[get("/listeners")]
async fn listeners(State(state): State<Arc<ServeState>>) -> impl IntoResponse {
Json(
state
Expand Down
7 changes: 4 additions & 3 deletions src/api/log.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::sync::Arc;

use axum::{extract::State, response::IntoResponse, routing::get, Json, Router};

use super::openapi::{http::get, routes, IntoRouter};
use super::{ServeState, StatefulRouter};
use axum::{extract::State, response::IntoResponse, Json};

pub fn routes() -> StatefulRouter {
Router::new().route("/logs/config", get(log_config))
routes![log_config].into_router()
}

#[get("/logs/config")]
async fn log_config(State(state): State<Arc<ServeState>>) -> impl IntoResponse {
Json(state.app.cfg().await.log_config()).into_response()
}
Loading
Loading