opt: use database connection pool
This commit is contained in:
@@ -5,6 +5,7 @@ use rocket::serde::{
|
||||
json::{json, Value},
|
||||
Serialize,
|
||||
};
|
||||
use crate::db_conn::DbConn;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Serialize)]
|
||||
@@ -48,8 +49,7 @@ pub fn c2output(p: &Post, cs: &Vec<Comment>, user: &CurrentUser) -> Vec<CommentO
|
||||
}
|
||||
|
||||
#[get("/getcomment?<pid>")]
|
||||
pub fn get_comment(pid: i32, user: CurrentUser) -> API<Value> {
|
||||
let conn = establish_connection();
|
||||
pub fn get_comment(pid: i32, user: CurrentUser, conn: DbConn) -> API<Value> {
|
||||
let p = Post::get(&conn, pid).map_err(APIError::from_db)?;
|
||||
if p.is_deleted {
|
||||
return Err(APIError::PcError(IsDeleted));
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
use crate::models::*;
|
||||
use crate::random_hasher::RandomHasher;
|
||||
use rocket::http::Status;
|
||||
use rocket::request::{self, FromRequest, Request};
|
||||
use rocket::request::{FromRequest, Request, Outcome};
|
||||
use rocket::response::{self, Responder};
|
||||
use rocket::serde::json::json;
|
||||
use crate::db_conn::DbPool;
|
||||
|
||||
#[catch(401)]
|
||||
pub fn catch_401_error() -> &'static str {
|
||||
@@ -20,7 +21,7 @@ pub struct CurrentUser {
|
||||
#[rocket::async_trait]
|
||||
impl<'r> FromRequest<'r> for CurrentUser {
|
||||
type Error = ();
|
||||
async fn from_request(request: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
|
||||
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> {
|
||||
let rh = request.rocket().state::<RandomHasher>().unwrap();
|
||||
let mut cu: Option<CurrentUser> = None;
|
||||
|
||||
@@ -35,7 +36,7 @@ impl<'r> FromRequest<'r> for CurrentUser {
|
||||
is_admin: false,
|
||||
});
|
||||
} else {
|
||||
let conn = establish_connection();
|
||||
let conn = request.rocket().state::<DbPool>().unwrap().get().unwrap();
|
||||
if let Some(user) = User::get_by_token(&conn, token) {
|
||||
let namehash = rh.hash_with_salt(&user.name);
|
||||
cu = Some(CurrentUser {
|
||||
@@ -48,8 +49,8 @@ impl<'r> FromRequest<'r> for CurrentUser {
|
||||
}
|
||||
}
|
||||
match cu {
|
||||
Some(u) => request::Outcome::Success(u),
|
||||
None => request::Outcome::Failure((Status::Unauthorized, ())),
|
||||
Some(u) => Outcome::Success(u),
|
||||
None => Outcome::Failure((Status::Unauthorized, ())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,12 +2,12 @@ use crate::api::comment::{c2output, CommentOutput};
|
||||
use crate::api::{APIError, CurrentUser, PolicyError::*, API};
|
||||
use crate::models::*;
|
||||
use chrono::NaiveDateTime;
|
||||
use diesel::SqliteConnection;
|
||||
use rocket::form::Form;
|
||||
use rocket::serde::{
|
||||
json::{json, Value},
|
||||
Serialize,
|
||||
};
|
||||
use crate::db_conn::DbConn;
|
||||
|
||||
#[derive(FromForm)]
|
||||
pub struct PostInput<'r> {
|
||||
@@ -40,7 +40,7 @@ pub struct PostOutput {
|
||||
reply: i32,
|
||||
}
|
||||
|
||||
fn p2output(p: &Post, user: &CurrentUser, conn: &SqliteConnection) -> PostOutput {
|
||||
fn p2output(p: &Post, user: &CurrentUser, conn: &DbConn) -> PostOutput {
|
||||
PostOutput {
|
||||
pid: p.id,
|
||||
text: p.content.to_string(),
|
||||
@@ -80,8 +80,7 @@ fn p2output(p: &Post, user: &CurrentUser, conn: &SqliteConnection) -> PostOutput
|
||||
}
|
||||
|
||||
#[get("/getone?<pid>")]
|
||||
pub fn get_one(pid: i32, user: CurrentUser) -> API<Value> {
|
||||
let conn = establish_connection();
|
||||
pub fn get_one(pid: i32, user: CurrentUser, conn: DbConn) -> API<Value> {
|
||||
let p = Post::get(&conn, pid).map_err(APIError::from_db)?;
|
||||
if !user.is_admin {
|
||||
if p.is_reported {
|
||||
@@ -98,9 +97,8 @@ pub fn get_one(pid: i32, user: CurrentUser) -> API<Value> {
|
||||
}
|
||||
|
||||
#[get("/getlist?<p>&<order_mode>")]
|
||||
pub fn get_list(p: Option<u32>, order_mode: u8, user: CurrentUser) -> API<Value> {
|
||||
pub fn get_list(p: Option<u32>, order_mode: u8, user: CurrentUser, conn: DbConn) -> API<Value> {
|
||||
let page = p.unwrap_or(1);
|
||||
let conn = establish_connection();
|
||||
let ps = Post::gets_by_page(&conn, order_mode, page, 25, user.is_admin)
|
||||
.map_err(APIError::from_db)?;
|
||||
let ps_data = ps
|
||||
@@ -115,8 +113,7 @@ pub fn get_list(p: Option<u32>, order_mode: u8, user: CurrentUser) -> API<Value>
|
||||
}
|
||||
|
||||
#[post("/dopost", data = "<poi>")]
|
||||
pub fn publish_post(poi: Form<PostInput>, user: CurrentUser) -> API<Value> {
|
||||
let conn = establish_connection();
|
||||
pub fn publish_post(poi: Form<PostInput>, user: CurrentUser, conn: DbConn) -> API<Value> {
|
||||
dbg!(poi.use_title, poi.allow_search);
|
||||
let r = Post::create(
|
||||
&conn,
|
||||
|
||||
@@ -2,9 +2,10 @@ use crate::api::{CurrentUser, API};
|
||||
use crate::random_hasher::RandomHasher;
|
||||
use rocket::serde::json::{json, Value};
|
||||
use rocket::State;
|
||||
use crate::db_conn::DbConn;
|
||||
|
||||
#[get("/systemlog")]
|
||||
pub fn get_systemlog(user: CurrentUser, rh: &State<RandomHasher>) -> API<Value> {
|
||||
pub fn get_systemlog(user: CurrentUser, rh: &State<RandomHasher>, conn: DbConn) -> API<Value> {
|
||||
Ok(json!({
|
||||
"tmp_token": rh.get_tmp_token(),
|
||||
"salt": look!(rh.salt),
|
||||
|
||||
Reference in New Issue
Block a user