/
server.go
56 lines (46 loc) · 977 Bytes
/
server.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
package gin
import (
"context"
"errors"
"fmt"
"github.com/26597925/EastCloud/pkg/server/api"
"github.com/gin-gonic/gin"
"net/http"
)
type Server struct {
*gin.Engine
*http.Server
option api.Option
}
func NewServer(option api.Option) api.Server {
return &Server{
Engine: gin.New(),
option: option,
}
}
func (svr *Server) Handler(handler api.Handler) error {
if handler == nil {
return errors.New("handler errors is nil ")
}
return handler(svr)
}
func (svr *Server) Init() error {
addr := fmt.Sprintf("%s:%d", svr.option.GetIP(), svr.option.GetPort())
svr.Server = &http.Server{
Addr: addr,
Handler: svr.Engine,
}
return nil
}
func (svr *Server) Start() error {
return svr.Server.ListenAndServe()
}
func (svr *Server) Stop() error {
return svr.Server.Close()
}
func (svr *Server) GracefulStop(ctx context.Context) error {
return svr.Server.Shutdown(ctx)
}
func (svr *Server) GetOption() api.Option {
return svr.option
}