-
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.
- Loading branch information
1 parent
001bde0
commit 40b04af
Showing
7 changed files
with
140 additions
and
3 deletions.
There are no files selected for viewing
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,6 @@ | ||
export DB_USERNAME="root" | ||
export DB_PASSWORD="password" | ||
export DB_HOSTNAME="localhost" | ||
export DB_PORT="3306" | ||
export DB_DATABASE="world" | ||
export RUST_LOG="debug" |
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 |
---|---|---|
|
@@ -17,3 +17,5 @@ Cargo.lock | |
# Added by cargo | ||
|
||
/target | ||
|
||
.env |
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,8 +1,31 @@ | ||
[package] | ||
name = "traO-Judge-backend" | ||
name = "trao-judge-backend" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
axum = "0.7" | ||
axum-extra = { version = "0.9", features = [ "typed-header" ] } | ||
tokio = { version = "1.0", features = ["full"] } | ||
serde = { version = "1.0", features = [ "derive" ] } | ||
serde_json = "1.0" | ||
tower = "0.4" | ||
tower-http = { version = "0.5.0", features = ["add-extension", "trace", "fs"] } | ||
tracing = "0.1" | ||
tracing-subscriber = { version = "0.3", features = ["env-filter"] } | ||
uuid = { version = "1.0", features = ["serde", "v4"] } | ||
reqwest = "0.12.5" | ||
anyhow = "1.0" | ||
thiserror = "1.0.63" | ||
validator = { version = "0.14.0", features = ["derive"] } | ||
bcrypt = "0.15" | ||
async-session = "3.0.0" | ||
sqlx = { version = "0.7", features = [ "mysql", "runtime-async-std-native-tls", "migrate" ] } | ||
|
||
[dependencies.async-sqlx-session] | ||
git = "https://github.com/maxcountryman/async-sqlx-session.git" | ||
default-features = false | ||
branch = "sqlx-0.7" | ||
features = ["mysql"] |
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,16 @@ | ||
services: | ||
mariadb: | ||
image: mariadb:latest | ||
environment: | ||
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD} | ||
MYSQL_DATABASE: ${DB_DATABASE} | ||
TZ: Asia/Tokyo | ||
expose: | ||
- 3306 | ||
ports: | ||
- 3306:3306 | ||
volumes: | ||
- mysql:/var/lib/mysql | ||
|
||
volumes: | ||
mysql: |
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,13 @@ | ||
use axum::{routing::get, Router}; | ||
|
||
use crate::repository::Repository; | ||
|
||
async fn pong() -> &'static str { | ||
"pong" | ||
} | ||
|
||
pub fn make_router(app_state: Repository) -> Router { | ||
let root_router = Router::new().route("/ping", get(pong)); | ||
|
||
Router::new().nest("/", root_router).with_state(app_state) | ||
} |
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,3 +1,27 @@ | ||
fn main() { | ||
println!("Hello, world!"); | ||
use anyhow::Ok; | ||
use repository::Repository; | ||
use tower_http::trace::TraceLayer; | ||
use tracing_subscriber::EnvFilter; | ||
|
||
mod handler; | ||
mod repository; | ||
|
||
#[tokio::main] | ||
async fn main() -> anyhow::Result<()> { | ||
tracing_subscriber::fmt() | ||
.with_env_filter(EnvFilter::try_from_default_env().unwrap_or("info".into())) | ||
.init(); | ||
|
||
let app_state = Repository::connect().await?; | ||
app_state.migrate().await?; | ||
|
||
let app = handler::make_router(app_state).layer(TraceLayer::new_for_http()); | ||
|
||
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await?; | ||
|
||
tracing::debug!("listening on {}", listener.local_addr()?); | ||
|
||
axum::serve(listener, app).await?; | ||
|
||
Ok(()) | ||
} |
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,53 @@ | ||
use async_sqlx_session::MySqlSessionStore; | ||
use sqlx::{ | ||
mysql::{MySqlConnectOptions, MySqlPoolOptions}, | ||
MySqlPool, | ||
}; | ||
|
||
#[derive(Clone)] | ||
pub struct Repository { | ||
pool: MySqlPool, | ||
session_store: MySqlSessionStore, | ||
} | ||
|
||
impl Repository { | ||
pub async fn connect() -> anyhow::Result<Self> { | ||
let options = get_option_from_env()?; | ||
|
||
let pool = MySqlPoolOptions::new() | ||
.max_connections(10) | ||
.connect_with(options) | ||
.await?; | ||
|
||
let session_store = | ||
MySqlSessionStore::from_client(pool.clone()).with_table_name("user_sessions"); | ||
|
||
Ok(Self { | ||
pool, | ||
session_store, | ||
}) | ||
} | ||
|
||
pub async fn migrate(&self) -> sqlx::Result<()> { | ||
sqlx::migrate!("./migrations").run(&self.pool).await?; | ||
|
||
self.session_store.migrate().await?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
fn get_option_from_env() -> anyhow::Result<MySqlConnectOptions> { | ||
let host = std::env::var("DB_HOSTNAME")?; | ||
let port = std::env::var("DB_PORT")?.parse()?; | ||
let user = std::env::var("DB_USERNAME")?; | ||
let password = std::env::var("DB_PASSWORD")?; | ||
let db_name = std::env::var("DB_DATABASE")?; | ||
|
||
Ok(MySqlConnectOptions::new() | ||
.host(&host) | ||
.port(port) | ||
.username(&user) | ||
.password(&password) | ||
.database(&db_name)) | ||
} |