diff options
author | Gabriel A. Giovanini <mail@gabrielgio.me> | 2022-05-15 16:05:36 +0200 |
---|---|---|
committer | Gabriel A. Giovanini <mail@gabrielgio.me> | 2022-05-15 16:05:36 +0200 |
commit | c434c45380fc6926a5525f97cc4df98a9b3cda94 (patch) | |
tree | 4740520f1ea16f2a5eb2bf62c09c913f3ac21e83 /src/main.rs | |
parent | 231f2cb2205988cf87062bc9f595307af1ed827f (diff) | |
download | macroblog.rs-c434c45380fc6926a5525f97cc4df98a9b3cda94.tar.gz macroblog.rs-c434c45380fc6926a5525f97cc4df98a9b3cda94.tar.bz2 macroblog.rs-c434c45380fc6926a5525f97cc4df98a9b3cda94.zip |
feat: Add actix http server option
The project has two target bin: actix and hyper.
- Actix is a lot more capable, feature complete and can handle more
request with lower response time but has a bigger memory, size
footprint.
- Hyper is simpler less capable mure with a much smaller footprint.
So by default I'll keep using hyper.
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 70 |
1 files changed, 0 insertions, 70 deletions
diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index ed34713..0000000 --- a/src/main.rs +++ /dev/null @@ -1,70 +0,0 @@ - -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 macroblog::router::Router; -use macroblog::blog::{render_index_page, render_post_page}; - - -async fn not_found() -> Result<Response<Body>, Infallible> { - let resp: Response<Body> = Response::builder() - .status(404) - .body("Not Found".into()) - .unwrap(); - Ok(resp) -} - - -async fn index() -> Result<Response<Body>, Infallible> { - let body = render_index_page(); - - let resp: Response<Body> = Response::builder() - .status(200) - .header("posts-type", "text/html") - .body(body.into()) - .unwrap(); - - Ok(resp) -} - - -async fn post(path: &String) -> Result<Response<Body>, Infallible> { - let body = render_post_page(path); - - let resp: Response<Body> = Response::builder() - .status(200) - .header("posts-type", "text/html") - .body(body.into()) - .unwrap(); - - Ok(resp) -} - -async fn request(req: Request<Body>) -> Result<Response<Body>, Infallible> { - let path = req.uri().path(); - - match Router::new(path) { - Router::Index => index().await, - Router::Post { page } => post(&page).await, - Router::NotFound => not_found().await - } -} - - -#[tokio::main] -async fn main() { - let port = env::var("PORT").unwrap_or("3000".into()).parse::<u16>().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 server = Server::bind(&addr).serve(make_svc); - - if let Err(e) = server.await { - eprintln!("server error: {}", e); - } -} |