summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: e24721125a47d40e99b1bd14b7816cd4173c6da7 (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
60
61
62
63
#[macro_use]
extern crate tracing;

use color_eyre::eyre::Result;
use std::{net::IpAddr, sync::Arc};
use warp::{Filter, path};

use std::str::FromStr;

pub mod blog;
pub mod misc;
mod internal;

use internal::SiteState;

#[tokio::main]
async fn main() -> Result<()> {
    color_eyre::install()?;
    tracing_subscriber::fmt::init();
    info!("Starting launch of {}", env!("GIT_SHA"));


    // Load .env
    kankyo::init();

    let state = Arc::new(internal::init().await?);

    let index = warp::get().and(path::end().and_then(misc::handlers::index));

    let blog_base = warp::path!("blog" / ..);
    let blog_list = blog_base.and(give_site_state(state.clone())).and_then(blog::handlers::list);
    let blog_post = blog_base.and(
        warp::path!(String)
            .and(give_site_state(state.clone())) 
            .and_then(blog::handlers::post),
    );

    let static_files = warp::path("static")
        .and(warp::fs::dir("./statics"));
    let site = index
        .or(blog_list.or(blog_post))
        .or(static_files).with(warp::log("site"));


    let server = warp::serve(site);

    server
        .run((
            IpAddr::from_str(&std::env::var("HOST").unwrap_or("127.0.0.1".into()))?,
            std::env::var("PORT")
                .unwrap_or("3030".into())
                .parse::<u16>()?,
        ))
        .await;

    Ok(())
}

fn give_site_state(sitestate: Arc<SiteState>) -> impl Filter<Extract = (Arc<SiteState>,), Error=std::convert::Infallible> + Clone {
    warp::any().map(move || sitestate.clone())
}

include!(concat!(env!("OUT_DIR"), "/templates.rs"));