From d931bc9225bb5fc2f760730e67e79443445c49f0 Mon Sep 17 00:00:00 2001 From: shin0729 Date: Fri, 1 Mar 2024 21:51:21 +0900 Subject: [PATCH 01/14] =?UTF-8?q?feat:=20=E3=83=98=E3=83=AB=E3=82=B9?= =?UTF-8?q?=E3=83=81=E3=82=A7=E3=83=83=E3=82=AF=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typing-server/api/controller/system/health.go | 11 +++++++++++ typing-server/api/presenter/server.go | 11 +++++++++++ 2 files changed, 22 insertions(+) create mode 100644 typing-server/api/controller/system/health.go create mode 100644 typing-server/api/presenter/server.go diff --git a/typing-server/api/controller/system/health.go b/typing-server/api/controller/system/health.go new file mode 100644 index 0000000..a28b06a --- /dev/null +++ b/typing-server/api/controller/system/health.go @@ -0,0 +1,11 @@ +package system + +import ( + "net/http" +) + +// HealthCheck はヘルスチェックのためのハンドラー関数です。 +func HealthCheck(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.Write([]byte("API is running")) +} diff --git a/typing-server/api/presenter/server.go b/typing-server/api/presenter/server.go new file mode 100644 index 0000000..e921a4e --- /dev/null +++ b/typing-server/api/presenter/server.go @@ -0,0 +1,11 @@ +package presenter + +import ( + "net/http" + + "github.com/su-its/typing/typing-server/api/controller/system" +) + +func RegisterRoutes() { + http.HandleFunc("/health", system.HealthCheck) +} From e81b84c1b73857b4b6c01a9480810588f6e9d2c1 Mon Sep 17 00:00:00 2001 From: shin0729 Date: Fri, 1 Mar 2024 21:51:31 +0900 Subject: [PATCH 02/14] =?UTF-8?q?feat:=20=E3=82=A8=E3=83=B3=E3=83=88?= =?UTF-8?q?=E3=83=AA=E3=83=9D=E3=82=A4=E3=83=B3=E3=83=88=E3=82=92=E5=AE=9F?= =?UTF-8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typing-server/api/cmd/main.go | 53 +++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 typing-server/api/cmd/main.go diff --git a/typing-server/api/cmd/main.go b/typing-server/api/cmd/main.go new file mode 100644 index 0000000..3dfe706 --- /dev/null +++ b/typing-server/api/cmd/main.go @@ -0,0 +1,53 @@ +package main + +import ( + "context" + "fmt" + "log/slog" + "net/http" + "time" + + "github.com/go-sql-driver/mysql" + "github.com/su-its/typing/typing-server/api/presenter" + "github.com/su-its/typing/typing-server/domain/repository/ent" +) + +func main() { + logger := slog.Default() + + jst, err := time.LoadLocation("Asia/Tokyo") + if err != nil { + logger.Error("failed to load location", fmt.Errorf("error: %w", err)) + } + + mysqlConfig := &mysql.Config{ + DBName: "typing-db", + User: "root", + Passwd: "password", + Addr: "localhost:3306", + Net: "tcp", + ParseTime: true, + Loc: jst, + } + + entClient, err := ent.Open("mysql", mysqlConfig.FormatDSN()) + if err != nil { + logger.Error("failed to open ent client", fmt.Errorf("error: %w", err)) + } + + logger.Info("ent client is opened") + + if err := entClient.Schema.Create(context.Background()); err != nil { + logger.Error("failed to create schema", fmt.Errorf("error: %w", err)) + } + + logger.Info("schema is created") + + presenter.RegisterRoutes() + go func() { + logger.Info("server is running") + if err := http.ListenAndServe(":8080", nil); err != nil { + logger.Error("failed to listen and serve", fmt.Errorf("error: %w", err)) + } + }() +} From 281328b3c2c4a0da0e1115cf401181f0359c32de Mon Sep 17 00:00:00 2001 From: shin0729 Date: Fri, 1 Mar 2024 21:52:10 +0900 Subject: [PATCH 03/14] =?UTF-8?q?ci:=20docker=E5=91=A8=E3=82=8A=E3=82=92?= =?UTF-8?q?=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typing-server/Dockerfile | 29 ++++++++++++++++++++++++++++ typing-server/docker-compose.dev.yml | 28 +++++++++++++++++++++++++++ typing-server/docker-compose.yml | 28 +++++++++++++++++++++++++++ typing-server/go.mod | 1 + typing-server/go.sum | 2 ++ 5 files changed, 88 insertions(+) create mode 100644 typing-server/Dockerfile create mode 100644 typing-server/docker-compose.dev.yml create mode 100644 typing-server/docker-compose.yml diff --git a/typing-server/Dockerfile b/typing-server/Dockerfile new file mode 100644 index 0000000..ea4df5a --- /dev/null +++ b/typing-server/Dockerfile @@ -0,0 +1,29 @@ +# 基本イメージ +FROM golang:1.22.0 as builder + +# 作業ディレクトリを設定 +WORKDIR /app + +# ソースコードをコピー +COPY . . + +# 依存関係をインストール +RUN go mod download + +# アプリケーションをビルド +RUN CGO_ENABLED=0 GOOS=linux go build -v -o server ./api/cmd/main.go + +# 実行イメージ +FROM alpine:latest +RUN apk --no-cache add ca-certificates + +# tzdataパッケージのインストール +RUN apk --no-cache add tzdata + +WORKDIR /root + +# ビルドしたバイナリをコピー +COPY --from=builder /app/server . + +# アプリケーションの実行 +CMD ["./server"] diff --git a/typing-server/docker-compose.dev.yml b/typing-server/docker-compose.dev.yml new file mode 100644 index 0000000..a2a1b09 --- /dev/null +++ b/typing-server/docker-compose.dev.yml @@ -0,0 +1,28 @@ +version: '2.8' +services: + db: + image: mysql:8.3.0 + environment: + MYSQL_ROOT_PASSWORD: rootpassword + MYSQL_DATABASE: typing-db + MYSQL_USER: root + MYSQL_PASSWORD: password + ports: + - "3305:3306" + volumes: + - db-data:/var/lib/mysql + api: + build: + context: . + dockerfile: Dockerfile + volumes: + - ./api:/app + ports: + - "8079:8080" + environment: + - DB_HOST=db + - DB_USER=root + - DB_PASSWORD=password + - DB_NAME=devdb +volumes: + db-data: diff --git a/typing-server/docker-compose.yml b/typing-server/docker-compose.yml new file mode 100644 index 0000000..a2a1b09 --- /dev/null +++ b/typing-server/docker-compose.yml @@ -0,0 +1,28 @@ +version: '2.8' +services: + db: + image: mysql:8.3.0 + environment: + MYSQL_ROOT_PASSWORD: rootpassword + MYSQL_DATABASE: typing-db + MYSQL_USER: root + MYSQL_PASSWORD: password + ports: + - "3305:3306" + volumes: + - db-data:/var/lib/mysql + api: + build: + context: . + dockerfile: Dockerfile + volumes: + - ./api:/app + ports: + - "8079:8080" + environment: + - DB_HOST=db + - DB_USER=root + - DB_PASSWORD=password + - DB_NAME=devdb +volumes: + db-data: diff --git a/typing-server/go.mod b/typing-server/go.mod index 5130d9a..4d5c904 100644 --- a/typing-server/go.mod +++ b/typing-server/go.mod @@ -4,6 +4,7 @@ go 1.22.0 require ( entgo.io/ent v0.13.1 + github.com/go-sql-driver/mysql v1.7.0 github.com/google/uuid v1.6.0 ) diff --git a/typing-server/go.sum b/typing-server/go.sum index 89035ab..a1a1a0e 100644 --- a/typing-server/go.sum +++ b/typing-server/go.sum @@ -12,6 +12,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-openapi/inflect v0.19.0 h1:9jCH9scKIbHeV9m12SmPilScz6krDxKRasNNSNPXu/4= github.com/go-openapi/inflect v0.19.0/go.mod h1:lHpZVlpIQqLyKwJ4N+YSc9hchQy/i12fJykb83CRBH4= +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/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68= github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= From 9277aa53be57c3983b6489090f064ebe4824075d Mon Sep 17 00:00:00 2001 From: shin0729 Date: Fri, 1 Mar 2024 22:19:14 +0900 Subject: [PATCH 04/14] =?UTF-8?q?ci:=20DB=E3=81=A8API=E3=81=AE=E8=A8=AD?= =?UTF-8?q?=E5=AE=9A=E3=82=92=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typing-server/api/cmd/main.go | 17 ++++++----- typing-server/docker-compose.dev.yml | 43 +++++++++++++++++----------- typing-server/docker-compose.yml | 43 +++++++++++++++++----------- 3 files changed, 60 insertions(+), 43 deletions(-) diff --git a/typing-server/api/cmd/main.go b/typing-server/api/cmd/main.go index 3dfe706..1e6d245 100644 --- a/typing-server/api/cmd/main.go +++ b/typing-server/api/cmd/main.go @@ -22,27 +22,26 @@ func main() { mysqlConfig := &mysql.Config{ DBName: "typing-db", - User: "root", - Passwd: "password", - Addr: "localhost:3306", - Net: "tcp", + User: "user", + Passwd: "password", // 環境変数から取得するか、直接指定 + Addr: "db:33061", // Docker Compose内でのサービス名とポート ParseTime: true, Loc: jst, } - + entClient, err := ent.Open("mysql", mysqlConfig.FormatDSN()) if err != nil { logger.Error("failed to open ent client", fmt.Errorf("error: %w", err)) + } else { + logger.Info("ent client is opened") } - logger.Info("ent client is opened") - if err := entClient.Schema.Create(context.Background()); err != nil { logger.Error("failed to create schema", fmt.Errorf("error: %w", err)) + } else { + logger.Info("schema is created") } - logger.Info("schema is created") - presenter.RegisterRoutes() go func() { logger.Info("server is running") diff --git a/typing-server/docker-compose.dev.yml b/typing-server/docker-compose.dev.yml index a2a1b09..880a788 100644 --- a/typing-server/docker-compose.dev.yml +++ b/typing-server/docker-compose.dev.yml @@ -1,16 +1,4 @@ -version: '2.8' services: - db: - image: mysql:8.3.0 - environment: - MYSQL_ROOT_PASSWORD: rootpassword - MYSQL_DATABASE: typing-db - MYSQL_USER: root - MYSQL_PASSWORD: password - ports: - - "3305:3306" - volumes: - - db-data:/var/lib/mysql api: build: context: . @@ -18,11 +6,32 @@ services: volumes: - ./api:/app ports: - - "8079:8080" + - "8080:8080" + networks: + app_net: + ipv4_address: '172.28.1.3' + extra_hosts: + - 'db:172.28.1.5' + db: + image: mysql:8.3.0 environment: - - DB_HOST=db - - DB_USER=root - - DB_PASSWORD=password - - DB_NAME=devdb + MYSQL_DATABASE: typing-db + MYSQL_USER: user + MYSQL_PASSWORD: password + MYSQL_ROOT_PASSWORD: password + ports: + - "3306:3306" + volumes: + - db-data:/var/lib/mysql + networks: + app_net: + ipv4_address: '172.28.1.5' volumes: db-data: +networks: + app_net: + driver: bridge + ipam: + driver: default + config: + - subnet: 172.28.1.0/24 \ No newline at end of file diff --git a/typing-server/docker-compose.yml b/typing-server/docker-compose.yml index a2a1b09..d720e68 100644 --- a/typing-server/docker-compose.yml +++ b/typing-server/docker-compose.yml @@ -1,16 +1,4 @@ -version: '2.8' services: - db: - image: mysql:8.3.0 - environment: - MYSQL_ROOT_PASSWORD: rootpassword - MYSQL_DATABASE: typing-db - MYSQL_USER: root - MYSQL_PASSWORD: password - ports: - - "3305:3306" - volumes: - - db-data:/var/lib/mysql api: build: context: . @@ -18,11 +6,32 @@ services: volumes: - ./api:/app ports: - - "8079:8080" + - "8080:8080" + networks: + app_net: + ipv4_address: '172.28.1.3' + extra_hosts: + - 'db:172.28.1.5' + db: + image: mysql:8.3.0 environment: - - DB_HOST=db - - DB_USER=root - - DB_PASSWORD=password - - DB_NAME=devdb + MYSQL_DATABASE: typing-db + MYSQL_USER: user + MYSQL_PASSWORD: password + MYSQL_ROOT_PASSWORD: password + ports: + - "33061:3306" + volumes: + - db-data:/var/lib/mysql + networks: + app_net: + ipv4_address: '172.28.1.5' volumes: db-data: +networks: + app_net: + driver: bridge + ipam: + driver: default + config: + - subnet: 172.28.1.0/24 \ No newline at end of file From 95e64819cb4c442664c947cb7d6a5bd215829170 Mon Sep 17 00:00:00 2001 From: shin0729 Date: Sat, 2 Mar 2024 14:31:05 +0900 Subject: [PATCH 05/14] =?UTF-8?q?feat:=E3=83=9D=E3=83=BC=E3=83=88=E7=95=AA?= =?UTF-8?q?=E5=8F=B7=E3=82=92=E5=A4=89=E6=9B=B4=E3=81=97=E3=81=BE=E3=81=97?= =?UTF-8?q?=E3=81=9F=EF=BC=8E=E3=81=93=E3=82=8C=E3=81=AF=E7=AB=B6=E5=90=88?= =?UTF-8?q?=E3=82=92=E9=81=BF=E3=81=91=E3=82=8B=E3=81=9F=E3=82=81=E3=81=A7?= =?UTF-8?q?=E3=81=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typing-server/api/cmd/main.go | 7 ++++--- typing-server/docker-compose.dev.yml | 4 +++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/typing-server/api/cmd/main.go b/typing-server/api/cmd/main.go index 1e6d245..d195e0a 100644 --- a/typing-server/api/cmd/main.go +++ b/typing-server/api/cmd/main.go @@ -23,12 +23,13 @@ func main() { mysqlConfig := &mysql.Config{ DBName: "typing-db", User: "user", - Passwd: "password", // 環境変数から取得するか、直接指定 - Addr: "db:33061", // Docker Compose内でのサービス名とポート + Passwd: "password", // 環境変数から取得するか、直接指定 + Net: "tcp", + Addr: "db:3306", // Docker Compose内でのサービス名とポート ParseTime: true, Loc: jst, } - + entClient, err := ent.Open("mysql", mysqlConfig.FormatDSN()) if err != nil { logger.Error("failed to open ent client", fmt.Errorf("error: %w", err)) diff --git a/typing-server/docker-compose.dev.yml b/typing-server/docker-compose.dev.yml index 880a788..3e35dea 100644 --- a/typing-server/docker-compose.dev.yml +++ b/typing-server/docker-compose.dev.yml @@ -1,5 +1,7 @@ services: api: + depends_on: + - db build: context: . dockerfile: Dockerfile @@ -20,7 +22,7 @@ services: MYSQL_PASSWORD: password MYSQL_ROOT_PASSWORD: password ports: - - "3306:3306" + - "3307:3306" volumes: - db-data:/var/lib/mysql networks: From 31a38963d674f10ab8a43c97554e7685f8e77c2f Mon Sep 17 00:00:00 2001 From: shin0729 Date: Sat, 2 Mar 2024 14:32:17 +0900 Subject: [PATCH 06/14] =?UTF-8?q?feat:DB=E3=81=AE=E3=82=BB=E3=83=83?= =?UTF-8?q?=E3=83=88=E3=82=A2=E3=83=83=E3=83=97=E3=82=92=E5=BE=85=E3=81=A4?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=ABdepends=20on=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0=E3=81=97=E3=81=BE=E3=81=97=E3=81=9F=EF=BC=8E=E5=90=8C?= =?UTF-8?q?=E6=A7=98=E3=81=AE=E7=90=86=E7=94=B1=E3=81=A7=E3=83=98=E3=83=AB?= =?UTF-8?q?=E3=82=B9=E3=83=81=E3=82=A7=E3=83=83=E3=82=AF=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0=E3=81=97=E3=81=BE=E3=81=97=E3=81=9F=EF=BC=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typing-server/docker-compose.dev.yml | 9 ++++++++- typing-server/docker-compose.yml | 11 ++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/typing-server/docker-compose.dev.yml b/typing-server/docker-compose.dev.yml index 3e35dea..18cec0a 100644 --- a/typing-server/docker-compose.dev.yml +++ b/typing-server/docker-compose.dev.yml @@ -1,7 +1,8 @@ services: api: depends_on: - - db + db: + condition: service_healthy build: context: . dockerfile: Dockerfile @@ -15,6 +16,12 @@ services: extra_hosts: - 'db:172.28.1.5' db: + healthcheck: + test: ["CMD", "mysqladmin", "ping", "-h", "localhost"] + interval: 10s + retries: 10 + start_period: 30s + image: mysql:8.3.0 environment: MYSQL_DATABASE: typing-db diff --git a/typing-server/docker-compose.yml b/typing-server/docker-compose.yml index d720e68..18cec0a 100644 --- a/typing-server/docker-compose.yml +++ b/typing-server/docker-compose.yml @@ -1,5 +1,8 @@ services: api: + depends_on: + db: + condition: service_healthy build: context: . dockerfile: Dockerfile @@ -13,6 +16,12 @@ services: extra_hosts: - 'db:172.28.1.5' db: + healthcheck: + test: ["CMD", "mysqladmin", "ping", "-h", "localhost"] + interval: 10s + retries: 10 + start_period: 30s + image: mysql:8.3.0 environment: MYSQL_DATABASE: typing-db @@ -20,7 +29,7 @@ services: MYSQL_PASSWORD: password MYSQL_ROOT_PASSWORD: password ports: - - "33061:3306" + - "3307:3306" volumes: - db-data:/var/lib/mysql networks: From 87d63d7840b258646f1552db9fc60097e325b442 Mon Sep 17 00:00:00 2001 From: shin0729 Date: Sat, 2 Mar 2024 14:49:48 +0900 Subject: [PATCH 07/14] =?UTF-8?q?feat:w.Write=E3=81=AE=E3=82=A8=E3=83=A9?= =?UTF-8?q?=E3=83=BC=E3=83=8F=E3=83=B3=E3=83=89=E3=83=AA=E3=83=B3=E3=82=B0?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0=E3=81=97=E3=81=BE=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typing-server/api/controller/system/health.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/typing-server/api/controller/system/health.go b/typing-server/api/controller/system/health.go index a28b06a..91be079 100644 --- a/typing-server/api/controller/system/health.go +++ b/typing-server/api/controller/system/health.go @@ -1,11 +1,20 @@ package system import ( + "log" "net/http" ) // HealthCheck はヘルスチェックのためのハンドラー関数です。 func HealthCheck(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) - w.Write([]byte("API is running")) + _, err := w.Write([]byte("API is running")) + if err != nil { + // エラーログを記録し、処理を終了します。 + // 実際には、この時点でレスポンスヘッダーやボディがクライアントに送信されている可能性が高いため、 + // http.Errorを呼び出すことは推奨されません。 + // 代わりに、ログに記録するなどのサーバー側での対応が適切です。 + log.Printf("failed to write response: %v", err) + } } + From 14450226a3600879902b9c736694ab2437cb09d7 Mon Sep 17 00:00:00 2001 From: shin0729 Date: Sat, 2 Mar 2024 15:24:36 +0900 Subject: [PATCH 08/14] =?UTF-8?q?feat:HTTP=E3=82=B5=E3=83=BC=E3=83=90?= =?UTF-8?q?=E3=83=BC=E3=81=AE=E9=9D=9E=E5=90=8C=E6=9C=9F=E8=B5=B7=E5=8B=95?= =?UTF-8?q?=E3=81=A8=E7=B5=82=E4=BA=86=E5=BE=85=E6=A9=9F=E3=81=AE=E5=AE=9F?= =?UTF-8?q?=E8=A3=85=E3=80=81=E5=8F=8A=E3=81=B3=E3=83=AD=E3=82=B0=E5=87=BA?= =?UTF-8?q?=E5=8A=9B=E3=81=AE=E5=BC=B7=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typing-server/api/cmd/main.go | 53 ++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/typing-server/api/cmd/main.go b/typing-server/api/cmd/main.go index d195e0a..bd875fb 100644 --- a/typing-server/api/cmd/main.go +++ b/typing-server/api/cmd/main.go @@ -2,9 +2,9 @@ package main import ( "context" - "fmt" - "log/slog" + "log" "net/http" + "sync" "time" "github.com/go-sql-driver/mysql" @@ -13,41 +13,60 @@ import ( ) func main() { - logger := slog.Default() + // 標準のログパッケージを使用 + logger := log.Default() + // タイムゾーンの設定 jst, err := time.LoadLocation("Asia/Tokyo") if err != nil { - logger.Error("failed to load location", fmt.Errorf("error: %w", err)) + logger.Printf("failed to load location: %v", err) + return } + // MySQLの接続設定 mysqlConfig := &mysql.Config{ - DBName: "typing-db", - User: "user", - Passwd: "password", // 環境変数から取得するか、直接指定 - Net: "tcp", - Addr: "db:3306", // Docker Compose内でのサービス名とポート - ParseTime: true, - Loc: jst, + DBName: "typing-db", // データベース名 + User: "user", // ユーザー名 + Passwd: "password", // パスワード + Net: "tcp", // ネットワークタイプ + Addr: "db:3306", // アドレス(Docker Compose内でのサービス名とポート) + ParseTime: true, // 時刻をtime.Timeで解析する + Loc: jst, // タイムゾーン } + // entクライアントの初期化 entClient, err := ent.Open("mysql", mysqlConfig.FormatDSN()) if err != nil { - logger.Error("failed to open ent client", fmt.Errorf("error: %w", err)) + logger.Printf("failed to open ent client: %v", err) + return } else { - logger.Info("ent client is opened") + logger.Println("ent client is opened") } + // スキーマの作成 if err := entClient.Schema.Create(context.Background()); err != nil { - logger.Error("failed to create schema", fmt.Errorf("error: %w", err)) + logger.Printf("failed to create schema: %v", err) + return } else { - logger.Info("schema is created") + logger.Println("schema is created") } + // ルートの登録 presenter.RegisterRoutes() + + // WaitGroupの宣言 + var wg sync.WaitGroup + + // HTTPサーバーの非同期起動 + wg.Add(1) go func() { - logger.Info("server is running") + defer wg.Done() // 関数終了時にWaitGroupをデクリメント + + logger.Println("server is running at http://localhost:8080") if err := http.ListenAndServe(":8080", nil); err != nil { - logger.Error("failed to listen and serve", fmt.Errorf("error: %w", err)) + logger.Printf("failed to listen and serve: %v", err) } }() + + wg.Wait() // HTTPサーバーの終了を待機 } From 8882235659e0b244300a49427e3af7ff92aa72ab Mon Sep 17 00:00:00 2001 From: shin0729 Date: Sat, 2 Mar 2024 18:08:24 +0900 Subject: [PATCH 09/14] =?UTF-8?q?refactor:=20log=E3=82=92slog=E3=81=AB?= =?UTF-8?q?=E7=BD=AE=E3=81=8D=E6=8F=9B=E3=81=88=E3=80=81=E3=83=AD=E3=82=B0?= =?UTF-8?q?=E5=87=BA=E5=8A=9B=E3=82=92=E3=82=A8=E3=83=A9=E3=83=BC=E3=83=8F?= =?UTF-8?q?=E3=83=B3=E3=83=89=E3=83=AA=E3=83=B3=E3=82=B0=E3=81=AE=E5=A4=96?= =?UTF-8?q?=E3=81=AB=E7=A7=BB=E5=8B=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typing-server/api/cmd/main.go | 24 ++++++++++++------------ typing-server/docker-compose.dev.yml | 3 ++- typing-server/docker-compose.yml | 3 ++- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/typing-server/api/cmd/main.go b/typing-server/api/cmd/main.go index bd875fb..2ec2ab6 100644 --- a/typing-server/api/cmd/main.go +++ b/typing-server/api/cmd/main.go @@ -2,7 +2,7 @@ package main import ( "context" - "log" + "log/slog" "net/http" "sync" "time" @@ -14,12 +14,12 @@ import ( func main() { // 標準のログパッケージを使用 - logger := log.Default() + logger := slog.Default() // タイムゾーンの設定 jst, err := time.LoadLocation("Asia/Tokyo") if err != nil { - logger.Printf("failed to load location: %v", err) + logger.Error("failed to load location: %v", err) return } @@ -37,19 +37,19 @@ func main() { // entクライアントの初期化 entClient, err := ent.Open("mysql", mysqlConfig.FormatDSN()) if err != nil { - logger.Printf("failed to open ent client: %v", err) + logger.Error("failed to open ent client: %v", err) return - } else { - logger.Println("ent client is opened") } + logger.Info("ent client is opened") + + defer entClient.Close() // スキーマの作成 if err := entClient.Schema.Create(context.Background()); err != nil { - logger.Printf("failed to create schema: %v", err) + logger.Error("failed to create schema: %v", err) return - } else { - logger.Println("schema is created") } + logger.Info("schema is created") // ルートの登録 presenter.RegisterRoutes() @@ -61,10 +61,10 @@ func main() { wg.Add(1) go func() { defer wg.Done() // 関数終了時にWaitGroupをデクリメント - - logger.Println("server is running at http://localhost:8080") + logger.Info("server is running at http://localhost:8080") if err := http.ListenAndServe(":8080", nil); err != nil { - logger.Printf("failed to listen and serve: %v", err) + logger.Error("failed to listen and serve: %v", err) + return } }() diff --git a/typing-server/docker-compose.dev.yml b/typing-server/docker-compose.dev.yml index 18cec0a..d21b1b6 100644 --- a/typing-server/docker-compose.dev.yml +++ b/typing-server/docker-compose.dev.yml @@ -43,4 +43,5 @@ networks: ipam: driver: default config: - - subnet: 172.28.1.0/24 \ No newline at end of file + - subnet: 172.28.1.0/24 + \ No newline at end of file diff --git a/typing-server/docker-compose.yml b/typing-server/docker-compose.yml index 18cec0a..d21b1b6 100644 --- a/typing-server/docker-compose.yml +++ b/typing-server/docker-compose.yml @@ -43,4 +43,5 @@ networks: ipam: driver: default config: - - subnet: 172.28.1.0/24 \ No newline at end of file + - subnet: 172.28.1.0/24 + \ No newline at end of file From d9ae516c912c9c5028794819740ede6e7709f0d8 Mon Sep 17 00:00:00 2001 From: shin0729 Date: Sat, 2 Mar 2024 18:12:35 +0900 Subject: [PATCH 10/14] =?UTF-8?q?refactor:slog=E3=81=AB=E5=A4=89=E6=9B=B4?= =?UTF-8?q?=E3=81=97=E3=81=BE=E3=81=97=E3=81=9F2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typing-server/api/controller/system/health.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/typing-server/api/controller/system/health.go b/typing-server/api/controller/system/health.go index 91be079..937ac73 100644 --- a/typing-server/api/controller/system/health.go +++ b/typing-server/api/controller/system/health.go @@ -1,7 +1,7 @@ package system import ( - "log" + "log/slog" "net/http" ) @@ -14,7 +14,7 @@ func HealthCheck(w http.ResponseWriter, r *http.Request) { // 実際には、この時点でレスポンスヘッダーやボディがクライアントに送信されている可能性が高いため、 // http.Errorを呼び出すことは推奨されません。 // 代わりに、ログに記録するなどのサーバー側での対応が適切です。 - log.Printf("failed to write response: %v", err) + slog.Error("failed to write response: %v", err) } } From e8e3202f5253a959b256ba32e5faa463e58c7b9d Mon Sep 17 00:00:00 2001 From: shin0729 Date: Sat, 2 Mar 2024 18:17:19 +0900 Subject: [PATCH 11/14] =?UTF-8?q?chore:=E6=96=87=E6=9C=AB=E3=81=AE?= =?UTF-8?q?=E3=82=B9=E3=83=9A=E3=83=BC=E3=82=B9=E3=82=92=E5=89=8A=E9=99=A4?= =?UTF-8?q?=E3=81=97=E3=81=BE=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typing-server/docker-compose.dev.yml | 1 - typing-server/docker-compose.yml | 1 - 2 files changed, 2 deletions(-) diff --git a/typing-server/docker-compose.dev.yml b/typing-server/docker-compose.dev.yml index d21b1b6..629343f 100644 --- a/typing-server/docker-compose.dev.yml +++ b/typing-server/docker-compose.dev.yml @@ -44,4 +44,3 @@ networks: driver: default config: - subnet: 172.28.1.0/24 - \ No newline at end of file diff --git a/typing-server/docker-compose.yml b/typing-server/docker-compose.yml index d21b1b6..629343f 100644 --- a/typing-server/docker-compose.yml +++ b/typing-server/docker-compose.yml @@ -44,4 +44,3 @@ networks: driver: default config: - subnet: 172.28.1.0/24 - \ No newline at end of file From 4541eeb92d0580c1dd563bb0f0e197bcf5acbc92 Mon Sep 17 00:00:00 2001 From: shin0729 Date: Sat, 2 Mar 2024 18:29:35 +0900 Subject: [PATCH 12/14] =?UTF-8?q?feat=20:=E3=82=B7=E3=82=B0=E3=83=8A?= =?UTF-8?q?=E3=83=AB=E3=83=8F=E3=83=B3=E3=83=89=E3=83=AA=E3=83=B3=E3=82=B0?= =?UTF-8?q?=E3=82=92=E5=AE=9F=E8=A3=85=E3=81=97=E3=81=BE=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typing-server/api/cmd/main.go | 38 +++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/typing-server/api/cmd/main.go b/typing-server/api/cmd/main.go index 2ec2ab6..dbdc9ac 100644 --- a/typing-server/api/cmd/main.go +++ b/typing-server/api/cmd/main.go @@ -4,7 +4,10 @@ import ( "context" "log/slog" "net/http" + "os" + "os/signal" "sync" + "syscall" "time" "github.com/go-sql-driver/mysql" @@ -56,17 +59,40 @@ func main() { // WaitGroupの宣言 var wg sync.WaitGroup - + // エラーを通知するためのチャネル + errChan := make(chan error, 1) + // シグナルハンドリングの準備 + sigChan := make(chan os.Signal, 1) + signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) // HTTPサーバーの非同期起動 wg.Add(1) go func() { defer wg.Done() // 関数終了時にWaitGroupをデクリメント - logger.Info("server is running at http://localhost:8080") - if err := http.ListenAndServe(":8080", nil); err != nil { - logger.Error("failed to listen and serve: %v", err) - return + // サーバーの設定 + server := &http.Server{Addr: ":8080"} + // 非同期でサーバーを開始 + go func() { + logger.Info("server is running at Addr :8080") + if err := server.ListenAndServe(); err != http.ErrServerClosed { + logger.Error("failed to listen and serve: %v", err) + errChan <- err // エラーをチャネルに送信 + } + }() + // シグナルを待機 + <-sigChan + logger.Info("shutting down the server...") + if err := server.Shutdown(nil); err != nil { + logger.Error("error during server shutdown: %v", err) + errChan <- err // エラーをチャネルに送信 } }() - + select { + case <-errChan: // エラーが発生した場合 + logger.Error("server stopped due to an error") + case sig := <-sigChan: // シグナルを受信した場合 + logger.Info("received signal: %s", sig) + } wg.Wait() // HTTPサーバーの終了を待機 + close(errChan) + logger.Info("server exited") } From bddc5f90df1d9acdc7c9ed11739921e3446ef31c Mon Sep 17 00:00:00 2001 From: shin0729 Date: Sat, 2 Mar 2024 18:31:25 +0900 Subject: [PATCH 13/14] =?UTF-8?q?refactor:=E4=B8=8D=E8=A6=81=E3=81=AA?= =?UTF-8?q?=E8=A1=8C=E3=82=92=E5=89=8A=E9=99=A4=E3=81=97=E3=81=BE=E3=81=97?= =?UTF-8?q?=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typing-server/api/cmd/main.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/typing-server/api/cmd/main.go b/typing-server/api/cmd/main.go index dbdc9ac..5d6430f 100644 --- a/typing-server/api/cmd/main.go +++ b/typing-server/api/cmd/main.go @@ -16,7 +16,6 @@ import ( ) func main() { - // 標準のログパッケージを使用 logger := slog.Default() // タイムゾーンの設定 @@ -43,9 +42,8 @@ func main() { logger.Error("failed to open ent client: %v", err) return } - logger.Info("ent client is opened") - defer entClient.Close() + logger.Info("ent client is opened") // スキーマの作成 if err := entClient.Schema.Create(context.Background()); err != nil { From f9bc1306a444041cc13585ec3542c0b1c4b8dd07 Mon Sep 17 00:00:00 2001 From: shin0729 Date: Sat, 2 Mar 2024 18:36:49 +0900 Subject: [PATCH 14/14] =?UTF-8?q?feat:=20HTTP=E3=82=B5=E3=83=BC=E3=83=90?= =?UTF-8?q?=E3=83=BC=E3=81=AE=E3=82=B7=E3=83=A3=E3=83=83=E3=83=88=E3=83=80?= =?UTF-8?q?=E3=82=A6=E3=83=B3=E5=87=A6=E7=90=86=E3=81=ABcontext.TODO()?= =?UTF-8?q?=E3=82=92=E4=BD=BF=E7=94=A8=E3=81=97=E3=80=81=E9=9D=99=E7=9A=84?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=E3=83=84=E3=83=BC=E3=83=AB=E3=81=AE=E8=AD=A6?= =?UTF-8?q?=E5=91=8A=E3=82=92=E8=A7=A3=E6=B6=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typing-server/api/cmd/main.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/typing-server/api/cmd/main.go b/typing-server/api/cmd/main.go index 5d6430f..82272b5 100644 --- a/typing-server/api/cmd/main.go +++ b/typing-server/api/cmd/main.go @@ -79,7 +79,8 @@ func main() { // シグナルを待機 <-sigChan logger.Info("shutting down the server...") - if err := server.Shutdown(nil); err != nil { + ctx := context.TODO() // Use context.TODO() as a temporary placeholder + if err := server.Shutdown(ctx); err != nil { logger.Error("error during server shutdown: %v", err) errChan <- err // エラーをチャネルに送信 }