min migrate to rocket 0.5
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
78d57ca2e6
commit
18eb3ec51b
1630
Cargo.lock
generated
1630
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -13,17 +13,14 @@ readme = "README.md"
|
|||||||
rand="0.8"
|
rand="0.8"
|
||||||
serde="1.0"
|
serde="1.0"
|
||||||
serde_json="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"}
|
chgk_ledb_lib = {git = "https://gitea.b4tman.ru/b4tman/chgk_ledb.git", rev="27260695f7", package="chgk_ledb_lib"}
|
||||||
mini-moka = "0.10.0"
|
mini-moka = "0.10.0"
|
||||||
|
|
||||||
[dependencies.rocket_contrib]
|
|
||||||
version = "0.4"
|
|
||||||
default-features = false
|
|
||||||
features = ["serve", "tera_templates"]
|
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
opt-level = 3
|
opt-level = 3
|
||||||
debug = false
|
debug = false
|
||||||
lto = true
|
lto = true
|
||||||
strip = true
|
strip = true
|
||||||
|
|
||||||
|
27
src/main.rs
27
src/main.rs
@ -1,16 +1,13 @@
|
|||||||
#![feature(proc_macro_hygiene, decl_macro)]
|
|
||||||
|
|
||||||
extern crate serde;
|
extern crate serde;
|
||||||
extern crate serde_json;
|
extern crate serde_json;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate rocket;
|
extern crate rocket;
|
||||||
extern crate rocket_contrib;
|
|
||||||
|
|
||||||
use rocket::response::Redirect;
|
use rocket::response::Redirect;
|
||||||
use rocket::{Rocket, State};
|
use rocket::State;
|
||||||
use rocket_contrib::serve::StaticFiles;
|
use rocket::fs::FileServer;
|
||||||
use rocket_contrib::templates::Template;
|
use rocket_dyn_templates::Template;
|
||||||
|
|
||||||
use rand::distributions::Uniform;
|
use rand::distributions::Uniform;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
@ -118,12 +115,12 @@ fn show_question_details(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[get("/q/<id>")]
|
#[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)
|
show_question_details("question", data.inner(), cache.inner(), id)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/q/<id>/a")]
|
#[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)
|
show_question_details("answer", data.inner(), cache.inner(), id)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,7 +135,7 @@ fn answer0() -> Redirect {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
fn index(data: State<AppState>) -> Redirect {
|
fn index(data: &State<AppState>) -> Redirect {
|
||||||
let id = random_question_id(&data.database_distribution);
|
let id = random_question_id(&data.database_distribution);
|
||||||
Redirect::temporary(format!("/q/{}", id))
|
Redirect::temporary(format!("/q/{}", id))
|
||||||
}
|
}
|
||||||
@ -150,26 +147,24 @@ fn not_found(_req: &rocket::Request) -> Template {
|
|||||||
Template::render("404", context)
|
Template::render("404", context)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rocket() -> Rocket {
|
#[launch]
|
||||||
|
fn rocket() -> _ {
|
||||||
let state: AppState = db::Reader::new(DB_FILENAME, 2048).expect("open db").into();
|
let state: AppState = db::Reader::new(DB_FILENAME, 2048).expect("open db").into();
|
||||||
let cache: TemplateCache = Cache::builder()
|
let cache: TemplateCache = Cache::builder()
|
||||||
.time_to_idle(Duration::from_secs(15 * 60))
|
.time_to_idle(Duration::from_secs(15 * 60))
|
||||||
.max_capacity(300)
|
.max_capacity(300)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
rocket::ignite()
|
rocket::build()
|
||||||
.manage(state)
|
.manage(state)
|
||||||
.manage(cache)
|
.manage(cache)
|
||||||
.register(catchers![not_found])
|
.register("/", catchers![not_found])
|
||||||
.mount(
|
.mount(
|
||||||
"/",
|
"/",
|
||||||
routes![index, show_question, show_answer, question0, answer0],
|
routes![index, show_question, show_answer, question0, answer0],
|
||||||
)
|
)
|
||||||
.mount("/q", routes![index])
|
.mount("/q", routes![index])
|
||||||
.mount("/q/static", StaticFiles::from("static/"))
|
.mount("/q/static", FileServer::from("static/"))
|
||||||
.attach(Template::fairing())
|
.attach(Template::fairing())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
|
||||||
rocket().launch();
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user