78 lines
1.4 KiB
Rust
78 lines
1.4 KiB
Rust
use axum::{
|
|
Router,
|
|
routing::{get, post},
|
|
};
|
|
use std::sync::Arc;
|
|
use utoipa::OpenApi;
|
|
use utoipa_swagger_ui::SwaggerUi;
|
|
|
|
use crate::Datasets;
|
|
|
|
mod lookup;
|
|
pub use lookup::*;
|
|
|
|
mod item;
|
|
pub use item::*;
|
|
|
|
mod extract;
|
|
pub use extract::*;
|
|
|
|
mod field;
|
|
pub use field::*;
|
|
|
|
mod items;
|
|
pub use items::*;
|
|
|
|
#[derive(OpenApi)]
|
|
#[openapi(
|
|
tags(),
|
|
paths(lookup, item_get, get_extract, items_list, get_field),
|
|
components(schemas(
|
|
LookupRequest,
|
|
LookupResponse,
|
|
LookupResult,
|
|
ItemQuery,
|
|
ExtractQuery,
|
|
FieldQuery,
|
|
ItemsQuery,
|
|
ItemsResponse,
|
|
ItemRef
|
|
))
|
|
)]
|
|
pub(crate) struct Api;
|
|
|
|
impl Datasets {
|
|
#[inline]
|
|
pub fn router(self: Arc<Self>, with_docs: bool) -> Router<()> {
|
|
self.router_prefix(with_docs, None)
|
|
}
|
|
|
|
#[inline]
|
|
pub fn router_prefix(self: Arc<Self>, with_docs: bool, prefix: Option<&str>) -> Router<()> {
|
|
let mut router = Router::new()
|
|
.route("/lookup", post(lookup))
|
|
.route("/item", get(item_get))
|
|
.route("/extract", get(get_extract))
|
|
.route("/field", get(get_field))
|
|
.route("/items", get(items_list))
|
|
.with_state(self.clone());
|
|
|
|
if let Some(prefix) = prefix {
|
|
router = Router::new().nest(prefix, router);
|
|
}
|
|
|
|
if with_docs {
|
|
let docs_path = match prefix {
|
|
None => "/docs".into(),
|
|
Some(prefix) => format!("{prefix}/docs"),
|
|
};
|
|
|
|
let docs = SwaggerUi::new(docs_path.clone())
|
|
.url(format!("{}/openapi.json", docs_path), Api::openapi());
|
|
|
|
router = router.merge(docs);
|
|
}
|
|
router
|
|
}
|
|
}
|