/
routes.go
49 lines (43 loc) · 1.14 KB
/
routes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package test
import (
"net/http"
"github.com/BlackBX/service-framework/router"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"go.uber.org/fx"
)
// Module allows the routes from this module to be registered to the app
var Module = fx.Provide(
NewRedisHandler,
NewPGHandler,
NewHTTPHandler,
fx.Annotated{
Group: "server",
Target: RegisterHandler,
},
)
// HandlerParams is the type that defines the parameters that are
// required to register the handlers to the router
type HandlerParams struct {
fx.In
RedisHandler RedisHandler
PGHandler PGHandler
HTTPHandler HTTPHandler
}
// RegisterHandler registers the handlers to the router
func RegisterHandler(params HandlerParams) router.Module {
return router.Module{
Path: "test",
Router: func(router *mux.Router) {
router.Handle("/redis", handlers.MethodHandler{
http.MethodGet: http.HandlerFunc(params.RedisHandler.Get),
})
router.Handle("/pg", handlers.MethodHandler{
http.MethodGet: http.HandlerFunc(params.PGHandler.Get),
})
router.Handle("/http/{id}", handlers.MethodHandler{
http.MethodGet: http.HandlerFunc(params.HTTPHandler.Get),
})
},
}
}