summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 81c4427ed791c93061d1382171d66083eeb7bae0 (plain) (blame)
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
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};
use tower_http::trace::{self, TraceLayer};
use tracing::{info, Level};
use axum::{routing::get, Router};
use tutors::fs_tutor_repo::FsTutorRepo;
use std::{sync::Arc, env};
use tower_http::services::ServeDir;
use posts::fs_post_repo::FsPostRepo;

mod helpers;
mod posts;
mod tutors;
mod views;
mod handlers;

#[tokio::main]
async fn main() {
    let tracing_filter = EnvFilter::builder()
        .with_env_var("CT_LOG")
        .try_from_env()
        .unwrap_or("carpentertutoring=debug,tower_http=debug,axum::rejection=trace".into());
    tracing_subscriber::registry()
        .with(tracing_filter)
        .with(tracing_subscriber::fmt::layer())
        .init();

    info!("loading state...");
    let blog_dir = env::var("CT_POSTS").unwrap();
    let tutor_dir = env::var("CT_TEAM").unwrap();
    let assets_dir = env::var("CT_ASSETS").unwrap();

    let posts = Arc::new(FsPostRepo::with_dir(blog_dir));
    let tutors = Arc::new(FsTutorRepo::with_dir(tutor_dir.clone()));

    info!("initializing router...");
    let app = Router::new()
        .route("/", get(handlers::index_handler))
        .route("/posts", get(handlers::posts_handler))
        .route("/posts/:post_id", get(handlers::post_handler))
        .with_state(posts)
        .route("/policies", get(handlers::policies_handler))
        .route("/brochure", get(handlers::brochure_handler))
        .route("/about", get(handlers::about_handler))
        .with_state(tutors)
        .nest_service("/assets", ServeDir::new(assets_dir))
        .nest_service("/team", ServeDir::new(tutor_dir))
        .fallback_service(ServeDir::new("static"))
        .layer(
            TraceLayer::new_for_http()
                .make_span_with(trace::DefaultMakeSpan::new()
                    .level(Level::INFO))
                .on_response(trace::DefaultOnResponse::new()
                    .level(Level::INFO))
        );

    let addr = env::var("CT_BIND").unwrap_or("0.0.0.0:8000".into());
    let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
    axum::serve(listener, app).await.unwrap();
}