redesign
This commit is contained in:
57
src/main.rs
57
src/main.rs
@@ -3,8 +3,8 @@
|
||||
extern crate serde;
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
extern crate serde_json;
|
||||
extern crate ledb;
|
||||
extern crate serde_json;
|
||||
#[macro_use]
|
||||
extern crate ledb_derive;
|
||||
extern crate ledb_types;
|
||||
@@ -14,15 +14,17 @@ extern crate ledb_types;
|
||||
|
||||
//use tera::Context;
|
||||
|
||||
#[macro_use] extern crate rocket;
|
||||
#[macro_use]
|
||||
extern crate rocket;
|
||||
extern crate rocket_contrib;
|
||||
|
||||
use rocket::{Rocket, State};
|
||||
use rocket::response::Redirect;
|
||||
use rocket::{Rocket, State};
|
||||
use rocket_contrib::serve::StaticFiles;
|
||||
use rocket_contrib::templates::Template;
|
||||
|
||||
use rand::seq::IteratorRandom;
|
||||
use rand::distributions::Uniform;
|
||||
use rand::Rng;
|
||||
|
||||
use ledb::{Options, Storage};
|
||||
|
||||
@@ -101,6 +103,21 @@ struct Question {
|
||||
|
||||
struct AppState {
|
||||
storage: Storage,
|
||||
database_distribution: Uniform<u32>,
|
||||
}
|
||||
|
||||
fn get_database_distribution(storage: &Storage) -> Uniform<u32> {
|
||||
let collection = storage
|
||||
.collection("questions")
|
||||
.expect("collection \"questions\"");
|
||||
let last_id = collection.last_id().expect("\"questions\" last_id");
|
||||
|
||||
rand::distributions::Uniform::new_inclusive(1u32, last_id)
|
||||
}
|
||||
|
||||
fn random_question_id(database_distribution: &Uniform<u32>) -> u32 {
|
||||
let mut rng = rand::thread_rng();
|
||||
rng.sample(database_distribution)
|
||||
}
|
||||
|
||||
fn get_question(storage: &Storage, id: u32) -> Result<Option<Question>, ()> {
|
||||
@@ -108,7 +125,9 @@ fn get_question(storage: &Storage, id: u32) -> Result<Option<Question>, ()> {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let collection = storage.collection("questions").expect("collection \"questions\"");
|
||||
let collection = storage
|
||||
.collection("questions")
|
||||
.expect("collection \"questions\"");
|
||||
let last_id = collection.last_id().expect("\"questions\" last_id");
|
||||
|
||||
if id > last_id {
|
||||
@@ -123,7 +142,11 @@ fn get_question(storage: &Storage, id: u32) -> Result<Option<Question>, ()> {
|
||||
}
|
||||
}
|
||||
|
||||
fn show_question_details(template_name: &'static str, data: &AppState, id: u32) -> Result<Template, Redirect> {
|
||||
fn show_question_details(
|
||||
template_name: &'static str,
|
||||
data: &AppState,
|
||||
id: u32,
|
||||
) -> Result<Template, Redirect> {
|
||||
let question = get_question(&data.storage, id);
|
||||
|
||||
if question.is_ok() {
|
||||
@@ -131,7 +154,12 @@ fn show_question_details(template_name: &'static str, data: &AppState, id: u32)
|
||||
|
||||
if question.is_some() {
|
||||
let question = question.unwrap();
|
||||
Ok(Template::render(template_name, &question))
|
||||
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");
|
||||
}
|
||||
Ok(Template::render(template_name, &context))
|
||||
} else {
|
||||
Err(Redirect::permanent("/q/"))
|
||||
}
|
||||
@@ -154,11 +182,7 @@ fn show_answer(data: State<AppState>, id: u32) -> Result<Template, Redirect> {
|
||||
|
||||
#[get("/")]
|
||||
fn index(data: State<AppState>) -> Redirect {
|
||||
let mut rng = rand::thread_rng();
|
||||
let collection = data.storage.collection("questions").expect("collection \"questions\"");
|
||||
let last_id = collection.last_id().expect("\"questions\" last_id");
|
||||
let id = (1..(last_id + 1)).choose(&mut rng).unwrap();
|
||||
|
||||
let id = random_question_id(&data.database_distribution);
|
||||
Redirect::temporary(format!("/q/{}", id))
|
||||
}
|
||||
|
||||
@@ -176,12 +200,13 @@ fn rocket() -> Rocket {
|
||||
}))
|
||||
.unwrap();
|
||||
|
||||
let state = AppState {
|
||||
storage: Storage::new("db", options).expect("open db")
|
||||
let mut state = AppState {
|
||||
storage: Storage::new("db", options).expect("open db"),
|
||||
database_distribution: Uniform::new(1, 3),
|
||||
};
|
||||
state.database_distribution = get_database_distribution(&state.storage);
|
||||
|
||||
let collection = state.storage.collection("questions").expect("collection \"questions\"");
|
||||
collection.last_id().expect("\"questions\" last_id");
|
||||
let state = state;
|
||||
|
||||
rocket::ignite()
|
||||
.manage(state)
|
||||
|
Reference in New Issue
Block a user