Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DATABASE_URL=
64 changes: 39 additions & 25 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ rand = "0.8.0"
# TODO(magurotuna): Upgrade tokio to v1 after sqlx gets compatible with that
tokio = { version = "0.2.4", features = [ "full" ] }
hyper = "0.14"
anyhow = "1.0.37"
async-trait = "0.1.42"
futures = "0.3.12"
10 changes: 10 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use anyhow::Result;

pub struct Config {
pub database_url: String,
}

pub fn load_config() -> Result<Config> {
let database_url = dotenv::var("DATABASE_URL")?;
Ok(Config { database_url })
}
Comment on lines +3 to +10
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

設定値は個別に持ち回すより構造体に入れておいたほうが便利なので、そのようにしてみました

12 changes: 5 additions & 7 deletions src/db.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use dotenv::dotenv;
use crate::config::Config;
use anyhow::Result;
use sqlx::{mysql::MySqlPool, MySql};
use std::env;

pub async fn new_pool() -> Result<sqlx::Pool<MySql>, sqlx::Error> {
dotenv().ok();
let url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");

let pool = MySqlPool::connect(&url).await?;
pub type DbPool = sqlx::Pool<MySql>;

pub async fn new_pool(config: &Config) -> Result<DbPool> {
let pool = MySqlPool::connect(&config.database_url).await?;
Comment on lines +7 to +8
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

元のコードではここで dotenv::ok(); してから env::var で環境変数を読み取っていましたが、.env からの読み取りは config.rs に移行し、Config 構造体を引数として渡すようにしました。(副作用が少なくなって、扱いやすくなるはず)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そのコードと直接関係のある話ではないんですけど、MySqlPool の使い方ってそれで合っていますかね・・?
sqlx のドキュメントをみてもよくわからなかったのでとりあえずそのようにしたのですが、正しい使い方なのかどうかが分かっておらずでした・・。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

合ってるはずです!:+1:

Ok(pool)
}
68 changes: 0 additions & 68 deletions src/docker_lib.rs

This file was deleted.

24 changes: 0 additions & 24 deletions src/error.rs

This file was deleted.

Empty file removed src/judge.rs
Empty file.
5 changes: 0 additions & 5 deletions src/lib.rs

This file was deleted.

Loading