changed backedn framwork from axum to actix and also added openapi and swagger ui

This commit is contained in:
2026-02-04 22:55:19 +01:00
parent 9b093e6739
commit e135a34d4c
11 changed files with 1716 additions and 91 deletions

View File

@@ -0,0 +1,56 @@
use std::{fmt::Display};
use actix_web::{ResponseError, web::{Data, Json}};
use apistos::{ApiComponent, ApiErrorComponent, actix::CreatedJson};
use chrono::{DateTime, Local};
use schemars::JsonSchema;
use serde::{Serialize, Deserialize};
use crate::AppState;
#[derive(Debug, Deserialize, JsonSchema, ApiComponent)]
pub struct RequestBody {
time: DateTime<Local>,
}
#[derive(Debug, Serialize, JsonSchema, ApiComponent)]
pub struct SuccessResponse {
pub time: DateTime<Local>,
pub enabled: bool
}
#[derive(Debug, Deserialize, Serialize, ApiErrorComponent)]
#[openapi_error(status(code = 500, description = "The alarm could not be created"))]
pub enum ErrorResponse {
InternalError(String)
}
impl Display for ErrorResponse {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ErrorResponse::InternalError(e) => write!(f, "Internal error: {}", e),
}
}
}
impl ResponseError for ErrorResponse {
fn status_code(&self) -> actix_web::http::StatusCode {
match self {
ErrorResponse::InternalError(_) => actix_web::http::StatusCode::INTERNAL_SERVER_ERROR,
}
}
}
#[apistos::api_operation(
summary = "Add new alarm",
description = r###"Creates new Alarm"###,
error_code= 500,
)]
pub async fn post(data: Data<AppState>, body: Json<RequestBody>) -> Result<CreatedJson<SuccessResponse>, ErrorResponse> {
let result = data.scheduler.add_alarm(body.time);
match result {
Ok(alarm) => Ok(CreatedJson(SuccessResponse { time: alarm.time, enabled: alarm.enabled })),
Err(e) => Err(ErrorResponse::InternalError(e)),
}
}