-
Notifications
You must be signed in to change notification settings - Fork 1
/
gin_service.go
144 lines (121 loc) · 3.12 KB
/
gin_service.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package master_gin
import (
"github.com/pkg/errors"
"log"
"net"
"net/http"
"os"
"sync"
"github.com/acl-dev/go-service"
"github.com/gin-gonic/gin"
)
var (
Version = "1.0.1"
)
type AcceptFunc func(net.Conn)
type CloseFunc func(net.Conn)
type GinServ struct {
Listener net.Listener
Engine *gin.Engine
}
type GinService struct {
Alone bool
Servers []*GinServ
AcceptHandler AcceptFunc
CloseHandler CloseFunc
}
// Run begin to start all the listening servers after Init() called.
func (service *GinService) Run() {
var g sync.WaitGroup // Used to wait for all service to stop.
g.Add(len(service.Servers))
for _, s := range service.Servers {
go func(serv *GinServ) {
defer g.Done()
service.run(serv.Listener, serv.Engine)
}(s)
}
// Waiting the disconnect status from acl_master.
master.Wait()
// Waiting all the gin services to stop.
g.Wait()
}
func (service *GinService) run(listener net.Listener, engine *gin.Engine) {
//_ = engine.RunListener(listener)
server := &http.Server{
Handler: engine,
ConnState: func(conn net.Conn, state http.ConnState) {
switch state {
case http.StateNew:
master.ConnCountInc()
if service.AcceptHandler != nil {
service.AcceptHandler(conn)
}
break
case http.StateActive:
break
case http.StateIdle:
break
case http.StateHijacked:
master.ConnCountDec()
if service.CloseHandler != nil {
service.CloseHandler(conn)
}
break
case http.StateClosed:
master.ConnCountDec()
if service.CloseHandler != nil {
service.CloseHandler(conn)
}
break
default:
break
}
},
}
err := server.Serve(listener)
if err != nil {
log.Printf("pid=%d: Http Service over, err=%s\r\n",
os.Getpid(), err.Error())
}
}
// Init Create the gin service when starting in alone or daemon mode;
// The addresses must not be empty in alone mode, and will be ignored in daemon mode,
// the addresses' format lookup like "127.0.0.1:8080;127.0.0.1:8081;127.0.0.1:8082";
func Init(addresses string) (*GinService, error) {
master.Prepare()
if master.Alone && len(addresses) == 0 {
log.Println("Listening addresses shouldn't be empty in running alone mode!")
return nil, errors.New("Listening addresses shouldn't be empty in alone mode")
}
listeners, err := master.ServiceInit(addresses)
if err != nil {
return nil, err
}
var service GinService
for _, l := range listeners {
engine := gin.Default()
serv := &GinServ{Listener: l, Engine: engine}
service.Servers = append(service.Servers, serv)
}
return &service, nil
}
func (service *GinService) GET(urlPath string, handler func(*gin.Context)) {
for _, s := range service.Servers {
s.Engine.GET(urlPath, handler)
}
}
func (service *GinService) POST(urlPath string, handler func(*gin.Context)) {
for _, s := range service.Servers {
s.Engine.POST(urlPath, handler)
}
}
func (service *GinService) HEAD(urlPath string, handler func(*gin.Context)) {
for _, s := range service.Servers {
s.Engine.HEAD(urlPath, handler)
}
}
func (service *GinService) NoRoute(handler func(*gin.Context)) {
for _, s := range service.Servers {
s.Engine.NoRoute(handler)
}
}