-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
47 lines (39 loc) · 1.04 KB
/
router.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
package main
import (
"log"
"net/http"
"github.com/gin-gonic/gin"
)
type router struct {
ginDispatcher *gin.Engine
controller IController
}
type IRouter interface {
GET(path string, f func(w http.ResponseWriter, req *http.Request))
Serve(addr string) error
}
// NewGinRouter a constructor to create new instance of Gin-gonic HTTP server
func NewGinRouter(controller IController) IRouter {
gin.SetMode(gin.ReleaseMode)
ginDispatcher := gin.New()
ginDispatcher.Use(
gin.Recovery(), // Recover from any panic and return HTTP 500 status code
)
router := router{
ginDispatcher: ginDispatcher,
controller: controller,
}
paths(&router)
return &router
}
func (r *router) GET(path string, f func(w http.ResponseWriter, req *http.Request)) {
r.ginDispatcher.GET(path, gin.WrapF(f))
}
func (r *router) Serve(addr string) error {
log.Printf("GIN HTTP server running on port '%v'\n", addr)
return r.ginDispatcher.Run(addr)
}
func paths(r *router) {
r.GET("/fizz-buzz/stats", r.controller.Stats)
r.GET("/fizz-buzz", r.controller.GetByParams)
}