use async db
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing

This commit is contained in:
2023-08-16 12:20:04 +03:00
parent a4dd460c37
commit 971c79a111
3 changed files with 264 additions and 191 deletions

View File

@@ -1,11 +1,11 @@
#[macro_use]
extern crate rocket;
use rocket::fs::FileServer;
use rocket::response::Redirect;
use rocket::State;
use rocket::fs::FileServer;
use rocket_dyn_templates::Template;
use rocket_dyn_templates::tera;
use rocket_dyn_templates::Template;
use rand::distributions::Uniform;
use rand::Rng;
@@ -13,7 +13,7 @@ use rand::Rng;
use std::ops::Deref;
use std::sync::Arc;
use chgk_ledb_lib::db;
use chgk_ledb_lib::async_db;
use chgk_ledb_lib::questions::Question;
use mini_moka::sync::Cache;
@@ -51,7 +51,7 @@ impl ArcTemplateData {
type TemplateCache = mini_moka::sync::Cache<usize, ArcTemplateData>;
type DataBaseInner = db::Reader<Question>;
type DataBaseInner = async_db::Reader<Question>;
type DataBase = Arc<DataBaseInner>;
struct AppState {
db: DataBase,
@@ -76,11 +76,11 @@ fn random_question_id(database_distribution: &Uniform<usize>) -> usize {
rng.sample(database_distribution)
}
fn get_question(db: &DataBase, id: usize) -> Result<Question, ()> {
db.get(id - 1).err_empty()
async fn get_question(db: &DataBase, id: usize) -> Result<Question, ()> {
db.get(id - 1).await.err_empty()
}
fn show_question_details(
async fn show_question_details(
template_name: &'static str,
data: &AppState,
cache: &TemplateCache,
@@ -90,7 +90,7 @@ fn show_question_details(
return value.render(template_name);
}
match get_question(&data.db, id) {
match get_question(&data.db, id).await {
Ok(question) => {
let mut context = tera::to_value(question).expect("question serialize");
if context.is_object() {
@@ -113,13 +113,17 @@ fn show_question_details(
}
#[get("/q/<id>")]
fn show_question(data: &State<AppState>, cache: &State<TemplateCache>, id: usize) -> Template {
show_question_details("question", data.inner(), cache.inner(), id)
async fn show_question(
data: &State<AppState>,
cache: &State<TemplateCache>,
id: usize,
) -> Template {
show_question_details("question", data.inner(), cache.inner(), id).await
}
#[get("/q/<id>/a")]
fn show_answer(data: &State<AppState>, cache: &State<TemplateCache>, id: usize) -> Template {
show_question_details("answer", data.inner(), cache.inner(), id)
async fn show_answer(data: &State<AppState>, cache: &State<TemplateCache>, id: usize) -> Template {
show_question_details("answer", data.inner(), cache.inner(), id).await
}
#[get("/q/0")]
@@ -146,8 +150,11 @@ fn not_found(_req: &rocket::Request) -> Template {
}
#[launch]
fn rocket() -> _ {
let state: AppState = db::Reader::new(DB_FILENAME, 2048).expect("open db").into();
async fn rocket() -> _ {
let state: AppState = async_db::Reader::new(DB_FILENAME)
.await
.expect("open db")
.into();
let cache: TemplateCache = Cache::builder()
.time_to_idle(Duration::from_secs(15 * 60))
.max_capacity(300)
@@ -165,4 +172,3 @@ fn rocket() -> _ {
.mount("/q/static", FileServer::from("static/"))
.attach(Template::fairing())
}