/
boot.go
53 lines (42 loc) · 1.02 KB
/
boot.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
package gallang
import (
"github.com/gin-gonic/gin"
)
// Gallang provide interface of gallang oembed app
type Gallang struct {
App *gin.Engine
Host string
}
// New build a new application
func New(addr string) *Gallang {
if len(addr) <= 1 {
addr = "0.0.0.0:5000"
}
app := new(Gallang)
app.Host = addr
app.Build()
return app
}
// Build will do action of building an app
func (g *Gallang) Build() {
g.App = gin.New()
g.App.Use(gin.Logger())
g.App.Use(gin.Recovery())
routes := getRouteDefinition()
for _, item := range routes {
g.RouteHandle(item.method, item.endpoint, item.worker)
}
}
// Use will tell Gallang to use the middleware
func (g *Gallang) Use(middleware gin.HandlerFunc) {
g.App.Use(middleware)
}
// RouteHandle provide a route registration
func (g *Gallang) RouteHandle(httpMethod, relativePath string, handlers gin.HandlerFunc) gin.IRoutes {
return g.App.Handle(httpMethod, relativePath, handlers)
}
// Run gallang API
func (g *Gallang) Run() (err error) {
err = g.App.Run(g.Host)
return err
}