aboutsummaryrefslogtreecommitdiff
path: root/src/bin
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin')
-rw-r--r--src/bin/actix.rs34
-rw-r--r--src/bin/hyper.rs69
2 files changed, 103 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
+}
diff --git a/src/bin/hyper.rs b/src/bin/hyper.rs
new file mode 100644
index 0000000..3f23f18
--- /dev/null
+++ b/src/bin/hyper.rs
@@ -0,0 +1,69 @@
+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);
+ }
+}