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/bin/actix.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/bin/actix.rs')
-rw-r--r-- | src/bin/actix.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/bin/actix.rs b/src/bin/actix.rs new file mode 100644 index 0000000..dd03ece --- /dev/null +++ b/src/bin/actix.rs @@ -0,0 +1,34 @@ +use actix_web::{get, web, App, HttpResponse, HttpServer, Responder, http::header::ContentType}; +use macroblog::blog::{render_index_page, render_post_page}; + +#[get("/")] +async fn index() -> impl Responder { + let body = render_index_page(); + + HttpResponse::Ok() + .content_type(ContentType::html()) + .body(body) +} + + +#[get("/posts/{name}")] +async fn posts(name: web::Path<String>) -> impl Responder { + let body = render_post_page(&name); + + HttpResponse::Ok() + .content_type(ContentType::html()) + .body(body) +} + + +#[actix_web::main] +async fn main() -> std::io::Result<()> { + HttpServer::new(|| { + App::new() + .service(index) + .service(posts) + }) + .bind(("0.0.0.0", 3000))? + .run() + .await +} |