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

                                    
 




             
 

                 







                                                                                            
 








                                                                    
                           

                                                      
                                                              
                          


                                                            
                           









                                                             
                                                                   
 

                                                                      
                                                                                        
 
use tower::Layer;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};
use tower_http::{trace::{self, TraceLayer}, normalize_path::NormalizePathLayer};
use tracing::{info, Level};
use axum::{routing::get, Router, ServiceExt, extract::Request};
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 app = NormalizePathLayer::trim_trailing_slash().layer(app);

    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, ServiceExt::<Request>::into_make_service(app)).await.unwrap();
}