summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 8643d7132461f989db2dee90f5b0ccba3cdd46dd (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
mod config;
mod errors;
mod graphql;
mod logger;
mod routes;
use std::{net::SocketAddr, time::Duration};

use crate::config::CONFIG;
use axum::{
    http::{header, Request},
    Router,
};
use tokio::net::TcpListener;
use tower_http::{
    classify::ServerErrorsFailureClass, sensitive_headers::SetSensitiveHeadersLayer,
    trace::TraceLayer,
};

use tracing::Span;

/// Create the app: setup everything and returns a `Router`
async fn create_app() -> Router {
    logger::setup();
    // let _ = db::setup().await;

    Router::new()
        .nest("/graphql", graphql::create_route())
        .fallback(crate::routes::page_404)
        // Mark the `Authorization` request header as sensitive so it doesn't
        // show in logs.
        .layer(SetSensitiveHeadersLayer::new(std::iter::once(
            header::AUTHORIZATION,
        )))
        // Use a layer for `TraceLayer`
        .layer(
            TraceLayer::new_for_http()
                .on_request(|request: &Request<_>, _span: &Span| {
                    tracing::info!("{} {}", request.method(), request.uri());
                })
                .on_failure(
                    |error: ServerErrorsFailureClass, latency: Duration, _span: &Span| {
                        tracing::error!("{} | {} s", error, latency.as_secs());
                    },
                ),
        )
}

#[tokio::main(flavor = "current_thread")]
async fn main() {
    let app = create_app().await;

    let host = &CONFIG.allowed_host;

    let addr = match host.parse::<SocketAddr>() {
        Ok(addr) => addr,
        Err(e) => {
            panic!("`{}` {}", host, e);
        }
    };
    tracing::info!("Listening on {}", addr);

    axum::serve(TcpListener::bind(&addr).await.unwrap(), app)
        .await
        .unwrap();
}