Skip to content

Commit

Permalink
Merge pull request #503 from traPtitech/feature/v1_write_flag
Browse files Browse the repository at this point in the history
v1の書き込み系api停止用環境変数作成
  • Loading branch information
mazrean authored Oct 26, 2022
2 parents f47708b + c633051 commit 64d4f99
Show file tree
Hide file tree
Showing 22 changed files with 289 additions and 50 deletions.
1 change: 1 addition & 0 deletions src/config/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ const (
type App interface {
Status() (AppStatus, error)
FeatureV2() bool
FeatureV1Write() bool
}
15 changes: 15 additions & 0 deletions src/config/v1/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,18 @@ func (*App) FeatureV2() bool {

return v2
}

func (*App) FeatureV1Write() bool {
env, ok := os.LookupEnv(envKeyFeatureV1Write)
if !ok {
return true
}

v2, err := strconv.ParseBool(env)
if err != nil {
log.Printf("failed to parse %s: %v\n", envKeyFeatureV2, err)
return true
}

return v2
}
5 changes: 3 additions & 2 deletions src/config/v1/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package v1
type envKey = string

const (
envKeyCollectionEnv envKey = "COLLECTION_ENV"
envKeyFeatureV2 envKey = "FEATURE_V2"
envKeyCollectionEnv envKey = "COLLECTION_ENV"
envKeyFeatureV2 envKey = "FEATURE_V2"
envKeyFeatureV1Write envKey = "FEATURE_V1_WRITE"

envKeyStorage envKey = "STORAGE"

Expand Down
24 changes: 20 additions & 4 deletions src/handler/v1/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,35 @@ import (

"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/traPtitech/trap-collection-server/src/config"
"github.com/traPtitech/trap-collection-server/src/domain/values"
"github.com/traPtitech/trap-collection-server/src/handler/v1/openapi"
"github.com/traPtitech/trap-collection-server/src/service"
)

type Game struct {
session *Session
gameService service.Game
featureWrite bool
session *Session
gameService service.Game
}

func NewGame(
appConf config.App,
session *Session,
gameService service.Game,
) *Game {
return &Game{
session: session,
gameService: gameService,
featureWrite: appConf.FeatureV1Write(),
session: session,
gameService: gameService,
}
}

func (g *Game) PostGame(c echo.Context, newGame *openapi.NewGame) (*openapi.GameInfo, error) {
if !g.featureWrite {
return nil, echo.NewHTTPError(http.StatusForbidden, "write feature is disabled")
}

session, err := getSession(c)
if err != nil {
return nil, echo.NewHTTPError(http.StatusInternalServerError, "failed to get session")
Expand Down Expand Up @@ -112,6 +120,10 @@ func (g *Game) GetGame(c echo.Context, strGameID string) (*openapi.Game, error)
}

func (g *Game) PutGame(c echo.Context, strGameID string, gameMeta *openapi.NewGame) (*openapi.GameInfo, error) {
if !g.featureWrite {
return nil, echo.NewHTTPError(http.StatusForbidden, "write feature is disabled")
}

ctx := c.Request().Context()

uuidGameID, err := uuid.Parse(strGameID)
Expand Down Expand Up @@ -219,6 +231,10 @@ func (g *Game) GetGames(c echo.Context, strAll string) ([]*openapi.Game, error)
}

func (g *Game) DeleteGames(c echo.Context, strGameID string) error {
if !g.featureWrite {
return echo.NewHTTPError(http.StatusForbidden, "write feature is disabled")
}

ctx := c.Request().Context()

uuidGameID, err := uuid.Parse(strGameID)
Expand Down
9 changes: 8 additions & 1 deletion src/handler/v1/game_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,29 @@ import (

"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/traPtitech/trap-collection-server/src/config"
"github.com/traPtitech/trap-collection-server/src/domain/values"
"github.com/traPtitech/trap-collection-server/src/handler/v1/openapi"
"github.com/traPtitech/trap-collection-server/src/service"
)

type GameFile struct {
featureWrite bool
gameFileService service.GameFile
}

func NewGameFile(gameFileService service.GameFile) *GameFile {
func NewGameFile(appConf config.App, gameFileService service.GameFile) *GameFile {
return &GameFile{
featureWrite: appConf.FeatureV1Write(),
gameFileService: gameFileService,
}
}

func (gf *GameFile) PostFile(c echo.Context, strGameID string, strEntryPoint string, strFileType string, file multipart.File) (*openapi.GameFile, error) {
if !gf.featureWrite {
return nil, echo.NewHTTPError(http.StatusForbidden, "v1 write is disabled")
}

ctx := c.Request().Context()

uuidGameID, err := uuid.Parse(strGameID)
Expand Down
15 changes: 13 additions & 2 deletions src/handler/v1/game_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
mockConfig "github.com/traPtitech/trap-collection-server/src/config/mock"
"github.com/traPtitech/trap-collection-server/src/domain"
"github.com/traPtitech/trap-collection-server/src/domain/values"
"github.com/traPtitech/trap-collection-server/src/handler/v1/openapi"
Expand All @@ -27,9 +28,14 @@ func TestPostFile(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockAppConfig := mockConfig.NewMockApp(ctrl)
mockAppConfig.
EXPECT().
FeatureV1Write().
Return(true)
mockGameFileService := mock.NewMockGameFile(ctrl)

gameFileHandler := NewGameFile(mockGameFileService)
gameFileHandler := NewGameFile(mockAppConfig, mockGameFileService)

type test struct {
description string
Expand Down Expand Up @@ -256,9 +262,14 @@ func TestGetGameFile(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockAppConfig := mockConfig.NewMockApp(ctrl)
mockAppConfig.
EXPECT().
FeatureV1Write().
Return(true)
mockGameFileService := mock.NewMockGameFile(ctrl)

gameFileHandler := NewGameFile(mockGameFileService)
gameFileHandler := NewGameFile(mockAppConfig, mockGameFileService)

type test struct {
description string
Expand Down
9 changes: 8 additions & 1 deletion src/handler/v1/game_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,28 @@ import (

"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/traPtitech/trap-collection-server/src/config"
"github.com/traPtitech/trap-collection-server/src/domain/values"
"github.com/traPtitech/trap-collection-server/src/service"
)

type GameImage struct {
featureWrite bool
gameImageService service.GameImage
}

func NewGameImage(gameImageService service.GameImage) *GameImage {
func NewGameImage(appConf config.App, gameImageService service.GameImage) *GameImage {
return &GameImage{
featureWrite: appConf.FeatureV1Write(),
gameImageService: gameImageService,
}
}

func (gi *GameImage) PostImage(c echo.Context, strGameID string, image multipart.File) error {
if !gi.featureWrite {
return echo.NewHTTPError(http.StatusForbidden, "v1 write is disabled")
}

ctx := c.Request().Context()

uuidGameID, err := uuid.Parse(strGameID)
Expand Down
15 changes: 13 additions & 2 deletions src/handler/v1/game_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
mockConf "github.com/traPtitech/trap-collection-server/src/config/mock"
"github.com/traPtitech/trap-collection-server/src/domain/values"
"github.com/traPtitech/trap-collection-server/src/service"
"github.com/traPtitech/trap-collection-server/src/service/mock"
Expand All @@ -24,9 +25,14 @@ func TestPostImage(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockAppConfig := mockConf.NewMockApp(ctrl)
mockAppConfig.
EXPECT().
FeatureV1Write().
Return(true)
mockGameImageService := mock.NewMockGameImage(ctrl)

gameImageHandler := NewGameImage(mockGameImageService)
gameImageHandler := NewGameImage(mockAppConfig, mockGameImageService)

type test struct {
description string
Expand Down Expand Up @@ -136,9 +142,14 @@ func TestGetImage(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockAppConfig := mockConf.NewMockApp(ctrl)
mockAppConfig.
EXPECT().
FeatureV1Write().
Return(true)
mockGameImageService := mock.NewMockGameImage(ctrl)

gameImageHandler := NewGameImage(mockGameImageService)
gameImageHandler := NewGameImage(mockAppConfig, mockGameImageService)

type test struct {
description string
Expand Down
9 changes: 8 additions & 1 deletion src/handler/v1/game_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,31 @@ import (

"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/traPtitech/trap-collection-server/src/config"
"github.com/traPtitech/trap-collection-server/src/domain/values"
"github.com/traPtitech/trap-collection-server/src/handler/v1/openapi"
"github.com/traPtitech/trap-collection-server/src/service"
)

type GameRole struct {
featureWrite bool
session *Session
gameAuthService service.GameAuth
}

func NewGameRole(session *Session, gameAuthService service.GameAuth) *GameRole {
func NewGameRole(appConf config.App, session *Session, gameAuthService service.GameAuth) *GameRole {
return &GameRole{
featureWrite: appConf.FeatureV1Write(),
session: session,
gameAuthService: gameAuthService,
}
}

func (gr *GameRole) PostMaintainer(c echo.Context, strGameID string, maintainers *openapi.Maintainers) error {
if !gr.featureWrite {
return echo.NewHTTPError(http.StatusForbidden, "write is disabled")
}

session, err := getSession(c)
if err != nil {
log.Printf("error: failed to get session: %v\n", err)
Expand Down
14 changes: 12 additions & 2 deletions src/handler/v1/game_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ func TestPostMaintainer(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockAppConfig := mockConfig.NewMockApp(ctrl)
mockAppConfig.
EXPECT().
FeatureV1Write().
Return(true)
mockGameAuthService := mock.NewMockGameAuth(ctrl)
mockConf := mockConfig.NewMockHandler(ctrl)
mockConf.
Expand All @@ -47,7 +52,7 @@ func TestPostMaintainer(t *testing.T) {
return
}

gameRoleHandler := NewGameRole(session, mockGameAuthService)
gameRoleHandler := NewGameRole(mockAppConfig, session, mockGameAuthService)

type test struct {
description string
Expand Down Expand Up @@ -270,6 +275,11 @@ func TestGetMaintainer(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockAppConfig := mockConfig.NewMockApp(ctrl)
mockAppConfig.
EXPECT().
FeatureV1Write().
Return(true)
mockGameAuthService := mock.NewMockGameAuth(ctrl)
mockConf := mockConfig.NewMockHandler(ctrl)
mockConf.
Expand All @@ -291,7 +301,7 @@ func TestGetMaintainer(t *testing.T) {
return
}

gameRoleHandler := NewGameRole(session, mockGameAuthService)
gameRoleHandler := NewGameRole(mockAppConfig, session, mockGameAuthService)

type test struct {
description string
Expand Down
Loading

0 comments on commit 64d4f99

Please sign in to comment.