qchgk_web/src/main.rs

129 lines
3.2 KiB
Rust
Raw Normal View History

2019-08-05 14:06:27 +00:00
#![feature(proc_macro_hygiene, decl_macro)]
2022-11-11 17:56:35 +00:00
extern crate serde;
2019-08-19 12:58:47 +00:00
extern crate serde_json;
2019-08-02 10:47:16 +00:00
2019-08-19 12:58:47 +00:00
#[macro_use]
extern crate rocket;
2019-08-05 14:06:27 +00:00
extern crate rocket_contrib;
2019-08-02 10:47:16 +00:00
2019-08-05 14:06:27 +00:00
use rocket::response::Redirect;
2019-08-19 12:58:47 +00:00
use rocket::{Rocket, State};
2019-08-05 14:06:27 +00:00
use rocket_contrib::serve::StaticFiles;
use rocket_contrib::templates::Template;
2019-08-02 10:47:16 +00:00
2019-08-19 12:58:47 +00:00
use rand::distributions::Uniform;
use rand::Rng;
2019-08-02 10:47:16 +00:00
2023-01-03 22:12:03 +00:00
use chgk_ledb_lib::db;
2022-11-11 17:56:35 +00:00
use chgk_ledb_lib::questions::Question;
2023-01-03 22:12:03 +00:00
const DB_FILENAME: &str = "db.dat";
2022-10-03 21:07:25 +00:00
trait ErrorEmpty {
type Output;
fn err_empty(self) -> Result<Self::Output, ()>;
}
impl<T, E> ErrorEmpty for Result<T, E> {
type Output = T;
fn err_empty(self) -> Result<Self::Output, ()> {
self.map_err(|_| ())
}
}
2023-01-03 22:12:03 +00:00
struct AppState{
database_distribution: Uniform<usize>,
2019-08-19 12:58:47 +00:00
}
2023-01-03 22:12:03 +00:00
fn open_db() -> db::Reader<Question> {
db::Reader::new(DB_FILENAME, 2048).expect("new db reader")
}
2019-08-19 12:58:47 +00:00
2023-01-03 22:12:03 +00:00
fn get_database_distribution() -> Uniform<usize> {
let last_id = open_db().len();
rand::distributions::Uniform::new_inclusive(1usize, last_id)
2019-08-19 12:58:47 +00:00
}
2023-01-03 22:12:03 +00:00
fn random_question_id(database_distribution: &Uniform<usize>) -> usize {
2019-08-19 12:58:47 +00:00
let mut rng = rand::thread_rng();
rng.sample(database_distribution)
2019-08-02 10:47:16 +00:00
}
2023-01-03 22:12:03 +00:00
fn get_question(id: usize) -> Result<Question, ()> {
open_db().get(id - 1).err_empty()
2019-08-02 10:47:16 +00:00
}
2023-01-03 22:12:03 +00:00
fn show_question_details(template_name: &'static str, data: &AppState, id: usize) -> Template {
match get_question(id) {
2022-10-03 21:07:25 +00:00
Ok(question) => {
2019-08-19 12:58:47 +00:00
let mut context = serde_json::to_value(question).expect("question serialize");
if context.is_object() {
let next_id = random_question_id(&data.database_distribution);
context["next"] = serde_json::to_value(next_id).expect("question id serialize");
}
2022-10-03 21:07:25 +00:00
Template::render(template_name, &context)
}
Err(_) => {
use std::collections::HashMap;
let context: HashMap<String, String> = HashMap::new();
Template::render("404", &context)
2019-08-02 10:47:16 +00:00
}
}
}
2019-08-05 14:06:27 +00:00
#[get("/q/<id>")]
2023-01-03 22:12:03 +00:00
fn show_question(data: State<AppState>, id: usize) -> Template {
2019-08-05 14:06:27 +00:00
show_question_details("question", data.inner(), id)
2019-08-02 10:47:16 +00:00
}
2019-08-05 14:06:27 +00:00
#[get("/q/<id>/a")]
2023-01-03 22:12:03 +00:00
fn show_answer(data: State<AppState>, id: usize) -> Template {
2019-08-05 14:06:27 +00:00
show_question_details("answer", data.inner(), id)
2019-08-02 10:47:16 +00:00
}
2022-10-03 21:07:25 +00:00
#[get("/q/0")]
fn question0() -> Redirect {
Redirect::to("/")
}
#[get("/q/0/a")]
fn answer0() -> Redirect {
Redirect::to("/")
}
2019-08-05 14:06:27 +00:00
#[get("/")]
fn index(data: State<AppState>) -> Redirect {
2019-08-19 12:58:47 +00:00
let id = random_question_id(&data.database_distribution);
2019-08-05 14:06:27 +00:00
Redirect::temporary(format!("/q/{}", id))
2019-08-02 10:47:16 +00:00
}
2019-08-05 14:06:27 +00:00
#[catch(404)]
fn not_found(_req: &rocket::Request) -> Template {
use std::collections::HashMap;
let context: HashMap<String, String> = HashMap::new();
Template::render("404", &context)
}
2019-08-02 10:47:16 +00:00
2019-08-05 14:06:27 +00:00
fn rocket() -> Rocket {
2023-01-03 22:12:03 +00:00
let state = AppState {
database_distribution: get_database_distribution(),
2019-08-05 14:06:27 +00:00
};
rocket::ignite()
.manage(state)
.register(catchers![not_found])
2022-10-03 21:07:25 +00:00
.mount(
"/",
routes![index, show_question, show_answer, question0, answer0],
)
2019-08-05 14:06:27 +00:00
.mount("/q", routes![index])
.mount("/q/static", StaticFiles::from("static/"))
.attach(Template::fairing())
}
fn main() {
rocket().launch();
2019-08-02 10:47:16 +00:00
}