generated from calmato/project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
61 lines (48 loc) · 1.22 KB
/
http.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
50
51
52
53
54
55
56
57
58
59
60
61
package native
import (
"context"
"fmt"
"net/http"
"github.com/calmato/gran-book/api/internal/gateway/util"
"github.com/calmato/gran-book/api/pkg/exception"
"github.com/gin-gonic/gin"
)
type HTTPServer struct {
ctx context.Context
server *http.Server
}
func newHTTPServer(ctx context.Context, port string, r *gin.Engine) *HTTPServer {
server := &http.Server{
Addr: fmt.Sprintf(":%s", port),
Handler: r,
}
return &HTTPServer{
ctx: ctx,
server: server,
}
}
func (s *HTTPServer) Serve() error {
return s.server.ListenAndServe()
}
func (s *HTTPServer) Stop() error {
return s.server.Shutdown(s.ctx)
}
func newRouter(reg *registry, opts ...gin.HandlerFunc) *gin.Engine {
rt := gin.Default()
rt.Use(opts...)
authRequiredGroup := rt.Group("")
authRequiredGroup.Use(util.SetRequestID())
authRequiredGroup.Use(reg.Authenticator.Authentication())
// auth required routes
reg.V1Api.Routes(authRequiredGroup)
reg.V2Api.Routes(authRequiredGroup)
// non auth required routes
reg.V1Api.NonAuthRoutes(rt.Group(""))
rt.GET("/health", util.HealthCheck)
// other routes
rt.NoRoute(func(c *gin.Context) {
err := fmt.Errorf("not found")
util.ErrorHandling(c, exception.ErrNotFound.New(err))
})
return rt
}