macroblog.rs @ 0e147a780e74b54afbd56ff7438077d855d5c1c2

 1use actix_web::{get, web, middleware, App, HttpResponse, HttpServer, Responder, http::header::ContentType};
 2use macroblog::blog::{render_index_page, render_post_page};
 3use std::{env};
 4
 5#[get("/")]
 6async fn index() -> impl Responder {
 7    let body = render_index_page();
 8
 9    HttpResponse::Ok()
10        .content_type(ContentType::html())
11        .body(body)
12}
13
14
15#[get("/posts/{name}")]
16async fn posts(name: web::Path<String>) -> impl Responder {
17    let body = render_post_page(&name);
18
19    HttpResponse::Ok()
20        .content_type(ContentType::html())
21        .body(body)
22}
23
24
25#[actix_web::main]
26async fn main() -> std::io::Result<()> {
27    let port = env::var("PORT").unwrap_or("3000".into()).parse::<u16>().unwrap_or(3000);
28    HttpServer::new(|| {
29        App::new()
30            .wrap(middleware::Compress::default())
31            .service(index)
32            .service(posts)
33    })
34    .bind(("0.0.0.0", port))?
35    .run()
36    .await
37}