aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 63a5386ecff2059e8fefcbbcce7d74f4a38c2124 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
pub mod router;

use std::convert::Infallible;
use rust_embed::RustEmbed;
use std::{env, str};
use std::net::SocketAddr;
use hyper::{Body, Request, Response, Server};
use hyper::service::{make_service_fn, service_fn};
use sailfish::TemplateOnce;
use ::router::Router;

struct PostEntry {
    title: String,
    file: String,
}

#[derive(TemplateOnce)]
#[template(path = "index.html")]
struct IndexTemplate {
    posts: Vec<PostEntry>,
}

#[derive(TemplateOnce)]
#[template(path = "post.html")]
struct PostTemplate {
    content: String,
}

#[derive(RustEmbed)]
#[folder = "content/posts/"]
struct PostAsset;


fn get_file_content(path: &str) -> String {
    let buffer = PostAsset::get(path)
        .unwrap()
        .data
        .into_owned();

    return String::from_utf8(buffer).unwrap();
}

fn get_post_entry(path: &String) -> PostEntry {
    let sub_title = str::replace(path, "_", " ");
    let title = str::replace(sub_title.as_str(), ".html", "");
    PostEntry {
        title: String::from(title),
        file: String::from(path),
    }
}

fn get_post_title() -> Vec<PostEntry> {
    PostAsset::iter()
        .map(|e| format!("{}", e))
        .map(|e| get_post_entry(&e))
        .collect()
}


async fn not_found() -> Result<Response<Body>, Infallible> {
    let resp: Response<Body> = Response::builder()
        .status(404)
        .body("Not Found".into())
        .unwrap();
    Ok(resp)
}


async fn index() -> Result<Response<Body>, Infallible> {
    let files = get_post_title();
    let body = IndexTemplate { posts: files }
        .render_once()
        .unwrap();

    let resp: Response<Body> = Response::builder()
        .status(200)
        .header("posts-type", "text/html")
        .body(body.into())
        .unwrap();

    Ok(resp)
}


async fn post(path: &String) -> Result<Response<Body>, Infallible> {
    let body = PostTemplate { content: get_file_content(path) }
        .render_once()
        .unwrap();

    let resp: Response<Body> = Response::builder()
        .status(200)
        .header("posts-type", "text/html")
        .body(body.into())
        .unwrap();

    Ok(resp)
}

async fn request(req: Request<Body>) -> Result<Response<Body>, Infallible> {
    let path = req.uri().path();

    match Router::new(path) {
        Router::Index => index().await,
        Router::Post { page } => post(&page).await,
        Router::NotFound => not_found().await
    }
}


#[tokio::main]
async fn main() {
    let port = env::var("PORT").unwrap_or("3000".into()).parse::<u16>().unwrap_or(3000);
    let addr = SocketAddr::from(([0, 0, 0, 0], port));

    let make_svc = make_service_fn(|_conn| async {
        Ok::<_, Infallible>(service_fn(request))
    });

    let server = Server::bind(&addr).serve(make_svc);

    if let Err(e) = server.await {
        eprintln!("server error: {}", e);
    }
}