0.1.7-beta fix templete dir error, embed templete

This commit is contained in:
Tim-Paik 2021-08-14 21:55:20 +08:00
parent e68a1b7dd8
commit b04ddc4585
3 changed files with 14 additions and 4 deletions

2
Cargo.lock generated
View File

@ -2174,7 +2174,7 @@ checksum = "d7cff876b8f18eed75a66cf49b65e7f967cb354a7aa16003fb55dbfd25b44b4f"
[[package]]
name = "web"
version = "0.1.6-beta"
version = "0.1.7-beta"
dependencies = [
"chrono",
"clap",

View File

@ -3,7 +3,7 @@ authors = ["Tim_Paik <timpaikc@outlook.com>"]
description = "simple http server written in rust"
edition = "2018"
name = "web"
version = "0.1.6-beta"
version = "0.1.7-beta"
[dependencies]
chrono = "0.4"

View File

@ -5,6 +5,7 @@ extern crate rocket;
use colored::*;
use rocket::fairing::{Fairing, Info, Kind};
use rocket::figment::providers::{Env, Format, Toml};
use rocket::response::Redirect;
use rocket::{config::TlsConfig, fs::NamedFile};
use rocket_dyn_templates::Template;
@ -318,7 +319,11 @@ async fn main() {
std::env::var("WEB_SERVER_NAME").unwrap_or("timpaik'web server".to_string()),
))
.merge(("cli_colors", matches.is_present("color")))
.merge(("log_level", "off"));
.merge(("log_level", "off"))
.merge(("template_dir", "."))
// The default is "templates/", an error will be reported if the folder is not found
.merge(Toml::file(Env::var_or("WEB_CONFIG", "web.toml")).nested())
.merge(Env::prefixed("WEB_").ignore(&["PROFILE"]).global());
let enable_tls = matches.is_present("cert") && matches.is_present("key");
@ -355,7 +360,12 @@ async fn main() {
match rocket::custom(figment)
.attach(Logger {})
.attach(Template::fairing())
.attach(Template::custom(|engines| {
engines
.tera
.add_raw_template("index", include_str!("../templates/index.html.tera"))
.unwrap();
}))
.mount("/", routes![file_server])
.register("/", catchers![not_found])
.launch()