1use actix_web::{get, web, middleware, App, HttpResponse, HttpServer, Responder, http::header::ContentType};
2use macroblog::blog::{render_index_page, render_post_page, render_projects};
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#[get("/projects")]
16async fn projects() -> impl Responder {
17 let body = render_projects();
18
19 HttpResponse::Ok()
20 .content_type(ContentType::html())
21 .body(body)
22}
23
24
25#[get("/posts/{name}")]
26async fn posts(name: web::Path<String>) -> impl Responder {
27
28 if !blog_post_exists(&name) {
29 return HttpResponse::NotFound()
30 .body("Not Found".to_string());
31 }
32
33 let body = render_post_page(&name);
34
35 HttpResponse::Ok()
36 .content_type(ContentType::html())
37 .body(body)
38}
39
40
41#[actix_web::main]
42async fn main() -> std::io::Result<()> {
43 let port = env::var("PORT").unwrap_or("3000".into()).parse::<u16>().unwrap_or(3000);
44 HttpServer::new(|| {
45 App::new()
46 .wrap(middleware::Compress::default())
47 .service(index)
48 .service(projects)
49 .service(posts)
50 })
51 .bind(("0.0.0.0", port))?
52 .run()
53 .await
54}