-
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.
- Loading branch information
1 parent
9390edb
commit a9e4362
Showing
9 changed files
with
125 additions
and
1 deletion.
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
openapi: 3.1.0 | ||
openapi: 3.0.4 | ||
info: | ||
title: Room Service | ||
description: |- | ||
|
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
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,10 @@ | ||
package entity | ||
|
||
import "time" | ||
|
||
type Base struct { | ||
ID string `gorm:"primaryKey; type:varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;NOT NULL"` | ||
CreatedAt time.Time | ||
UpdatedAt time.Time | ||
DeletedAt *time.Time `sql:"index"` | ||
} |
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,5 @@ | ||
package entity | ||
|
||
type Group struct { | ||
Base | ||
} |
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,7 @@ | ||
package entity | ||
|
||
import "gorm.io/gorm" | ||
|
||
type Member struct { | ||
gorm.Model | ||
} |
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,5 @@ | ||
package entity | ||
|
||
type Room struct { | ||
Base | ||
} |
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,9 @@ | ||
package dbrepo | ||
|
||
import "context" | ||
|
||
type Repository interface { | ||
Open(ctx context.Context) error | ||
Close(ctx context.Context) | ||
Migrate(ctx context.Context) error | ||
} |
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,72 @@ | ||
package mysqldb | ||
|
||
import ( | ||
"context" | ||
aulogging "github.com/StephanHCB/go-autumn-logging" | ||
"github.com/eurofurence/reg-room-service/internal/entity" | ||
"github.com/eurofurence/reg-room-service/internal/repository/database/dbrepo" | ||
"gorm.io/driver/mysql" | ||
"gorm.io/gorm" | ||
"gorm.io/gorm/logger" | ||
"gorm.io/gorm/schema" | ||
"time" | ||
) | ||
|
||
type MysqlRepository struct { | ||
db *gorm.DB | ||
connectString string | ||
Now func() time.Time | ||
} | ||
|
||
func Create(connectString string) dbrepo.Repository { | ||
return &MysqlRepository{ | ||
Now: time.Now, | ||
connectString: connectString, | ||
} | ||
} | ||
|
||
func (r *MysqlRepository) Open(ctx context.Context) error { | ||
gormConfig := gorm.Config{ | ||
NamingStrategy: schema.NamingStrategy{ | ||
TablePrefix: "room_", | ||
}, | ||
Logger: logger.Default.LogMode(logger.Silent), | ||
} | ||
|
||
db, err := gorm.Open(mysql.Open(r.connectString), &gormConfig) | ||
if err != nil { | ||
aulogging.ErrorErrf(ctx, err, "failed to open mysql connection: %s", err.Error()) | ||
return err | ||
} | ||
|
||
sqlDb, err := db.DB() | ||
if err != nil { | ||
aulogging.ErrorErrf(ctx, err, "failed to configure mysql connection: %s", err.Error()) | ||
return err | ||
} | ||
|
||
// see https://making.pusher.com/production-ready-connection-pooling-in-go/ | ||
sqlDb.SetMaxOpenConns(100) | ||
sqlDb.SetMaxIdleConns(50) | ||
sqlDb.SetConnMaxLifetime(time.Minute * 10) | ||
|
||
r.db = db | ||
return nil | ||
} | ||
|
||
func (r *MysqlRepository) Close(_ context.Context) { | ||
// no more db close in gorm v2 | ||
} | ||
|
||
func (r *MysqlRepository) Migrate(ctx context.Context) error { | ||
err := r.db.AutoMigrate( | ||
&entity.Member{}, | ||
&entity.Group{}, | ||
&entity.Room{}, | ||
) | ||
if err != nil { | ||
aulogging.Logger.NoCtx().Error().WithErr(err).Printf("failed to migrate mysql db: %s", err.Error()) | ||
return err | ||
} | ||
return nil | ||
} |