Skip to content

Commit

Permalink
wip: web
Browse files Browse the repository at this point in the history
  • Loading branch information
ElaBosak233 committed Jul 29, 2024
1 parent a653ffe commit e2ce4bc
Show file tree
Hide file tree
Showing 39 changed files with 141 additions and 90 deletions.
19 changes: 17 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ mod media;
mod model;
mod proxy;
mod repository;
mod server;
mod traits;
mod util;
mod web;

use tracing::info;

#[tokio::main]
async fn main() {
Expand All @@ -25,5 +27,18 @@ async fn main() {
.replace("{{build_at}}", env!("BUILD_AT"))
);

server::bootstrap().await;
logger::init();
config::init().await;
database::init().await;
container::init().await;
web::init();

info!("{:?}", util::jwt::get_secret().await);

let addr = format!("{}:{}", config::get_app_config().axum.host, config::get_app_config().axum.port);
let listener = tokio::net::TcpListener::bind(&addr).await;

info!("Cloudsdale service has been started at {}. Enjoy your hacking challenges!", &addr);

axum::serve(listener.unwrap(), web::get_app()).await.unwrap();
}
1 change: 0 additions & 1 deletion src/server/middleware/mod.rs

This file was deleted.

40 changes: 0 additions & 40 deletions src/server/mod.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use axum::{
use serde_json::json;

use crate::model::category::request::{CreateRequest, FindRequest, UpdateRequest};
use crate::server::service;
use crate::web::service;
use crate::util::validate;

/// **Find** can find categories.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use axum::{
Extension,
extract::{Multipart, Path, Query},
http::{header, Response, StatusCode},
response::IntoResponse,
Extension, Json,
Json, response::IntoResponse,
};
use serde_json::json;

use crate::{server::service, traits::Ext};

use crate::traits::Ext;
use crate::web::service;
use crate::util::validate;

/// **Find** can be used to find challenges.
Expand Down
File renamed without changes.
7 changes: 4 additions & 3 deletions src/server/controller/game.rs → src/web/controller/game.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::{server::service, traits::Ext};
use crate::traits::Ext;
use axum::{
Extension,
extract::{Multipart, Path, Query},
http::{Response, StatusCode},
response::IntoResponse,
Extension, Json,
Json, response::IntoResponse,
};
use mime::Mime;
use serde_json::json;
use crate::web::service;

pub async fn find(Extension(ext): Extension<Ext>, Query(params): Query<crate::model::game::request::FindRequest>) -> impl IntoResponse {
let operator = ext.operator.unwrap();
Expand Down
File renamed without changes.
File renamed without changes.
7 changes: 4 additions & 3 deletions src/server/controller/pod.rs → src/web/controller/pod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use axum::{
Extension,
extract::{Path, Query},
http::StatusCode,
response::IntoResponse,
Extension, Json,
Json, response::IntoResponse,
};
use serde_json::json;

use crate::{server::service, traits::Ext};
use crate::traits::Ext;
use crate::web::service;

pub async fn find(Query(params): Query<crate::model::pod::request::FindRequest>) -> impl IntoResponse {
match service::pod::find(params).await {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use axum::{
Extension,
extract::{Path, Query},
http::StatusCode,
response::IntoResponse,
Extension, Json,
Json, response::IntoResponse,
};
use serde_json::json;

use crate::{server::service::submission as submission_service, traits::Ext};
use crate::traits::Ext;
use crate::web::service::submission as submission_service;

/// **Find** can be used to find submissions.
///
Expand Down
10 changes: 4 additions & 6 deletions src/server/controller/team.rs → src/web/controller/team.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use crate::{
server::service::{team as team_service, user_team as user_team_service},
traits::Ext,
};
use crate::traits::Ext;
use axum::{
Extension,
extract::{Multipart, Path, Query},
http::{Response, StatusCode},
response::IntoResponse,
Extension, Json,
Json, response::IntoResponse,
};
use mime::Mime;
use serde_json::json;
use crate::web::service::{team as team_service, user_team as user_team_service};

fn can_modify_team(user: crate::model::user::Model, team_id: i64) -> bool {
return user.group == "admin" || user.teams.iter().any(|team| team.id == team_id && team.captain_id == user.id);
Expand Down
7 changes: 4 additions & 3 deletions src/server/controller/user.rs → src/web/controller/user.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use axum::{
Extension,
extract::{Multipart, Path, Query},
http::{Response, StatusCode},
response::IntoResponse,
Extension, Json,
Json, response::IntoResponse,
};
use mime::Mime;
use serde_json::json;

use crate::{server::service::user as user_service, traits::Ext, util::validate};
use crate::{traits::Ext, util::validate};
use crate::web::service::user as user_service;

/// **Find** can find user's information.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use serde_json::json;
use std::future::Future;
use std::pin::Pin;

use crate::server::service::user as user_service;
use crate::web::service::user as user_service;
use crate::{traits::Ext, util};

pub fn jwt(group: util::jwt::Group) -> impl Fn(Request<axum::body::Body>, Next) -> Pin<Box<dyn Future<Output = Result<Response, StatusCode>> + Send>> + Clone {
Expand Down
40 changes: 40 additions & 0 deletions src/web/middleware/frontend.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::{fs, path::PathBuf};

use axum::{
extract::Request,
http::{Response, StatusCode},
middleware::Next,
response::IntoResponse,
};

pub async fn serve(req: Request, next: Next) -> Result<axum::response::Response, StatusCode> {
let path: String = req.uri().path().to_string();

if path.starts_with("/api") {
return Ok(next.run(req).await);
}

let filepath = PathBuf::from("dist").join(path.strip_prefix("/").unwrap_or_default());

async fn index() -> Result<axum::response::Response, StatusCode> {
if let Ok(index_content) = fs::read_to_string(PathBuf::from("dist").join("index.html")) {
return Ok(Response::builder().status(StatusCode::OK).body(index_content).unwrap().into_response());
} else {
return Ok(Response::builder()
.status(StatusCode::NOT_FOUND)
.body("404 Not Found".to_string())
.unwrap()
.into_response());
}
}

if filepath == PathBuf::from("dist").join("index.html") {
return index().await;
}

if let Ok(content) = fs::read_to_string(&filepath) {
return Ok(Response::builder().status(StatusCode::OK).body(content).unwrap().into_response());
} else {
return index().await;
}
}
2 changes: 2 additions & 0 deletions src/web/middleware/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod auth;
pub mod frontend;
33 changes: 33 additions & 0 deletions src/web/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
pub mod controller;
pub mod middleware;
pub mod router;
pub mod service;

use std::sync::OnceLock;

use axum::{middleware::from_fn, Router};
use reqwest::Method;
use tower_http::{
cors::{Any, CorsLayer},
trace::TraceLayer,
};

static APP: OnceLock<Router> = OnceLock::new();

pub fn init() {
let cors = CorsLayer::new()
.allow_methods([Method::GET, Method::POST, Method::PUT, Method::DELETE, Method::OPTIONS])
.allow_headers(Any)
.allow_origin(Any);

let app: Router = Router::new()
.merge(Router::new().nest("/api", router::router()).layer(TraceLayer::new_for_http()))
.layer(from_fn(middleware::frontend::serve))
.layer(cors);

APP.set(app).unwrap();
}

pub fn get_app() -> Router {
return APP.get().unwrap().clone();
}
6 changes: 3 additions & 3 deletions src/server/router/category.rs → src/web/router/category.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use axum::{
middleware::from_fn,
routing::{delete, get, post, put},
Router,
routing::{delete, get, post, put},
};

use crate::server::{controller, middleware::auth};
use crate::web::controller;
use crate::web::middleware::auth;
use crate::util::jwt::Group;

pub fn router() -> Router {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use axum::{
extract::DefaultBodyLimit,
middleware::from_fn,
routing::{delete, get, post, put},
Router,
routing::{delete, get, post, put},
};

use crate::server::{controller, middleware::auth};
use crate::web::controller;
use crate::web::middleware::auth;
use crate::util::jwt::Group;

pub fn router() -> Router {
Expand Down
2 changes: 1 addition & 1 deletion src/server/router/config.rs → src/web/router/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use axum::{routing::get, Router};

use crate::server::controller;
use crate::web::controller;

pub fn router() -> Router {
return Router::new()
Expand Down
6 changes: 3 additions & 3 deletions src/server/router/game.rs → src/web/router/game.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use axum::{
extract::DefaultBodyLimit,
middleware::from_fn,
routing::{delete, get, post, put},
Router,
routing::{delete, get, post, put},
};

use crate::server::{controller, middleware::auth};
use crate::web::controller;
use crate::web::middleware::auth;
use crate::util::jwt::Group;

pub fn router() -> Router {
Expand Down
2 changes: 1 addition & 1 deletion src/server/router/media.rs → src/web/router/media.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use axum::{routing::get, Router};

use crate::server::controller;
use crate::web::controller;

pub fn router() -> Router {
return Router::new().route("/*path", get(controller::media::get_file));
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions src/server/router/pod.rs → src/web/router/pod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use axum::{
middleware::from_fn,
routing::{delete, get, post, put},
Router,
routing::{delete, get, post, put},
};

use crate::server::{controller, middleware::auth};
use crate::web::controller;
use crate::web::middleware::auth;
use crate::util::jwt::Group;

pub fn router() -> Router {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use axum::{
middleware::from_fn,
routing::{delete, get, post},
Router,
routing::{delete, get, post},
};

use crate::server::{controller, middleware::auth};
use crate::web::controller;
use crate::web::middleware::auth;
use crate::util::jwt::Group;

pub fn router() -> Router {
Expand Down
6 changes: 3 additions & 3 deletions src/server/router/team.rs → src/web/router/team.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use axum::{
extract::DefaultBodyLimit,
middleware::from_fn,
routing::{delete, get, post, put},
Router,
routing::{delete, get, post, put},
};

use crate::server::{controller, middleware::auth};
use crate::web::controller;
use crate::web::middleware::auth;
use crate::util::jwt::Group;

pub fn router() -> Router {
Expand Down
6 changes: 3 additions & 3 deletions src/server/router/user.rs → src/web/router/user.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use axum::{
extract::DefaultBodyLimit,
middleware::from_fn,
routing::{delete, get, post, put},
Router,
routing::{delete, get, post, put},
};

use crate::server::{controller, middleware::auth};
use crate::web::controller;
use crate::web::middleware::auth;
use crate::util::jwt::Group;

pub fn router() -> Router {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit e2ce4bc

Please sign in to comment.