Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/backend/api/seed #15

Merged
merged 6 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions typing-app/docs/get-started.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 開発環境構築

## 1. リポジトリのクローン

```bash
$ git clone https://github.com/su-its/typing.git
$ cd typing/typing-app
```

## 2. パッケージのインストール(npm ではなく bun を採用しています)

### Bun をインストールする (まだインストールしていない場合)

```bash
$ curl -fsSL https://bun.sh/install | bash
```

### プロジェクトの依存パッケージをインストール(typing-app ディレクトリで実行)

```bash
$ bun install
```

## 3. API & DB の起動(typing-server ディレクトリで実行)

```bash
$ docker-compose --file docker-compose.dev.yml up --build
```

## 4. フロントエンドの起動(typing-app ディレクトリで実行)

```bash
$ bun dev
```
4 changes: 2 additions & 2 deletions typing-server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ RUN go mod download
RUN CGO_ENABLED=0 GOOS=linux go build -v -o server ./api/cmd/main.go

# 実行イメージ
FROM alpine:latest
FROM alpine:latest
RUN apk --no-cache add ca-certificates

# tzdataパッケージのインストール
Expand All @@ -26,4 +26,4 @@ WORKDIR /root
COPY --from=builder /app/server .

# アプリケーションの実行
CMD ["./server"]
CMD ["./server","-seed","false"]
29 changes: 29 additions & 0 deletions typing-server/Dockerfile.dev
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","-seed","true"]
60 changes: 57 additions & 3 deletions typing-server/api/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package main

import (
"context"
"flag"
"fmt"
"log/slog"
"math/rand"
"net/http"
"os"
"os/signal"
Expand All @@ -11,11 +14,14 @@ import (
"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"
"github.com/su-its/typing/typing-server/domain/repository/ent/user"
)

func main() {
seedFlag := flag.Bool("seed", false, "シードデータを挿入する場合はtrueを指定")
flag.Parse()

logger := slog.Default()

// タイムゾーンの設定
Expand Down Expand Up @@ -57,8 +63,16 @@ func main() {
}
logger.Info("schema is created")

// ルートの登録
presenter.RegisterRoutes()
// シードデータの挿入
if *seedFlag {
if err := seedData(context.Background(), entClient); err != nil {
logger.Error("failed to seed data: %v", err)
return
}
logger.Info("シードデータが挿入されました")
} else {
logger.Info("シードデータは挿入されませんでした")
}

// WaitGroupの宣言
var wg sync.WaitGroup
Expand Down Expand Up @@ -100,3 +114,43 @@ func main() {
close(errChan)
logger.Info("server exited")
}

// TODO: 本番環境では削除する
func seedData(ctx context.Context, client *ent.Client) error {
// シードデータの作成
for i := 0; i < 10; i++ {
isAlreadySeeded, err := client.User.Query().Where(user.StudentNumber(fmt.Sprintf("user%d", i+1))).Exist(ctx)
if err != nil {
return err
}
if isAlreadySeeded {
continue
}

u, err := client.User.Create().
SetStudentNumber(fmt.Sprintf("user%d", i+1)).
SetHandleName(fmt.Sprintf("handle%d", i+1)).
Save(ctx)
if err != nil {
panic(err)
}

for j := 0; j < 5; j++ {
score, err := client.Score.Create().
SetKeystrokes(rand.Intn(200)).
SetAccuracy(rand.Float64()).
SetCreatedAt(time.Now()).
Save(ctx)
if err != nil {
panic(err)
}

_, err = client.User.UpdateOne(u).
AddScores(score).Save(ctx)
if err != nil {
panic(err)
}
}
}
return nil
}
8 changes: 4 additions & 4 deletions typing-server/docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
condition: service_healthy
build:
context: .
dockerfile: Dockerfile
dockerfile: Dockerfile.dev
volumes:
- ./api:/app
environment:
Expand All @@ -14,9 +14,9 @@ services:
- "8080:8080"
networks:
app_net:
ipv4_address: '172.28.1.3'
ipv4_address: "172.28.1.3"
extra_hosts:
- 'db:172.28.1.5'
- "db:172.28.1.5"
db:
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
Expand All @@ -36,7 +36,7 @@ services:
- db-data:/var/lib/mysql
networks:
app_net:
ipv4_address: '172.28.1.5'
ipv4_address: "172.28.1.5"
volumes:
db-data:
networks:
Expand Down
11 changes: 3 additions & 8 deletions typing-server/domain/repository/ent/migrate/schema.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading