Skip to content

Commit 4d5ce58

Browse files
committed
バックエンドをec2デプロイ対応した
1 parent 66f86e9 commit 4d5ce58

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

ec2-docker-compose.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
services:
2+
# db:
3+
# image: mysql:8.0.32
4+
# platform: linux/amd64 # mac環境で必須なことがあるらしい
5+
# env_file: ./db/.env
6+
# container_name: v_kara_db
7+
# build:
8+
# context: ./db
9+
# healthcheck:
10+
# test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
11+
# timeout: 5s
12+
# retries: 10
13+
# ports:
14+
# - "3306:3306"
15+
# command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
16+
17+
api:
18+
image: v-kara-api:latest
19+
container_name: v-kara-api
20+
environment:
21+
- DEPLOY_ENV=EC2_DOCKER_COMPOSE
22+
- DEPLOY_DB_ENV=RDS
23+
- IS_DOCKER_COMPOSE=true
24+
env_file: api.env # これが無いと、上述のenvironment変数が無視される。なぜかは不明。
25+
ports:
26+
- "8080:8080"
27+
28+
app:
29+
image: v-kara-app:latest
30+
container_name: v-kara-app
31+
env_file: .env
32+
environment:
33+
- DEPLOY_ENV=EC2_DOCKER_COMPOSE
34+
- IS_DOCKER_COMPOSE=true
35+
ports:
36+
- "80:80"

t0016Go/infra/db.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ type SqlHandler struct {
2020

2121
// 似たような処理がSetListenerIdintoCookie()にもあるので、envを変更するときは注意
2222
func GetEnvVar() {
23+
fmt.Println("----環境変数取得:開始----")
2324
if common.IsOnCloud {
2425
//クラウド環境
2526
fmt.Println("クラウド環境で起動")
27+
if common.DEPLOY_ENV == "EC2_DOCKER_COMPOSE" && common.DEPLOY_DB_ENV == "RDS" {
28+
fmt.Println("EC2のdocker composeで起動")
29+
}
2630
} else if common.IsOnLocalWithDockerCompose {
2731
// ローカルのdocker上(compose使用)
2832
fmt.Println("ローカルのdockerコンテナ内で起動")
@@ -37,8 +41,8 @@ func GetEnvVar() {
3741
}
3842
}
3943

40-
guest := os.Getenv("GUEST_USER_NAME")
41-
fmt.Printf("「%v」の中身が空でなければ環境変数を読み込めてるはず。 \n", guest)
44+
fmt.Printf("guest:「%v」の中身が空でなければ環境変数の読み込みは成功してる\n", os.Getenv("GUEST_USER_NAME"))
45+
fmt.Println("----環境変数取得:終了----")
4246
}
4347

4448
func dbInit() database.SqlHandler {

t0016Go/interfaces/v1/controllers/common/env.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ import (
88
"github.com/sharin-sushi/0016go_next_relation/domain"
99
)
1010

11-
var goEnv = os.Getenv("GO_ENV") //ローカルpc上でのみ設定 =development と記載
12-
var isDockerCompose = os.Getenv("IS_DOCKER_COMPOSE") //docckercompos.ymlにのみ =true と記載
11+
// TODO: 無駄に複雑になってしまったのでシンプルにする
12+
var goEnv = os.Getenv("GO_ENV") // ローカルpc上でのみ設定 =development と記載
13+
var isDockerCompose = os.Getenv("IS_DOCKER_COMPOSE") // docker-compose.ymlにのみ =true と記載
14+
var DEPLOY_ENV = os.Getenv("DEPLOY_ENV") // EC2用 docker-compose.ymlにのみ =EC2_DOCKER_COMPOSE と記載
15+
var DEPLOY_DB_ENV = os.Getenv("DEPLOY_DB_ENV") // EC2用 docker-compose.ymlにのみ =RDS と記載
1316

14-
var IsOnCloud = (goEnv == "" && isDockerCompose == "")
17+
var IsOnCloud = (goEnv == "" && isDockerCompose == "") || (DEPLOY_ENV == "EC2_DOCKER_COMPOSE" && DEPLOY_DB_ENV == "RDS")
1518
var IsOnLocalWithDockerCompose = (goEnv == "" && isDockerCompose == "true")
1619
var IsOnLocalWithOutDockerCompose = (goEnv == "development" && isDockerCompose == "")
1720
var IsOnLocal = (IsOnLocalWithDockerCompose || IsOnLocalWithOutDockerCompose)

0 commit comments

Comments
 (0)