-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from su-its/feat/backend/docker-setup
Feat/backend/docker setup
- Loading branch information
Showing
8 changed files
with
252 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"sync" | ||
"syscall" | ||
"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: %v", err) | ||
return | ||
} | ||
|
||
// MySQLの接続設定 | ||
mysqlConfig := &mysql.Config{ | ||
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: %v", err) | ||
return | ||
} | ||
defer entClient.Close() | ||
logger.Info("ent client is opened") | ||
|
||
// スキーマの作成 | ||
if err := entClient.Schema.Create(context.Background()); err != nil { | ||
logger.Error("failed to create schema: %v", err) | ||
return | ||
} | ||
logger.Info("schema is created") | ||
|
||
// ルートの登録 | ||
presenter.RegisterRoutes() | ||
|
||
// 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をデクリメント | ||
// サーバーの設定 | ||
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...") | ||
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 // エラーをチャネルに送信 | ||
} | ||
}() | ||
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package system | ||
|
||
import ( | ||
"log/slog" | ||
"net/http" | ||
) | ||
|
||
// HealthCheck はヘルスチェックのためのハンドラー関数です。 | ||
func HealthCheck(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
_, err := w.Write([]byte("API is running")) | ||
if err != nil { | ||
// エラーログを記録し、処理を終了します。 | ||
// 実際には、この時点でレスポンスヘッダーやボディがクライアントに送信されている可能性が高いため、 | ||
// http.Errorを呼び出すことは推奨されません。 | ||
// 代わりに、ログに記録するなどのサーバー側での対応が適切です。 | ||
slog.Error("failed to write response: %v", err) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
services: | ||
api: | ||
depends_on: | ||
db: | ||
condition: service_healthy | ||
build: | ||
context: . | ||
dockerfile: Dockerfile | ||
volumes: | ||
- ./api:/app | ||
ports: | ||
- "8080:8080" | ||
networks: | ||
app_net: | ||
ipv4_address: '172.28.1.3' | ||
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 | ||
MYSQL_USER: user | ||
MYSQL_PASSWORD: password | ||
MYSQL_ROOT_PASSWORD: password | ||
ports: | ||
- "3307: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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
services: | ||
api: | ||
depends_on: | ||
db: | ||
condition: service_healthy | ||
build: | ||
context: . | ||
dockerfile: Dockerfile | ||
volumes: | ||
- ./api:/app | ||
ports: | ||
- "8080:8080" | ||
networks: | ||
app_net: | ||
ipv4_address: '172.28.1.3' | ||
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 | ||
MYSQL_USER: user | ||
MYSQL_PASSWORD: password | ||
MYSQL_ROOT_PASSWORD: password | ||
ports: | ||
- "3307: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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters