Abolish middleware and use the catch method

This commit is contained in:
Tim-Paik 2021-08-10 01:09:34 +08:00
parent 5d0241a387
commit 24dc99efc0
3 changed files with 18 additions and 35 deletions

4
Cargo.lock generated
View File

@ -369,9 +369,9 @@ dependencies = [
[[package]]
name = "filetime"
version = "0.2.14"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d34cfa13a63ae058bfa601fe9e313bbdb3746427c1459185464ce0fcf62e1e8"
checksum = "975ccf83d8d9d0d84682850a38c8169027be83368805971cc4f238c2b245bc98"
dependencies = [
"cfg-if 1.0.0",
"libc",

View File

@ -2,15 +2,14 @@
extern crate clap;
#[macro_use]
extern crate rocket;
/*
#[macro_use]
extern crate rocket_dyn_templates;
*/
use colored::*;
use rocket::fairing::{Fairing, Info, Kind};
use rocket::http::Status;
use rocket::response::status;
use rocket::{config::TlsConfig, fs::NamedFile};
use rocket_dyn_templates::Template;
use std::collections::HashMap;
use std::net::IpAddr;
use std::path::Path;
use std::str::FromStr;
@ -31,35 +30,20 @@ struct IndexContext<'r> {
}
#[catch(404)]
fn not_found() {}
struct Index {}
#[rocket::async_trait]
impl Fairing for Index {
fn info(&self) -> Info {
Info {
name: "Index",
kind: Kind::Response,
}
}
async fn on_response<'r>(
&self,
request: &'r rocket::Request<'_>,
response: &mut rocket::Response<'r>,
) {
if response.status().code != 404
|| !Path::new(
&(std::env::var("ROOT").unwrap().to_string() + request.uri().path().as_str()),
)
.is_dir()
{
return;
}
response.set_status(rocket::http::Status::Ok);
let str = "Hello,World!";
response.set_sized_body(str.len(), std::io::Cursor::new(str));
fn not_found(request: &rocket::Request) -> status::Custom<Template> {
let path = request.uri().path().to_string();
let path = path[1..path.len()].to_string();
// Remove the / in front of the path, if the path with / is spliced, the previous path will be ignored
let path = Path::new(&std::env::var("ROOT").unwrap()).join(path);
if !path.is_dir() {
let context: HashMap<&str, &str> = HashMap::new();
return status::Custom(Status::NotFound, Template::render("404", &context));
// Need to have file 404.tera as a placeholder
}
let context = &IndexContext {
title: &"title?".to_string(),
};
status::Custom(Status::Ok, Template::render("index", context))
}
struct Logger {}
@ -262,7 +246,6 @@ async fn main() {
}
match rocket::custom(figment)
.attach(Index {})
.attach(Logger {})
.attach(Template::fairing())
.mount("/", routes![file_server])

0
templates/404.tera Normal file
View File