-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpserver.go
160 lines (131 loc) · 3.52 KB
/
httpserver.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package httpserver
import (
"context"
"errors"
"fmt"
"github.com/duanshanghanqing/rocket/pkg/utils"
"github.com/duanshanghanqing/rocket/registry"
"github.com/duanshanghanqing/rocket/server"
"log"
"net/http"
"os"
"os/signal"
"time"
)
type Server struct {
option *server.Option
httpServer *http.Server
}
type ServerOption func(s *Server)
func WithServerOptionID(id string) ServerOption {
return func(s *Server) {
s.option.ID = id
}
}
func WithServerOptionPost(post int) ServerOption {
return func(s *Server) {
s.option.Post = post
}
}
func WithServerOptionTimeout(timeout time.Duration) ServerOption {
return func(s *Server) {
s.option.Timeout = timeout
}
}
func WithServerOptionSignal(signals []os.Signal) ServerOption {
return func(s *Server) {
s.option.Signals = signals
}
}
func WithServerOptionServiceRegisterCenter(serviceRegisterCenter registry.IRegistrar) ServerOption {
return func(s *Server) {
s.option.ServiceRegisterCenter = serviceRegisterCenter
}
}
func WithServerOptionServiceRegisterInfo(serviceRegisterInfo *registry.ServiceRegisterInfo) ServerOption {
return func(s *Server) {
s.option.ServiceRegisterInfo = serviceRegisterInfo
}
}
func WithServerHttpServer(httpServer *http.Server) ServerOption {
return func(s *Server) {
s.httpServer = httpServer
}
}
func (s *Server) startHttpServer() error {
// 服务注册
if s.option.ServiceRegisterCenter != nil {
err := s.option.ServiceRegisterCenter.Register(context.Background(), s.option.ServiceRegisterInfo)
if err != nil {
return err
}
}
// When the user does not implement a handler, implement a default
if s.httpServer.Handler == nil {
mux := http.NewServeMux()
// health examination
mux.HandleFunc("/health", func(writer http.ResponseWriter, request *http.Request) {
_, _ = writer.Write([]byte(string("ok")))
})
s.httpServer.Handler = mux
}
log.Printf("http server start: %s", utils.HostPostToAddress("", s.option.Post))
err := s.httpServer.ListenAndServe()
time.Sleep(6 * time.Second)
return err
}
func (s *Server) stopHttpServer() error {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, s.option.Signals...)
err := fmt.Errorf("%s", <-signalChan)
log.Println("http server stopping")
if s.option.ServiceRegisterCenter != nil {
_ = s.option.ServiceRegisterCenter.Deregister(context.Background(), s.option.ServiceRegisterInfo)
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err = s.httpServer.Shutdown(ctx)
time.Sleep(5 * time.Second)
log.Println("http server stop")
return err
}
func (s *Server) Run() error {
errChan := make(chan error)
go func() {
errChan <- s.startHttpServer()
}()
go func() {
errChan <- s.stopHttpServer()
}()
return <-errChan
}
func New(opts ...ServerOption) (server.IServer, error) {
defaultOption, err := server.NewHttpDefault()
if err != nil {
return nil, err
}
s := &Server{
option: defaultOption,
}
for _, opt := range opts {
opt(s)
}
if s.httpServer == nil {
s.httpServer = &http.Server{
Addr: utils.HostPostToAddress("", s.option.Post),
}
}
_, post, _ := utils.AddressToHostPost(s.httpServer.Addr)
s.option.Post = post
if s.option.ServiceRegisterInfo != nil {
if s.option.ServiceRegisterInfo.Name == "" {
return nil, errors.New("service name cannot be empty")
}
if s.option.ServiceRegisterInfo.Host == "" {
return nil, errors.New("service register host cannot be empty")
}
s.option.ServiceRegisterInfo.ID = s.option.ID
s.option.ServiceRegisterInfo.Port = post
}
return s, nil
}