Skip to content

Commit

Permalink
Merge pull request #2 from fuki01/add-docker
Browse files Browse the repository at this point in the history
add docker
  • Loading branch information
fuki01 authored May 23, 2024
2 parents 490e614 + d7d1a71 commit b549ed1
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 13 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
/.DS_Store
/database.db
/database.db
docker/db/data/
mysql_data/
.dbdata/
30 changes: 25 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
package main

import (
"fmt"
"os"

"github.com/fuki01/onion-architecture/domain/task"
"github.com/fuki01/onion-architecture/infrastructure"
"github.com/fuki01/onion-architecture/infrastructure/config"
"github.com/fuki01/onion-architecture/presentation/controller"
"github.com/fuki01/onion-architecture/presentation/router"
"github.com/fuki01/onion-architecture/usecase"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"github.com/joho/godotenv"
)

func loadEnv(envfile string) {
err := godotenv.Load(envfile)
fmt.Println("err: ", err)
if err != nil {
panic("no env file")
}
}

func main() {
// データベース接続を設定
db, err := gorm.Open(sqlite.Open("database.db"), &gorm.Config{})
// 環境変数を読み込む
loadEnv(".env")
user := os.Getenv("DB_USER")
pass := os.Getenv("DB_PASS")
host := os.Getenv("DB_HOST")
dbname := os.Getenv("DB_NAME")

if user == "" || pass == "" || host == "" || dbname == "" {
panic("failed to load env")
}

db, err := config.NewDatabase(user, pass, host, dbname).Connect()
if err != nil {
panic("failed to connect database")
}
Expand All @@ -36,5 +57,4 @@ func main() {

// サーバーを起動
r.Run(":8080")

}
24 changes: 24 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
version: "3.3"
services:
db:
platform: linux/x86_64
image: mysql:5.7.22
environment:
MYSQL_DATABASE: taskdb
MYSQL_USER: root
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
volumes:
- .dbdata:/var/lib/mysql
ports:
- 33066:3306

backend:
build:
context: ./
dockerfile: ./docker/app/Dockerfile
ports:
- "8080:8080"
depends_on:
- db

13 changes: 13 additions & 0 deletions docker/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM golang:1.21.4

WORKDIR /app

COPY . .

RUN go mod download

RUN go build -o /app/main ./cmd/main.go

EXPOSE 8080

CMD ["/app/main"]
5 changes: 5 additions & 0 deletions docker/db/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM mysql

COPY my.cnf /etc/mysql/conf.d/my.cnf

CMD ["mysqld", "--character-set-server=utf8mb4", "--collation-server=utf8mb4_unicode_ci"]
6 changes: 6 additions & 0 deletions docker/db/my.cnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci

[client]
default-character-set=utf8mb4
2 changes: 1 addition & 1 deletion domain/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type User struct {

func (u *User) Validate() error {
if u.Name == "" {
return errors.New("invalid email format")
return errors.New("invalid name")
}
return nil
}
Expand Down
5 changes: 5 additions & 0 deletions env/dev.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ENV=local
DB_USER=root
DB_PASS=password
DB_HOST=db
DB_NAME=taskdb
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21.4
require (
github.com/gin-gonic/gin v1.10.0
github.com/stretchr/testify v1.9.0
gorm.io/driver/sqlite v1.5.5
gorm.io/driver/mysql v1.5.6
gorm.io/gorm v1.25.10
)

Expand All @@ -20,14 +20,15 @@ require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
Expand Down
11 changes: 7 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
Expand All @@ -32,6 +34,8 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
Expand All @@ -42,8 +46,6 @@ github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down Expand Up @@ -92,8 +94,9 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/sqlite v1.5.5 h1:7MDMtUZhV065SilG62E0MquljeArQZNfJnjd9i9gx3E=
gorm.io/driver/sqlite v1.5.5/go.mod h1:6NgQ7sQWAIFsPrJJl1lSNSu2TABh0ZZ/zm5fosATavE=
gorm.io/driver/mysql v1.5.6 h1:Ld4mkIickM+EliaQZQx3uOJDJHtrd70MxAUqWqlx3Y8=
gorm.io/driver/mysql v1.5.6/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM=
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
gorm.io/gorm v1.25.10 h1:dQpO+33KalOA+aFYGlK+EfxcI5MbO7EP2yYygwh9h+s=
gorm.io/gorm v1.25.10/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
Expand Down
54 changes: 54 additions & 0 deletions infrastructure/config/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package config

import (
"fmt"
"log"
"time"

"gorm.io/driver/mysql"
"gorm.io/gorm"
)

type Database struct {
User string
Pass string
Host string
DBName string
}

func NewDatabase(user, pass, host, dbname string) *Database {
return &Database{
User: user,
Pass: pass,
Host: host,
DBName: dbname,
}
}

func (d *Database) Connect() (*gorm.DB, error) {
connection := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=True&loc=Local", d.User, d.Pass, d.Host, d.DBName)
db, err := connectToDatabase(connection)
if err != nil {
return nil, err
}

return db, nil
}

func connectToDatabase(connection string) (*gorm.DB, error) {
var db *gorm.DB
var err error
maxAttempts := 5
interval := time.Second * 2

for attempts := 1; attempts <= maxAttempts; attempts++ {
db, err = gorm.Open(mysql.Open(connection), &gorm.Config{})
if err == nil {
return db, nil
}
log.Printf("Failed to connect to database (attempt %d): %v", attempts, err)
time.Sleep(interval)
}

return nil, fmt.Errorf("failed to connect to database after %d attempts", maxAttempts)
}

0 comments on commit b549ed1

Please sign in to comment.