Skip to content

Commit

Permalink
Merge pull request #18 from MidraLab/fxi/feat-router-file
Browse files Browse the repository at this point in the history
routerのモジュール化?
  • Loading branch information
eiei114 authored Nov 4, 2023
2 parents a9bb610 + c4eda31 commit 6181295
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 22 deletions.
21 changes: 5 additions & 16 deletions app/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"example.com/application/service"
"example.com/config"
infrastructure "example.com/infrastructure/persistence"
_interface "example.com/interface/handler"
"github.com/uptrace/bunrouter"
handler "example.com/interface/handler"
router "example.com/interface/router"
"log"
"net/http"
)
Expand All @@ -25,21 +25,10 @@ func main() {

userRepository := infrastructure.NewUserRepository(db)
userService := service.NewUserService(userRepository)
userHandler := _interface.NewUserHandler(userService)
userHandler := handler.NewUserHandler(userService)
middleware := middleware.NewMiddleware(userService)

r := bunrouter.New()
r.Use(middleware.RecoverMiddleware())
r.Use(middleware.CorsMiddleware())

r.POST("/user/create", userHandler.UserCreateHandle())

r.Use(middleware.AuthenticateMiddleware()).WithGroup("", func(group *bunrouter.Group) {
group.POST("/user/get", userHandler.UserGetHandle())
group.POST("/user/score", userHandler.ScoreUpdateHandle())
group.POST("/user/destroy", userHandler.DestroyHandle())
group.GET("/users/get", userHandler.UserRankingGetHandle())
})
router := router.NewRouter(userHandler, middleware)
r := router.InitRouter()

log.Println("listening on http://localhost:8080")
log.Println(http.ListenAndServe(":8080", r))
Expand Down
4 changes: 2 additions & 2 deletions app/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ require (
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/sys v0.14.0 // indirect
)
8 changes: 4 additions & 4 deletions app/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAh
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
go.uber.org/mock v0.3.0 h1:3mUxI1No2/60yUYax92Pt8eNOEecx2D3lcXZh2NEZJo=
go.uber.org/mock v0.3.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
36 changes: 36 additions & 0 deletions app/interface/router/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package _interface

import (
"example.com/application/middleware"
_interface "example.com/interface/handler"
"github.com/uptrace/bunrouter"
)

type Router struct {
UserHandler _interface.UserHandlerInterface
Middleware middleware.MiddlewareInterface
}

func NewRouter(userHandler _interface.UserHandlerInterface, middleware middleware.MiddlewareInterface) *Router {
return &Router{
UserHandler: userHandler,
Middleware: middleware,
}
}

func (i *Router) InitRouter() *bunrouter.Router {
b := bunrouter.New()
b.Use(i.Middleware.RecoverMiddleware())
b.Use(i.Middleware.CorsMiddleware())

b.POST("/user/create", i.UserHandler.UserCreateHandle())

b.Use(i.Middleware.AuthenticateMiddleware()).WithGroup("", func(group *bunrouter.Group) {
group.POST("/user/get", i.UserHandler.UserGetHandle())
group.POST("/user/score", i.UserHandler.ScoreUpdateHandle())
group.POST("/user/destroy", i.UserHandler.DestroyHandle())
group.GET("/users/get", i.UserHandler.UserRankingGetHandle())
})

return b
}

0 comments on commit 6181295

Please sign in to comment.