From 6a31a30b98f7febe9ac0db74211ef074aefc7ad3 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Sat, 11 Jun 2022 18:15:38 +0200 Subject: feat: Add project tab --- src/bin/actix.rs | 12 +++++++++++- src/bin/hyper.rs | 36 +++++++++++++++++++++++------------- 2 files changed, 34 insertions(+), 14 deletions(-) (limited to 'src/bin') diff --git a/src/bin/actix.rs b/src/bin/actix.rs index 101fe2e..978e8ed 100644 --- a/src/bin/actix.rs +++ b/src/bin/actix.rs @@ -1,5 +1,5 @@ use actix_web::{get, web, middleware, App, HttpResponse, HttpServer, Responder, http::header::ContentType}; -use macroblog::blog::{render_index_page, render_post_page}; +use macroblog::blog::{render_index_page, render_post_page, render_projects}; use macroblog::router::blog_post_exists; use std::env; @@ -12,6 +12,15 @@ async fn index() -> impl Responder { .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) -> impl Responder { @@ -36,6 +45,7 @@ async fn main() -> std::io::Result<()> { App::new() .wrap(middleware::Compress::default()) .service(index) + .service(projects) .service(posts) }) .bind(("0.0.0.0", port))? diff --git a/src/bin/hyper.rs b/src/bin/hyper.rs index 3f23f18..f24cbe4 100644 --- a/src/bin/hyper.rs +++ b/src/bin/hyper.rs @@ -1,11 +1,10 @@ -use std::convert::Infallible; -use std::{env}; -use std::net::SocketAddr; -use hyper::{Body, Request, Response, Server}; use hyper::service::{make_service_fn, service_fn}; +use hyper::{Body, Request, Response, Server}; +use macroblog::blog::{render_index_page, render_post_page, render_projects}; use macroblog::router::Router; -use macroblog::blog::{render_index_page, render_post_page}; - +use std::convert::Infallible; +use std::env; +use std::net::SocketAddr; async fn not_found() -> Result, Infallible> { let resp: Response = Response::builder() @@ -15,7 +14,6 @@ async fn not_found() -> Result, Infallible> { Ok(resp) } - async fn index() -> Result, Infallible> { let body = render_index_page(); @@ -28,6 +26,17 @@ async fn index() -> Result, Infallible> { Ok(resp) } +async fn projects() -> Result, Infallible> { + let body = render_projects(); + + let resp: Response = Response::builder() + .status(200) + .header("posts-type", "text/html") + .body(body.into()) + .unwrap(); + + Ok(resp) +} async fn post(path: &String) -> Result, Infallible> { let body = render_post_page(path); @@ -46,20 +55,21 @@ async fn request(req: Request) -> Result, Infallible> { match Router::new(path) { Router::Index => index().await, + Router::Projects => projects().await, Router::Post { page } => post(&page).await, - Router::NotFound => not_found().await + Router::NotFound => not_found().await, } } - #[tokio::main] async fn main() { - let port = env::var("PORT").unwrap_or("3000".into()).parse::().unwrap_or(3000); + let port = env::var("PORT") + .unwrap_or("3000".into()) + .parse::() + .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 make_svc = make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(request)) }); let server = Server::bind(&addr).serve(make_svc); -- cgit v1.2.3