-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add cache to question and page
- Loading branch information
Showing
21 changed files
with
433 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use sea_orm_migration::prelude::*; | ||
|
||
#[derive(DeriveMigrationName)] | ||
pub struct Migration; | ||
|
||
#[async_trait::async_trait] | ||
impl MigrationTrait for Migration { | ||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
manager | ||
.create_table( | ||
Table::create() | ||
.table(Admin::Table) | ||
.if_not_exists() | ||
.col( | ||
ColumnDef::new(Admin::Id) | ||
.big_unsigned() | ||
.not_null() | ||
.primary_key(), | ||
) | ||
.col(ColumnDef::new(Admin::Username).string().not_null()) | ||
.col(ColumnDef::new(Admin::Disabled).boolean().not_null().default(false)) | ||
.to_owned(), | ||
) | ||
.await | ||
} | ||
|
||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
manager | ||
.drop_table(Table::drop().table(Admin::Table).to_owned()) | ||
.await | ||
} | ||
} | ||
|
||
#[derive(DeriveIden)] | ||
enum Admin { | ||
Table, | ||
Id, | ||
Username, | ||
Disabled, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
use axum::Router; | ||
use axum::routing::get; | ||
use axum::Router; | ||
|
||
mod page; | ||
mod question; | ||
mod modify; | ||
|
||
pub fn get_question_routers() -> Router { | ||
Router::new() | ||
.route("/", get(page::get_page)) | ||
.route("/", get(page::get_page).post(modify::new_question)) | ||
.route("/:question", get(question::get_question)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use crate::model::question::QuestionType; | ||
use crate::service::questions::save_question; | ||
use axum::Json; | ||
use sea_orm::JsonValue; | ||
use serde::Serialize; | ||
|
||
pub async fn new_question(Json(question): Json<NewQuestionRequest>) -> String { | ||
let id = save_question(question.content, question.r#type, question.values, question.condition, question.required, None).await; | ||
id.to_string() | ||
} | ||
|
||
#[derive(serde::Deserialize, Serialize)] | ||
pub struct NewQuestionRequest { | ||
pub content: JsonValue, | ||
pub r#type: QuestionType, | ||
pub values: Option<Vec<JsonValue>>, | ||
pub condition: Option<String>, | ||
pub required: bool, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use crate::controller::error::ErrorMessage; | ||
use crate::model::generated::admin; | ||
use crate::model::generated::prelude::Admin; | ||
use axum::extract::FromRequestParts; | ||
use axum::http::request::Parts; | ||
use lazy_static::lazy_static; | ||
use migration::async_trait::async_trait; | ||
use moka::future::Cache; | ||
use sea_orm::ColumnTrait; | ||
use sea_orm::{EntityTrait, QueryFilter}; | ||
use std::time::Duration; | ||
|
||
lazy_static! { | ||
static ref ADMIN_TOKEN_CACHE: Cache<String, admin::Model> = Cache::builder() | ||
.time_to_idle(Duration::from_secs(60 * 60 * 24 * 7)) //if the key is not accessed for 7 days, it will be removed | ||
.build(); | ||
} | ||
|
||
pub async fn get_admin_by_id(id: i32) -> Option<admin::Model> { | ||
Admin::find() | ||
.filter(admin::Column::Id.eq(id)) | ||
.one(&*crate::DATABASE).await.unwrap() | ||
} | ||
|
||
pub async fn register_admin_token(token: &str, user: i32) { | ||
let admin = get_admin_by_id(user).await.unwrap(); | ||
ADMIN_TOKEN_CACHE.insert(token.to_string(), admin).await; | ||
} | ||
|
||
pub async fn get_admin_by_token(token: &str) -> Option<admin::Model> { | ||
ADMIN_TOKEN_CACHE.get(token).await | ||
} | ||
|
||
pub struct AdminTokenInfo(pub admin::Model); | ||
|
||
#[async_trait] | ||
impl<S> FromRequestParts<S> for AdminTokenInfo | ||
where S: Send + Sync { | ||
type Rejection = ErrorMessage; | ||
|
||
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> { | ||
let headers = &parts.headers; | ||
let token = headers.get("token") | ||
.ok_or(ErrorMessage::InvalidToken)? | ||
.to_str() | ||
.map_err(|_| ErrorMessage::InvalidToken)?; | ||
let user = get_admin_by_token(token).await | ||
.ok_or(ErrorMessage::TokenNotActivated)?; | ||
|
||
Ok(AdminTokenInfo(user)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.15 | ||
use sea_orm::entity::prelude::*; | ||
|
||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] | ||
#[sea_orm(table_name = "admin")] | ||
pub struct Model { | ||
#[sea_orm(primary_key, auto_increment = false)] | ||
pub id: i32, | ||
pub username: String, | ||
pub disabled: bool, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] | ||
pub enum Relation {} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
pub mod prelude; | ||
|
||
pub mod admin; | ||
pub mod answer; | ||
pub mod page; | ||
pub mod question; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.