Skip to content
This repository has been archived by the owner on Nov 3, 2024. It is now read-only.

Commit

Permalink
add: get postgres endpoint from env
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshihiro-shu committed Oct 21, 2023
1 parent 968e50e commit 11b62c2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
6 changes: 6 additions & 0 deletions batch/qiita/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=password
DB_NAME=postgres
DB_SSL=disable
1 change: 1 addition & 0 deletions batch/qiita/.envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dotenv
29 changes: 26 additions & 3 deletions batch/qiita/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
use std::error::Error;
use std::env;
use tokio; // tokioは非同期ランタイムです
use tokio_postgres::{NoTls};

mod qiita_response;
mod entity;

#[derive(Debug)]
struct Tag {
id: i32,
name: String,
}

fn construct_db_url() -> String {
// 環境変数から値を取得します。環境変数が設定されていない場合はデフォルト値を使用します。
let db_host = env::var("DB_HOST").unwrap_or_else(|_| "localhost".to_string());
let db_port = env::var("DB_PORT").unwrap_or_else(|_| "5432".to_string());
let db_user = env::var("DB_USER").unwrap_or_else(|_| "user".to_string());
let db_password = env::var("DB_PASSWORD").unwrap_or_else(|_| "password".to_string());
let db_name = env::var("DB_NAME").unwrap_or_else(|_| "database".to_string());
let db_ssl = env::var("DB_SSL").unwrap_or_else(|_| "disable".to_string());

// SSLの設定に基づいて、SSLモードを指定します。
let ssl_mode = match db_ssl.as_str() {
"disable" => "?sslmode=disable",
"require" => "?sslmode=require",
"prefer" => "?sslmode=prefer",
_ => "", // デフォルトのSSLモードを使用します(例:SSLモードが指定されていない場合や不明な値の場合)
};

// すべての情報を結合して、接続URLを形成します。
format!(
"postgresql://{}:{}@{}:{}/{}{}",
db_user, db_password, db_host, db_port, db_name, ssl_mode
)
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
Expand All @@ -33,10 +56,10 @@ async fn main() -> Result<(), Box<dyn Error>> {

let res: qiita_response::QiitaResponse = serde_json::from_str(&response_text).unwrap();

let db_endpoint = "postgresql://postgres:password@127.0.0.1:5432/postgres";
let db_endpoint = construct_db_url();

// 非同期接続
let (db_client, db_connection) = tokio_postgres::connect(db_endpoint, NoTls).await?;
let (db_client, db_connection) = tokio_postgres::connect(&db_endpoint, NoTls).await?;

// The connection object performs the actual communication with the database,
// so spawn it off to run on its own.
Expand Down

0 comments on commit 11b62c2

Please sign in to comment.