aboutsummaryrefslogblamecommitdiff
path: root/src/main.rs
blob: ba319d98dc1628d26c462abacc8ffb7b877f0c6e (plain) (tree)
1
2
3
4
5
6
7
8
9








                                     
                                                         





                                      



             

                 

                                   









                                                                           
                                                           















                                                                           
                                                                                                  










                                            
use axum::{
    error_handling::HandleErrorLayer,
    http::StatusCode,
    response::IntoResponse,
    routing::get,
    Json, Router
};

use serde::{Deserialize, Serialize};
use std::{net::SocketAddr, time::Duration, str::FromStr};

use tower::{BoxError, ServiceBuilder};
use tower_http::trace::TraceLayer;

use tracing_subscriber::prelude::*;

mod errors;

mod handlers;

#[tokio::main]
async fn main() {
    kankyo::init();
    color_eyre::install().unwrap();
        tracing_subscriber::registry()
        .with(tracing_subscriber::EnvFilter::new(
            std::env::var("RUST_LOG")
                .unwrap_or_else(|_| "waifud=info,tower_http=debug".into()),
        ))
        .with(tracing_subscriber::fmt::layer())
        .init();

    let app = Router::new()
        .route("/health", get(health_check))
        .route("/waifus/list", get(handlers::waifus::list))
        .layer(            ServiceBuilder::new()
                .layer(HandleErrorLayer::new(|error: BoxError| async move {
                    if error.is::<tower::timeout::error::Elapsed>() {
                        Ok(StatusCode::REQUEST_TIMEOUT)
                    } else {
                        Err((
                            StatusCode::INTERNAL_SERVER_ERROR,
                            format!("Unhandled internal error: {}", error),
                        ))
                    }
                }))
                .timeout(Duration::from_secs(10))
                .layer(TraceLayer::new_for_http())
                .into_inner(),
        );

    let addr = SocketAddr::from_str(std::env::var("BIND_ADDR").unwrap().as_str().into()).unwrap();
    tracing::info!("Listening on {}", addr);

    axum::Server::bind(&addr)
        .serve(app.into_make_service())
        .await
        .unwrap();
}

async fn health_check() -> &'static str {
    "OK"
}