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