min migrate to rocket 0.5
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Dmitry Belyaev 2023-08-05 19:21:00 +03:00
parent 78d57ca2e6
commit 18eb3ec51b
Signed by: b4tman
GPG Key ID: 41A00BF15EA7E5F3
3 changed files with 1038 additions and 628 deletions

1630
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -13,17 +13,14 @@ readme = "README.md"
rand="0.8"
serde="1.0"
serde_json="1.0"
rocket="0.4"
rocket = { version = "=0.5.0-rc.3", features = ["json"] }
rocket_dyn_templates = { version = "=0.1.0-rc.3", features = ["tera"] }
chgk_ledb_lib = {git = "https://gitea.b4tman.ru/b4tman/chgk_ledb.git", rev="27260695f7", package="chgk_ledb_lib"}
mini-moka = "0.10.0"
[dependencies.rocket_contrib]
version = "0.4"
default-features = false
features = ["serve", "tera_templates"]
[profile.release]
opt-level = 3
debug = false
lto = true
strip = true

View File

@ -1,16 +1,13 @@
#![feature(proc_macro_hygiene, decl_macro)]
extern crate serde;
extern crate serde_json;
#[macro_use]
extern crate rocket;
extern crate rocket_contrib;
use rocket::response::Redirect;
use rocket::{Rocket, State};
use rocket_contrib::serve::StaticFiles;
use rocket_contrib::templates::Template;
use rocket::State;
use rocket::fs::FileServer;
use rocket_dyn_templates::Template;
use rand::distributions::Uniform;
use rand::Rng;
@ -118,12 +115,12 @@ fn show_question_details(
}
#[get("/q/<id>")]
fn show_question(data: State<AppState>, cache: State<TemplateCache>, id: usize) -> Template {
fn show_question(data: &State<AppState>, cache: &State<TemplateCache>, id: usize) -> Template {
show_question_details("question", data.inner(), cache.inner(), id)
}
#[get("/q/<id>/a")]
fn show_answer(data: State<AppState>, cache: State<TemplateCache>, id: usize) -> Template {
fn show_answer(data: &State<AppState>, cache: &State<TemplateCache>, id: usize) -> Template {
show_question_details("answer", data.inner(), cache.inner(), id)
}
@ -138,7 +135,7 @@ fn answer0() -> Redirect {
}
#[get("/")]
fn index(data: State<AppState>) -> Redirect {
fn index(data: &State<AppState>) -> Redirect {
let id = random_question_id(&data.database_distribution);
Redirect::temporary(format!("/q/{}", id))
}
@ -150,26 +147,24 @@ fn not_found(_req: &rocket::Request) -> Template {
Template::render("404", context)
}
fn rocket() -> Rocket {
#[launch]
fn rocket() -> _ {
let state: AppState = db::Reader::new(DB_FILENAME, 2048).expect("open db").into();
let cache: TemplateCache = Cache::builder()
.time_to_idle(Duration::from_secs(15 * 60))
.max_capacity(300)
.build();
rocket::ignite()
rocket::build()
.manage(state)
.manage(cache)
.register(catchers![not_found])
.register("/", catchers![not_found])
.mount(
"/",
routes![index, show_question, show_answer, question0, answer0],
)
.mount("/q", routes![index])
.mount("/q/static", StaticFiles::from("static/"))
.mount("/q/static", FileServer::from("static/"))
.attach(Template::fairing())
}
fn main() {
rocket().launch();
}