Browse Source

WIP: basic API

develop
chodak166 4 months ago
parent
commit
1d67dab036
  1. 9
      apps/app_api/src/api.rs
  2. 74
      apps/app_api/src/api/health.rs
  3. 2
      apps/app_api/src/main.rs
  4. 16
      apps/app_api/src/router.rs
  5. 25
      apps/app_api/src/router/handlers.rs
  6. 34
      apps/app_api/src/router/responses.rs
  7. 0
      apps/app_api/src/state.rs

9
apps/app_api/src/api.rs

@ -0,0 +1,9 @@
use crate::state::AppState;
use axum::Router;
use std::sync::Arc;
pub mod health;
pub fn routes() -> Router<Arc<AppState>> {
Router::new().nest("/api", health::routes())
}

74
apps/app_api/src/api/health.rs

@ -0,0 +1,74 @@
use axum::{
Json, Router,
extract::State,
http::StatusCode,
response::IntoResponse,
routing::{get, post},
};
use chrono::Utc;
use serde::Serialize;
use serde_json::Value;
use std::sync::Arc;
use crate::state::AppState;
// --- DTOs ---
#[derive(Debug, Serialize)]
pub struct EchoResponse {
pub data: Value,
pub timestamp: String,
}
#[derive(Debug, Serialize)]
pub struct VersionResponse {
pub name: String,
pub version: String,
}
#[derive(Debug, Serialize)]
pub struct ErrorResponse {
pub error: String,
}
impl IntoResponse for ErrorResponse {
fn into_response(self) -> axum::response::Response {
(StatusCode::INTERNAL_SERVER_ERROR, Json(self)).into_response()
}
}
impl<E: std::error::Error> From<E> for ErrorResponse {
fn from(err: E) -> Self {
Self {
error: err.to_string(),
}
}
}
// --- Handlers ---
pub async fn echo_handler(
State(_state): State<Arc<AppState>>,
Json(payload): Json<Value>,
) -> Result<Json<EchoResponse>, ErrorResponse> {
let response = EchoResponse {
data: payload,
timestamp: Utc::now().to_rfc3339(),
};
Ok(Json(response))
}
pub async fn version_handler(State(state): State<Arc<AppState>>) -> Json<VersionResponse> {
Json(VersionResponse {
name: state.name.clone(),
version: state.version.clone(),
})
}
// --- Router ---
pub fn routes() -> Router<Arc<AppState>> {
Router::new()
.route("/echo", post(echo_handler))
.route("/version", get(version_handler))
}

2
apps/app_api/src/main.rs

@ -1,8 +1,10 @@
mod api;
mod app; mod app;
mod commands; mod commands;
mod config; mod config;
mod container; mod container;
mod router; mod router;
mod state;
use anyhow::Result; use anyhow::Result;
use app::Application; use app::Application;

16
apps/app_api/src/router.rs

@ -1,22 +1,14 @@
use axum::{ use axum::Router;
Router,
routing::{get, post},
};
use std::sync::Arc; use std::sync::Arc;
use tower_http::{cors::CorsLayer, trace::TraceLayer}; use tower_http::{cors::CorsLayer, trace::TraceLayer};
mod handlers; use crate::api;
mod responses; use crate::state::AppState;
mod state;
pub use state::AppState;
pub fn create_router() -> Router { pub fn create_router() -> Router {
let state = Arc::new(AppState::new()); let state = Arc::new(AppState::new());
Router::new() api::routes()
.route("/api/echo", post(handlers::echo_handler))
.route("/api/version", get(handlers::version_handler))
.with_state(state) .with_state(state)
.layer(TraceLayer::new_for_http()) .layer(TraceLayer::new_for_http())
.layer(CorsLayer::permissive()) .layer(CorsLayer::permissive())

25
apps/app_api/src/router/handlers.rs

@ -1,25 +0,0 @@
use axum::{Json, extract::State};
use chrono::Utc;
use serde_json::Value;
use std::sync::Arc;
use super::responses::{EchoResponse, ErrorResponse, VersionResponse};
use super::state::AppState;
pub async fn echo_handler(
State(_state): State<Arc<AppState>>,
Json(payload): Json<Value>,
) -> Result<Json<EchoResponse>, ErrorResponse> {
let response = EchoResponse {
data: payload,
timestamp: Utc::now().to_rfc3339(),
};
Ok(Json(response))
}
pub async fn version_handler(State(state): State<Arc<AppState>>) -> Json<VersionResponse> {
Json(VersionResponse {
name: state.name.clone(),
version: state.version.clone(),
})
}

34
apps/app_api/src/router/responses.rs

@ -1,34 +0,0 @@
use axum::{Json, http::StatusCode, response::IntoResponse};
use serde::Serialize;
use serde_json::Value;
#[derive(Debug, Serialize)]
pub struct EchoResponse {
pub data: Value,
pub timestamp: String,
}
#[derive(Debug, Serialize)]
pub struct VersionResponse {
pub name: String,
pub version: String,
}
#[derive(Debug, Serialize)]
pub struct ErrorResponse {
pub error: String,
}
impl IntoResponse for ErrorResponse {
fn into_response(self) -> axum::response::Response {
(StatusCode::INTERNAL_SERVER_ERROR, Json(self)).into_response()
}
}
impl<E: std::error::Error> From<E> for ErrorResponse {
fn from(err: E) -> Self {
Self {
error: err.to_string(),
}
}
}

0
apps/app_api/src/router/state.rs → apps/app_api/src/state.rs

Loading…
Cancel
Save