aboutsummaryrefslogtreecommitdiff
path: root/src/bin/actix.rs
blob: 978e8ed20001affa0cb82bd096ad50e8dc651828 (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
use actix_web::{get, web, middleware, App, HttpResponse, HttpServer, Responder, http::header::ContentType};
use macroblog::blog::{render_index_page, render_post_page, render_projects};
use macroblog::router::blog_post_exists;
use std::env;

#[get("/")]
async fn index() -> impl Responder {
    let body = render_index_page();

    HttpResponse::Ok()
        .content_type(ContentType::html())
        .body(body)
}

#[get("/projects")]
async fn projects() -> impl Responder {
    let body = render_projects();

    HttpResponse::Ok()
        .content_type(ContentType::html())
        .body(body)
}


#[get("/posts/{name}")]
async fn posts(name: web::Path<String>) -> impl Responder {

    if !blog_post_exists(&name) {
        return HttpResponse::NotFound()
            .body("Not Found".to_string());
    }

    let body = render_post_page(&name);

    HttpResponse::Ok()
        .content_type(ContentType::html())
        .body(body)
}


#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let port = env::var("PORT").unwrap_or("3000".into()).parse::<u16>().unwrap_or(3000);
    HttpServer::new(|| {
        App::new()
            .wrap(middleware::Compress::default())
            .service(index)
            .service(projects)
            .service(posts)
    })
    .bind(("0.0.0.0", port))?
    .run()
    .await
}