-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgogreenservice.go
53 lines (45 loc) · 1.07 KB
/
gogreenservice.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 gogreen
import (
"log"
"net/http"
"github.com/gin-gonic/gin"
"github.com/pyaesone17/gogreen/app/bundles/car"
"github.com/pyaesone17/gogreen/app/bundles/rent"
"github.com/pyaesone17/gogreen/app/core"
viper "github.com/spf13/viper"
)
// BlogService holds
type BlogService struct {
viper *viper.Viper
srv *http.Server
}
// NewService is constructor for BlogService struct
func NewService(viper *viper.Viper) *BlogService {
return &BlogService{
viper: viper,
}
}
// ListenAndServe will listen the port
func (s *BlogService) ListenAndServe() {
r := gin.Default()
for _, b := range initBundles(s.viper) {
for _, route := range b.GetRoutes() {
log.Printf("adding handler for \"%s %s\"", route.Method, route.Path)
r.Handle(route.Method, route.Path, route.Handler)
}
}
r.Run() // listen and serve on 0.0.0.0:8080
return
}
// Stop will stop running the server
func (s *BlogService) Stop() {
if s.srv != nil {
s.srv.Close()
}
}
func initBundles(viper *viper.Viper) []core.Bundle {
return []core.Bundle{
rent.NewBundle(viper),
car.NewBundle(viper),
}
}