-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservice.go
66 lines (51 loc) · 1.09 KB
/
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
package hermes
import (
"context"
"github.com/lab259/go-rscsrv"
"github.com/valyala/fasthttp"
)
// ServiceConfig TODO
type ServiceConfig struct {
Name string
Router Router
Bind string
TLS *FasthttpServiceConfigurationTLS
}
// Service TODO
type Service interface {
rscsrv.Service
rscsrv.StartableWithContext
}
type service struct {
config ServiceConfig
server fasthttp.Server
}
func (srv *service) listenAndServe() error {
srv.server.Handler = srv.config.Router.Handler()
if srv.config.TLS == nil {
return srv.server.ListenAndServe(srv.config.Bind)
}
return srv.server.ListenAndServeTLS(srv.config.Bind, srv.config.TLS.CertFile, srv.config.TLS.KeyFile)
}
func (srv *service) StartWithContext(ctx context.Context) (err error) {
done := make(chan bool, 1)
go func() {
<-ctx.Done()
err = srv.server.Shutdown()
close(done)
}()
if err := srv.listenAndServe(); err != nil {
return err
}
<-done
return
}
func (srv *service) Name() string {
return srv.config.Name
}
// NewService TODO
func NewService(config ServiceConfig) Service {
return &service{
config: config,
}
}