Skip to content

Commit

Permalink
wip: web custom extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
ElaBosak233 committed Dec 9, 2024
1 parent 2b575ab commit 3ed4018
Show file tree
Hide file tree
Showing 46 changed files with 575 additions and 408 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ name = "cdsctf"
version = "0.0.1"
edition = "2021"
description = "The CdsCTF project is an open-source, high-performance, Jeopardy-style's CTF platform."
publish = false

[dependencies]
# Async
async-trait = { version = "0.1" }
tokio = { version = "1.41", features = ["full"] }
tokio-util = { version = "0.7.12" }
tokio = { version = "1.42", features = ["full"] }
tokio-util = { version = "0.7.13" }
futures = { version = "^0.3" }
futures-util = { version = "^0.3" }
tower = { version = "0.5" }
Expand Down
3 changes: 1 addition & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn main() {
"cargo:rustc-env=GIT_COMMIT={}",
String::from_utf8(
Command::new("git")
.args(&["rev-parse", "HEAD"])
.args(["rev-parse", "HEAD"])
.output()
.expect("Failed to execute git command")
.stdout,
Expand All @@ -18,6 +18,5 @@ fn main() {
"cargo:rustc-env=BUILD_AT={}",
chrono::Utc::now()
.format("%Y-%m-%d %H:%M:%S UTC")
.to_string()
);
}
2 changes: 1 addition & 1 deletion src/assets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ pub fn get(path: &str) -> Option<Vec<u8>> {
if let Ok(file) = fs::read(format!("assets/{}", path)) {
return Some(file);
}
Assets::get(&path).map(|e| e.data.into_owned())
Assets::get(path).map(|e| e.data.into_owned())
}
3 changes: 1 addition & 2 deletions src/cluster/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod traits;

use std::{collections::BTreeMap, process, sync::OnceLock, time::Duration};
use std::{collections::BTreeMap, process};

use axum::extract::ws::WebSocket;
use k8s_openapi::{
Expand All @@ -12,7 +12,6 @@ use k8s_openapi::{
};
use kube::{
api::{Api, DeleteParams, ListParams, PostParams, ResourceExt},
config::Kubeconfig,
runtime::{reflector::Lookup, wait::conditions},
Client as K8sClient, Config,
};
Expand Down
2 changes: 1 addition & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use futures_util::StreamExt;
use once_cell::sync::Lazy;
use sea_orm::EntityTrait;
use serde::{Deserialize, Serialize};
use tokio::sync::{Mutex, RwLock};
use tokio::sync::RwLock;
use tracing::info;

use crate::db::get_db;
Expand Down
1 change: 0 additions & 1 deletion src/config/site/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use sea_orm::FromJsonQueryResult;
use serde::{Deserialize, Serialize};

use crate::config::traits::Merge;

#[derive(Clone, Debug, Serialize, Deserialize, FromJsonQueryResult, PartialEq, Eq, Default)]
pub struct Config {
Expand Down
4 changes: 1 addition & 3 deletions src/db/entity/challenge.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use async_trait::async_trait;
use sea_orm::{
ActiveModelBehavior, ActiveModelTrait, ColumnTrait, ConnectionTrait, DbErr, DeriveActiveEnum,
DeriveEntityModel, DerivePrimaryKey, EntityTrait, EnumIter, FromJsonQueryResult,
PaginatorTrait, PrimaryKeyTrait, QueryFilter, QuerySelect, Related, RelationDef, RelationTrait,
DeriveEntityModel, DerivePrimaryKey, EntityTrait, EnumIter, FromJsonQueryResult, PrimaryKeyTrait, Related, RelationDef, RelationTrait,
Set,
};
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};

use super::{game, game_challenge, pod, submission};
use crate::db::get_db;

#[derive(Debug, Clone, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "challenges")]
Expand Down
3 changes: 1 addition & 2 deletions src/db/entity/game.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use async_trait::async_trait;
use sea_orm::{entity::prelude::*, QuerySelect, Set};
use sea_orm::{entity::prelude::*, Set};
use serde::{Deserialize, Serialize};

use super::{challenge, game_challenge, game_team, pod, submission, team};
use crate::db::get_db;

#[derive(Debug, Clone, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "games")]
Expand Down
1 change: 0 additions & 1 deletion src/db/entity/game_challenge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

use super::{challenge, game, user};
use crate::db::get_db;

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "game_challenges")]
Expand Down
1 change: 0 additions & 1 deletion src/db/entity/pod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use sea_orm::{entity::prelude::*, FromJsonQueryResult, Set};
use serde::{Deserialize, Serialize};

use super::{challenge, game, team, user};
use crate::db::get_db;

#[derive(Debug, Clone, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "pods")]
Expand Down
3 changes: 1 addition & 2 deletions src/db/entity/submission.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use async_trait::async_trait;
use sea_orm::{entity::prelude::*, IntoActiveModel, QueryOrder, QuerySelect, Set, TryIntoModel};
use sea_orm::{entity::prelude::*, Set};
use serde::{Deserialize, Serialize};

use super::{challenge, game, team, user};
use crate::db::get_db;

#[derive(Debug, Clone, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "submissions")]
Expand Down
3 changes: 1 addition & 2 deletions src/db/entity/team.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use async_trait::async_trait;
use sea_orm::{entity::prelude::*, Iterable, JoinType, QuerySelect, Set};
use sea_orm::{entity::prelude::*, Set};
use serde::{Deserialize, Serialize};

use super::{game, game_team, pod, submission, user, user_team};
use crate::db::get_db;

#[derive(Debug, Clone, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "teams")]
Expand Down
3 changes: 1 addition & 2 deletions src/db/entity/user.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use async_trait::async_trait;
use sea_orm::{entity::prelude::*, Condition, QuerySelect, Set};
use sea_orm::{entity::prelude::*, Set};
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};

use super::{pod, submission, team, user_team};
use crate::db::get_db;

#[derive(Debug, Clone, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "users")]
Expand Down
5 changes: 1 addition & 4 deletions src/db/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ where
let schema = Schema::new(builder);
let stmt = builder.build(schema.create_table_from_entity(entity).if_not_exists());

match db.execute(stmt).await {
Err(e) => error!("Error: {}", e),
_ => {}
}
if let Err(e) = db.execute(stmt).await { error!("Error: {}", e) }
}

pub async fn migrate(db: &DbConn) {
Expand Down
7 changes: 3 additions & 4 deletions src/db/transfer/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

use crate::{
Expand All @@ -18,9 +17,9 @@ impl From<entity::config::Model> for Config {
fn from(model: entity::config::Model) -> Self {
Self {
id: model.id,
auth: model.auth.into(),
cluster: model.cluster.into(),
site: model.site.into(),
auth: model.auth,
cluster: model.cluster,
site: model.site,
}
}
}
4 changes: 2 additions & 2 deletions src/db/transfer/game_challenge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ impl From<entity::game_challenge::Model> for GameChallenge {
}

async fn preload(
mut models: Vec<entity::game_challenge::Model>,
models: Vec<entity::game_challenge::Model>,
) -> Result<Vec<GameChallenge>, DbErr> {
let challenges = models
.load_one(entity::challenge::Entity, get_db())
.await?
.into_iter()
.map(|c| c.map(|challenge| Challenge::from(challenge)))
.map(|c| c.map(Challenge::from))
.collect::<Vec<Option<Challenge>>>();

let mut game_challenges = models
Expand Down
2 changes: 1 addition & 1 deletion src/db/transfer/game_team.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub async fn find(
let game_teams = sql.all(get_db()).await?;
let mut game_teams = game_teams
.into_iter()
.map(|game_team| GameTeam::from(game_team))
.map(GameTeam::from)
.collect::<Vec<GameTeam>>();

game_teams = preload(game_teams).await?;
Expand Down
10 changes: 5 additions & 5 deletions src/db/transfer/pod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,28 +72,28 @@ async fn preload(mut pods: Vec<Pod>) -> Result<Vec<Pod>, DbErr> {
let models = pods
.clone()
.into_iter()
.map(|pod| entity::pod::Model::from(pod))
.map(entity::pod::Model::from)
.collect::<Vec<entity::pod::Model>>();

let users = models
.load_one(entity::user::Entity, get_db())
.await?
.into_iter()
.map(|u| u.map(|user| User::from(user)))
.map(|u| u.map(User::from))
.collect::<Vec<Option<User>>>();

let teams = models
.load_one(entity::team::Entity, get_db())
.await?
.into_iter()
.map(|t| t.map(|team| Team::from(team)))
.map(|t| t.map(Team::from))
.collect::<Vec<Option<Team>>>();

let challenges = models
.load_one(entity::challenge::Entity, get_db())
.await?
.into_iter()
.map(|c| c.map(|challenge| Challenge::from(challenge)))
.map(|c| c.map(Challenge::from))
.collect::<Vec<Option<Challenge>>>();

for (i, pod) in pods.iter_mut().enumerate() {
Expand Down Expand Up @@ -150,7 +150,7 @@ pub async fn find(
let pods = sql.all(get_db()).await?;
let mut pods = pods
.into_iter()
.map(|pod| Pod::from(pod))
.map(Pod::from)
.collect::<Vec<Pod>>();

pods = preload(pods).await?;
Expand Down
14 changes: 7 additions & 7 deletions src/db/transfer/submission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,32 +75,32 @@ async fn preload(mut submissions: Vec<Submission>) -> Result<Vec<Submission>, Db
let models = submissions
.clone()
.into_iter()
.map(|submission| entity::submission::Model::from(submission))
.map(entity::submission::Model::from)
.collect::<Vec<entity::submission::Model>>();

let users = models
.load_one(entity::user::Entity, get_db())
.await?
.into_iter()
.map(|u| u.map(|user| User::from(user)))
.map(|u| u.map(User::from))
.collect::<Vec<Option<User>>>();
let challenges = models
.load_one(entity::challenge::Entity, get_db())
.await?
.into_iter()
.map(|c| c.map(|challenge| Challenge::from(challenge)))
.map(|c| c.map(Challenge::from))
.collect::<Vec<Option<Challenge>>>();
let teams = models
.load_one(entity::team::Entity, get_db())
.await?
.into_iter()
.map(|t| t.map(|team| Team::from(team)))
.map(|t| t.map(Team::from))
.collect::<Vec<Option<Team>>>();
let games = models
.load_one(entity::game::Entity, get_db())
.await?
.into_iter()
.map(|g| g.map(|game| Game::from(game)))
.map(|g| g.map(Game::from))
.collect::<Vec<Option<Game>>>();

for (i, submission) in submissions.iter_mut().enumerate() {
Expand Down Expand Up @@ -163,7 +163,7 @@ pub async fn find(
let submissions = sql.all(get_db()).await?;
let mut submissions = submissions
.into_iter()
.map(|submission| Submission::from(submission))
.map(Submission::from)
.collect::<Vec<Submission>>();

submissions = preload(submissions).await?;
Expand All @@ -180,7 +180,7 @@ pub async fn get_by_challenge_ids(challenge_ids: Vec<i64>) -> Result<Vec<Submiss

let mut submissions = submissions
.into_iter()
.map(|submission| Submission::from(submission))
.map(Submission::from)
.collect::<Vec<Submission>>();
submissions = preload(submissions).await?;
Ok(submissions)
Expand Down
6 changes: 3 additions & 3 deletions src/db/transfer/team.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use sea_orm::{entity::prelude::*, Iterable, JoinType, QuerySelect, Set};
use sea_orm::{entity::prelude::*, Iterable, JoinType, QuerySelect};
use serde::{Deserialize, Serialize};

use super::User;
Expand Down Expand Up @@ -70,7 +70,7 @@ async fn preload(mut teams: Vec<Team>) -> Result<Vec<Team>, DbErr> {
let models = teams
.clone()
.into_iter()
.map(|team| entity::team::Model::from(team))
.map(entity::team::Model::from)
.collect::<Vec<entity::team::Model>>();
let users = models
.load_many_to_many(entity::user::Entity, entity::user_team::Entity, get_db())
Expand All @@ -79,7 +79,7 @@ async fn preload(mut teams: Vec<Team>) -> Result<Vec<Team>, DbErr> {
.map(|users| {
users
.into_iter()
.map(|user| User::from(user))
.map(User::from)
.collect::<Vec<User>>()
})
.collect::<Vec<Vec<User>>>();
Expand Down
6 changes: 3 additions & 3 deletions src/db/transfer/user.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use sea_orm::{entity::prelude::*, Condition, QuerySelect, Set};
use sea_orm::{entity::prelude::*, Condition, QuerySelect};
use serde::{Deserialize, Serialize};

use super::Team;
Expand Down Expand Up @@ -64,7 +64,7 @@ async fn preload(mut users: Vec<User>) -> Result<Vec<User>, DbErr> {
let models = users
.clone()
.into_iter()
.map(|user| entity::user::Model::from(user))
.map(entity::user::Model::from)
.collect::<Vec<entity::user::Model>>();
let teams = models
.load_many_to_many(entity::team::Entity, entity::user_team::Entity, get_db())
Expand All @@ -73,7 +73,7 @@ async fn preload(mut users: Vec<User>) -> Result<Vec<User>, DbErr> {
.map(|teams| {
teams
.into_iter()
.map(|team| Team::from(team))
.map(Team::from)
.collect::<Vec<Team>>()
})
.collect::<Vec<Vec<Team>>>();
Expand Down
2 changes: 1 addition & 1 deletion src/db/transfer/user_team.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub async fn find(
let user_teams = sql.all(get_db()).await?;
let user_teams = user_teams
.into_iter()
.map(|user_team| UserTeam::from(user_team))
.map(UserTeam::from)
.collect::<Vec<UserTeam>>();

Ok((user_teams, total))
Expand Down
2 changes: 1 addition & 1 deletion src/env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub mod db;
pub mod metric;
pub mod queue;

use std::{path::Path, process, sync::OnceLock};
use std::{path::Path, process};

use once_cell::sync::OnceCell;
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion src/media/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub async fn get(path: String, filename: String) -> Result<Vec<u8>, MediaError>
match File::open(&filepath).await {
Ok(mut file) => {
let mut buffer = Vec::new();
if let Err(_) = file.read_to_end(&mut buffer).await {
if (file.read_to_end(&mut buffer).await).is_err() {
return Err(MediaError::InternalServerError(String::new()));
}
Ok(buffer)
Expand Down
2 changes: 1 addition & 1 deletion src/media/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub fn hash(data: Vec<u8>) -> String {
}

pub async fn img_convert_to_webp(img: Vec<u8>) -> Result<Vec<u8>, anyhow::Error> {
let origin_image = image::load_from_memory(&*img)?.to_rgba8();
let origin_image = image::load_from_memory(&img)?.to_rgba8();
let webp_encoder =
webp::Encoder::from_rgba(&origin_image, origin_image.width(), origin_image.height());
let webp_image = webp_encoder.encode(85.0);
Expand Down
2 changes: 1 addition & 1 deletion src/metric/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use once_cell::sync::Lazy;
use prometheus::{Gauge, IntCounterVec, IntGauge, Opts, Registry};

pub static METRICS_REGISTRY: Lazy<Registry> = Lazy::new(|| Registry::new());
pub static METRICS_REGISTRY: Lazy<Registry> = Lazy::new(Registry::new);

pub static HTTP_REQUEST_TOTAL: Lazy<IntCounterVec> = Lazy::new(|| {
let opts = Opts::new(
Expand Down
Loading

0 comments on commit 3ed4018

Please sign in to comment.