-
Notifications
You must be signed in to change notification settings - Fork 2
エラー型に anyhow クレートを利用 + 非同期実行の構造を大幅に整理 #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b03fb2d
0d8f74c
9c01677
6c50595
8e1f5d2
6368ab9
f4d0237
3e68ecd
62350f4
afd4b03
f601ef6
c30a8ec
c4acb15
e509db7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DATABASE_URL= |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 }) | ||
} | ||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 元のコードではここで There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. そのコードと直接関係のある話ではないんですけど、 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 合ってるはずです!:+1: |
||
Ok(pool) | ||
} |
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
設定値は個別に持ち回すより構造体に入れておいたほうが便利なので、そのようにしてみました