-
Notifications
You must be signed in to change notification settings - Fork 2
/
fasthttp_service.go
79 lines (66 loc) · 2.21 KB
/
fasthttp_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
package hermes
import (
"errors"
rscsrv "github.com/lab259/go-rscsrv"
"github.com/valyala/fasthttp"
)
// FasthttpServiceConfiguration keeps all the configuration needed to start the
// `FasthttpService`.
type FasthttpServiceConfiguration struct {
Bind string
TLS *FasthttpServiceConfigurationTLS
}
// FasthttpServiceConfigurationTLS keeps the configuration for starting a TLS
// server.
type FasthttpServiceConfigurationTLS struct {
CertFile string
KeyFile string
}
// FasthttpService implements the server for starting
type FasthttpService struct {
serviceState
Configuration FasthttpServiceConfiguration
Server fasthttp.Server
}
// LoadConfiguration does not do anything in this implementation. This methods
// is just a placeholder to be overwritten on its usage.
func (service *FasthttpService) LoadConfiguration() (interface{}, error) {
return nil, errors.New("not implemented")
}
// ApplyConfiguration checks if the passing interface is a
// `FasthttpServiceConfiguration` and applies its configuration to the service.
func (service *FasthttpService) ApplyConfiguration(configuration interface{}) error {
switch c := configuration.(type) {
case *FasthttpServiceConfiguration:
service.Configuration = *c
return nil
case FasthttpServiceConfiguration:
service.Configuration = c
return nil
}
return rscsrv.ErrWrongConfigurationInformed
}
// Restart returns an error due to fasthttp not being able to stop the service.
func (service *FasthttpService) Restart() error {
if err := service.Stop(); err != nil {
return err
}
return service.Start()
}
// Start ListenAndServe the server. This method is blocking because it uses
// the fasthttp.ListenAndServe implementation.
func (service *FasthttpService) Start() error {
service.setRunning(true)
if service.Configuration.TLS == nil {
return service.Server.ListenAndServe(service.Configuration.Bind)
}
return service.Server.ListenAndServeTLS(service.Configuration.Bind, service.Configuration.TLS.CertFile, service.Configuration.TLS.KeyFile)
}
// Stop closes the listener and waits the `Start` to stop.
func (service *FasthttpService) Stop() error {
if service.isRunning() {
service.setRunning(false)
return service.Server.Shutdown()
}
return nil
}