diff --git a/app/cmd/main.go b/app/cmd/main.go index 9d3276a..fbd7821 100644 --- a/app/cmd/main.go +++ b/app/cmd/main.go @@ -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" ) @@ -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)) diff --git a/app/go.mod b/app/go.mod index 8ec884e..69912ef 100644 --- a/app/go.mod +++ b/app/go.mod @@ -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 ) diff --git a/app/go.sum b/app/go.sum index bf55a25..4a7017b 100644 --- a/app/go.sum +++ b/app/go.sum @@ -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= diff --git a/app/interface/router/router.go b/app/interface/router/router.go new file mode 100644 index 0000000..0949a42 --- /dev/null +++ b/app/interface/router/router.go @@ -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 +}