implement empty endpoints with openapi
This commit is contained in:
2539
Cargo.lock
generated
Normal file
2539
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
3
README.md
Normal file
3
README.md
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# test_exercise
|
||||||
|
|
||||||
|
Небольшой тестовый репозиторий на раст для ликвидации skill issue
|
||||||
63
src/lib.rs
Normal file
63
src/lib.rs
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
use axum::{debug_handler, extract::Json, response::IntoResponse};
|
||||||
|
use schemas::Patient;
|
||||||
|
use tracing::info;
|
||||||
|
use utoipa::{OpenApi, openapi::Response};
|
||||||
|
use utoipa_axum::{router::OpenApiRouter, routes};
|
||||||
|
|
||||||
|
use crate::openapi::route_docs;
|
||||||
|
|
||||||
|
pub mod models;
|
||||||
|
pub mod openapi;
|
||||||
|
pub mod schemas;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
struct AppState {}
|
||||||
|
|
||||||
|
#[derive(OpenApi)]
|
||||||
|
struct Api;
|
||||||
|
|
||||||
|
pub async fn router() {
|
||||||
|
let addr = "0.0.0.0:3000";
|
||||||
|
tracing_subscriber::fmt::init();
|
||||||
|
let (router, docs) = OpenApiRouter::with_openapi(Api::openapi())
|
||||||
|
.routes(routes!(get_openapi))
|
||||||
|
.routes(routes!(post_patient))
|
||||||
|
.routes(routes!(get_patients))
|
||||||
|
.routes(routes!(update_patient_by_id))
|
||||||
|
.routes(routes!(delete_patient_by_id))
|
||||||
|
.split_for_parts();
|
||||||
|
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
|
||||||
|
info!("Listening in {:?}", listener.local_addr());
|
||||||
|
let router = route_docs(router, docs);
|
||||||
|
axum::serve(listener, router).await.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[debug_handler]
|
||||||
|
#[utoipa::path(get, path = "/api/patients", description="Get all patients", responses((status = OK, body = Patient)))]
|
||||||
|
async fn get_patients() -> impl IntoResponse {
|
||||||
|
axum::http::StatusCode::NOT_IMPLEMENTED
|
||||||
|
}
|
||||||
|
|
||||||
|
#[debug_handler]
|
||||||
|
#[utoipa::path(get, path = "/api/openapi.json", description="Get openapi.json file", responses((status = OK)))]
|
||||||
|
async fn get_openapi()-> impl IntoResponse {
|
||||||
|
axum::http::StatusCode::NOT_IMPLEMENTED
|
||||||
|
}
|
||||||
|
|
||||||
|
#[debug_handler]
|
||||||
|
#[utoipa::path(post, path = "/api/post_patient",description="Create a new patient", responses((status = OK)))]
|
||||||
|
async fn post_patient(Json(patient): Json<Patient>)-> impl IntoResponse {
|
||||||
|
axum::http::StatusCode::NOT_IMPLEMENTED
|
||||||
|
}
|
||||||
|
|
||||||
|
#[debug_handler]
|
||||||
|
#[utoipa::path(post, path = "/api/update_patiend_by_id",description="Update patient by ID", responses((status = OK)))]
|
||||||
|
async fn update_patient_by_id(Json(patient): Json<Patient>)-> impl IntoResponse {
|
||||||
|
axum::http::StatusCode::NOT_IMPLEMENTED
|
||||||
|
}
|
||||||
|
|
||||||
|
#[debug_handler]
|
||||||
|
#[utoipa::path(delete, path = "/api/delete_patient_by_id", description="Delete patient by ID", responses((status = OK)))]
|
||||||
|
async fn delete_patient_by_id(Json(patinent): Json<Patient>)-> impl IntoResponse {
|
||||||
|
axum::http::StatusCode::NOT_IMPLEMENTED
|
||||||
|
}
|
||||||
14
src/models.rs
Normal file
14
src/models.rs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use utoipa::ToSchema;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, ToSchema, Serialize, Deserialize)]
|
||||||
|
struct PatientId {
|
||||||
|
id: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, ToSchema, Serialize, Deserialize)]
|
||||||
|
pub struct Patient {
|
||||||
|
id: PatientId,
|
||||||
|
name: String,
|
||||||
|
diagnosis: String, //todo! enum
|
||||||
|
}
|
||||||
9
src/openapi.rs
Normal file
9
src/openapi.rs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
use axum::Router;
|
||||||
|
use utoipa::openapi::OpenApi;
|
||||||
|
use utoipa_scalar::{Scalar, Servable};
|
||||||
|
|
||||||
|
struct ApiDocs;
|
||||||
|
|
||||||
|
pub fn route_docs(router: Router, docs: OpenApi) -> Router {
|
||||||
|
router.merge(Scalar::with_url("/", docs))
|
||||||
|
}
|
||||||
8
src/schemas.rs
Normal file
8
src/schemas.rs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use utoipa::ToSchema;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, ToSchema, Serialize, Deserialize)]
|
||||||
|
pub struct Patient {
|
||||||
|
name: String,
|
||||||
|
diagnosis: String, //todo! enum
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user