Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finished: Part IV: Basic Server & log tracing
- Loading branch information
1 parent
51120a3
commit 7526728
Showing
6 changed files
with
73 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
mod router; | ||
mod serve; | ||
|
||
pub use serve::listen; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use hyper::{Body, Request, Response, StatusCode}; | ||
use routerify::{ext::RequestExt, Error, Middleware, RequestInfo, Router}; | ||
use tracing::{error, info}; | ||
|
||
async fn logger(req: Request<Body>) -> Result<Request<Body>, Error> { | ||
info!( | ||
"{} {} {}", | ||
req.remote_addr(), | ||
req.method(), | ||
req.uri().path() | ||
); | ||
Ok(req) | ||
} | ||
|
||
async fn error_handler(err: routerify::RouteError, _: RequestInfo) -> Response<Body> { | ||
error!("{}", err); | ||
Response::builder() | ||
.status(StatusCode::INTERNAL_SERVER_ERROR) | ||
.body(Body::from(format!("Something went wrong: {}", err))) | ||
.unwrap() | ||
} | ||
|
||
async fn home_handler(_: Request<Body>) -> Result<Response<Body>, Error> { | ||
Ok(Response::new(Body::from("Url Mapper in Rust!"))) | ||
} | ||
|
||
pub fn router() -> Router<Body, Error> { | ||
Router::builder() | ||
.middleware(Middleware::pre(logger)) | ||
.get("/", home_handler) | ||
.err_handler_with_info(error_handler) | ||
.build() | ||
.unwrap() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use crate::server::router::router; | ||
use crate::CONFIG; | ||
use anyhow::Result; | ||
use hyper::Server; | ||
use routerify::RouterService; | ||
use tracing::info; | ||
|
||
pub async fn listen() -> Result<()> { | ||
let router = router(); | ||
let service = RouterService::new(router).unwrap(); | ||
let address = format!("{}:{}", CONFIG.host, CONFIG.port).parse()?; | ||
|
||
let server = Server::bind(&address).serve(service); | ||
info!("Server started listening on {}", address); | ||
server.await?; | ||
Ok(()) | ||
} |