Skip to content

Commit

Permalink
mongoDB対応のコードを書いた。
Browse files Browse the repository at this point in the history
  • Loading branch information
inai17ibar committed Oct 25, 2023
1 parent 6e38091 commit 6797577
Show file tree
Hide file tree
Showing 22 changed files with 536 additions and 382 deletions.
20 changes: 18 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,27 @@ jobs:
uses: actions/setup-go@v4
with:
go-version: '1.21'

- name: Get dependencies
run: go get -v -t -d ./...

- name: Set up Docker Compose
run: |
sudo apt-get update
sudo apt-get install -y docker-compose
- name: Start MongoDB using Docker Compose
run: |
docker-compose -f docker-compose.yml up -d
- name: Wait for MongoDB
run: sleep 10

- name: Run tests
run: go test ./...
env:
API_KEY_GITHUB: ${{ secrets.API_KEY_GITHUB }}
API_KEY_GITHUB: ${{ secrets.API_KEY_GITHUB }}

- name: Shutdown MongoDB
run: |
docker-compose -f docker-compose.yml down
33 changes: 0 additions & 33 deletions .github/workflows/deploy-to-ecr.yml

This file was deleted.

24 changes: 0 additions & 24 deletions .github/workflows/docker-publish.yml

This file was deleted.

52 changes: 31 additions & 21 deletions api/fetch_db.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
package api

import (
"context"
"go-weed-backend/db"
"go-weed-backend/internal/model"
"log"

"github.com/jinzhu/gorm"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

func FetchAndSaveContribution() {
dbInstance := db.GetDB() // この変数を以下のコードで利用
client := db.GetDB() // MongoDB client
collection := client.Collection("contributions") // 対応するコレクションを取得

// 最後のコントリビューションの日付を取得
var lastContribution model.ContributionDayDB
dbInstance.Order("date desc").First(&lastContribution)
opts := options.FindOne().SetSort(bson.D{{Key: "date", Value: -1}})
err := collection.FindOne(context.TODO(), bson.D{}, opts).Decode(&lastContribution)
if err != nil && err != mongo.ErrNoDocuments {
log.Printf("error querying last contribution date: %v\n", err)
return
}

respData, err := CallGithubContributionAPI()
if err != nil {
Expand All @@ -29,31 +38,29 @@ func FetchAndSaveContribution() {
contributionDaysDB := model.ConvertToDBModels(contributions)

for _, c := range contributionDaysDB {
existingData := model.ContributionDayDB{}
if err := dbInstance.Where("date = ?", c.Date).First(&existingData).Error; err != nil {
if err == gorm.ErrRecordNotFound {
if err := dbInstance.Create(&c).Error; err != nil {
log.Printf("error creating contribution: %v\n", err)
}
} else {
log.Printf("error querying contribution: %v\n", err)
}
} else {
if err := dbInstance.Model(&existingData).Updates(&c).Error; err != nil {
log.Printf("error updating contribution: %v\n", err)
}
filter := bson.M{"date": c.Date}
update := bson.M{"$set": &c}
opts := options.Update().SetUpsert(true) // Upsertオプションを使って、存在しない場合は新しく挿入する

_, err := collection.UpdateOne(context.TODO(), filter, update, opts)
if err != nil {
log.Printf("error updating/creating contribution: %v\n", err)
}
}
}

func FetchAndSaveCommits() {
dbInstance := db.GetDB() // この変数を以下のコードで利用
client := db.GetDB() // MongoDB client
collection := client.Collection("commits") // 対応するコレクションを取得

// 最後のコミットの日付を取得
var lastCommit model.MyCommit
dbInstance.Order("date desc").First(&lastCommit)

var commits []model.MyCommit
opts := options.FindOne().SetSort(bson.D{{Key: "date", Value: -1}})
err := collection.FindOne(context.TODO(), bson.D{}, opts).Decode(&lastCommit)
if err != nil && err != mongo.ErrNoDocuments {
log.Printf("error querying last commit date: %v\n", err)
return
}

commits, err := CallGithubAllCommitAPI()
if err != nil {
Expand All @@ -62,7 +69,10 @@ func FetchAndSaveCommits() {

for _, c := range commits {
if c.Date.After(lastCommit.Date) {
dbInstance.Save(&c)
_, err := collection.InsertOne(context.TODO(), c)
if err != nil {
log.Printf("error inserting commit: %v\n", err)
}
}
}
}
24 changes: 7 additions & 17 deletions cmd/go-weed/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@ import (
"encoding/json"
"go-weed-backend/api"
"go-weed-backend/db"
"go-weed-backend/internal/handler"
"go-weed-backend/internal/model"
"go-weed-backend/router"
"io/ioutil"
"log"
"net/http"
"os"
"time"

_ "github.com/jinzhu/gorm/dialects/sqlite"

_ "github.com/mattn/go-sqlite3"
)

Expand Down Expand Up @@ -62,29 +58,23 @@ func main() {
log.Fatalf("Failed to load configuration: %s", err)
}

//localDBPath, s3Client, bucketName, fileKey := util.ConnectS3AWS()

localDBPath := "todos.db"
// データベースに接続
db.InitDB(localDBPath)
database := db.GetDB()
// MongoDBに接続
dbName := "todo_weed_mongo"
db.InitDB(dbName) // この接続文字列はconfigから取得 config.MongoDBConnectionString
defer db.CloseDB()

// マイグレーションを実行してテーブルを作成
database.AutoMigrate(&model.Todo{})
database.AutoMigrate(&model.MyCommit{}) //これがそのままテーブル名になる
database.AutoMigrate(&model.ContributionDayDB{})
database.AutoMigrate(&model.TaskResult{})
db.EnsureCollectionExists(db.GetDB().Client(), dbName, "todos")
db.EnsureCollectionExists(db.GetDB().Client(), dbName, "mycommits")
db.EnsureCollectionExists(db.GetDB().Client(), dbName, "contributionDays")
db.EnsureCollectionExists(db.GetDB().Client(), dbName, "taskResults")

go func() {
// サーバー起動後、初回のフェッチは遅延させる
time.Sleep(30 * time.Minute) //もっといい書き方を考えたい、別プログラムとか
fetchCommitsPeriodically()
}()

// ハンドラーの初期化
handler.Init(database)

// ルーターのセットアップ
r := router.NewRouter()

Expand Down
58 changes: 49 additions & 9 deletions db/db.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,67 @@
// db/db.go
package db

import (
"context"
"log"
"time"

"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

var instance *gorm.DB
var instance *mongo.Client
var dbName = "testDB"

func InitDB(uri string) {

if instance != nil {
return
}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

func InitDB(localDBPath string) {
var err error
instance, err = gorm.Open("sqlite3", localDBPath)
instance, err = mongo.Connect(ctx, options.Client().ApplyURI(uri))
if err != nil {
log.Fatal(err)
}
// defer instance.Disconnect(ctx)

err = instance.Ping(ctx, nil)
if err != nil {
log.Fatalf("Failed to ping: %s", err)
}
//fmt.Println("Successfully connected to MongoDB!")
}

func GetDB() *gorm.DB {
return instance
func GetDB() *mongo.Database {
return instance.Database(dbName)
}

func CloseDB() {
instance.Close()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := instance.Disconnect(ctx); err != nil {
log.Fatal(err)
}
}

func EnsureCollectionExists(client *mongo.Client, dbName, collectionName string) error {
// コレクションの一覧を取得
collections, err := client.Database(dbName).ListCollectionNames(context.TODO(), bson.M{"name": collectionName})
if err != nil {
return err
}

// コレクションが存在しない場合、新しいコレクションを作成
if len(collections) == 0 {
err := client.Database(dbName).CreateCollection(context.TODO(), collectionName, options.CreateCollection().SetCapped(false))
if err != nil {
return err
}
}

return nil
}
7 changes: 6 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ services:
volumes:
- ./todos.db:/weed-app/go-weed-backend/todos.db
ports:
- "8081:8081"
- "8081:8081"

mongodb:
image: mongo:latest
ports:
- "27017:27017"
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,32 @@ require (
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/machinebox/graphql v0.2.2 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
go.mongodb.org/mongo-driver v1.12.1 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.16.0 // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
Expand Down
Loading

0 comments on commit 6797577

Please sign in to comment.