Skip to content

Commit

Permalink
fix #817 add 404 handle for actix 4 example & update readme for (#818)
Browse files Browse the repository at this point in the history
* feat: add global 404 page

* docs: add cargo-watch install on README file
  • Loading branch information
xiaoquisme authored Jun 26, 2022
1 parent 8faabf4 commit 987c6b5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/actix_example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
Run server with auto-reloading:

```bash
cargo install systemfd
cargo install systemfd cargo-watch
systemfd --no-pid -s http::8000 -- cargo watch -x run
```
12 changes: 12 additions & 0 deletions examples/actix_example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,17 @@ async fn delete(data: web::Data<AppState>, id: web::Path<i32>) -> Result<HttpRes
.finish())
}

async fn not_found(data: web::Data<AppState>, request: HttpRequest) -> Result<HttpResponse, Error> {
let mut ctx = tera::Context::new();
ctx.insert("uri", request.uri().path());

let template = &data.templates;
let body = template.render("error/404.html.tera", &ctx)
.map_err(|_| error::ErrorInternalServerError("Template error"))?;

Ok(HttpResponse::Ok().content_type("text/html").body(body))
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "debug");
Expand Down Expand Up @@ -186,6 +197,7 @@ async fn main() -> std::io::Result<()> {
.service(Fs::new("/static", "./static"))
.app_data(web::Data::new(state.clone()))
.wrap(middleware::Logger::default()) // enable logger
.default_service(web::route().to(not_found))
.configure(init)
});

Expand Down

0 comments on commit 987c6b5

Please sign in to comment.