summaryrefslogtreecommitdiffstats
path: root/src/lib.rs
blob: 1ccecf71ad666a776d0b63bd2343143f9fbe180e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::path::PathBuf;

use serde::{Deserialize, Serialize};
use service::Services;
use tower_http::services::ServeDir;

pub use crate::error::{Error, Result};

pub mod api;
pub mod error;
pub mod service;

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase", tag = "status", content = "output")]
pub enum Status {
    #[default]
    Pass,
    Fail(Option<String>),
    Warn(Option<String>),
}

impl<T: std::error::Error> From<T> for Status {
    fn from(value: T) -> Self {
        Status::Fail(Some(value.to_string()))
    }
}

impl axum::response::IntoResponse for Status {
    fn into_response(self) -> axum::response::Response {
        axum::Json(self).into_response()
    }
}

pub fn router(root: PathBuf) -> axum::Router<Services> {
    axum::Router::new()
        .nest_service("/", ServeDir::new(root))
        .nest("/api", api::router())
        .layer(tower_http::trace::TraceLayer::new_for_http())
}