-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsloop.go
169 lines (137 loc) · 3.07 KB
/
sloop.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
161
162
163
164
165
166
167
168
169
package dawn
import (
"errors"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/go-dawn/dawn/config"
"github.com/go-dawn/dawn/fiberx"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/pprof"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/gofiber/fiber/v2/middleware/requestid"
)
// Version of current dawn package
const Version = "0.4.0"
// Config is a struct holding the sloop settings.
type Config struct {
// App indicates to fiber app instance
App *fiber.App
}
// Sloop denotes Dawn application
type Sloop struct {
// Config is the embedded config
Config
app *fiber.App
mods []Moduler
cleanups []Cleanup
sigCh chan os.Signal
}
// New returns a new Sloop with options.
func New(config ...Config) *Sloop {
s := &Sloop{
sigCh: make(chan os.Signal, 1),
}
if len(config) > 0 {
s.Config = config[0]
}
s.app = s.Config.App
return s
}
// Default returns an Sloop instance with the
// `RequestID`, `Logger`, `Recovery`, `Pprof`
// middleware already attached in default fiber app.
func Default(cfg ...fiber.Config) *Sloop {
c := fiber.Config{}
if len(cfg) > 0 {
c = cfg[0]
}
if c.ErrorHandler == nil {
c.ErrorHandler = fiberx.ErrHandler
}
app := fiber.New(c)
app.Use(
requestid.New(),
fiberx.Logger(),
recover.New(),
)
if config.GetBool("debug") {
app.Use(pprof.New())
}
return &Sloop{
app: app,
sigCh: make(chan os.Signal, 1),
}
}
// AddModulers appends more Modulers
func (s *Sloop) AddModulers(m ...Moduler) *Sloop {
s.mods = append(s.mods, m...)
return s
}
// Run runs a web server
func (s *Sloop) Run(addr string) error {
defer s.Cleanup()
if s.app == nil {
return errors.New("dawn: app is nil")
}
s.Setup().registerRoutes()
return s.app.Listen(addr)
}
// RunTls runs a tls web server
func (s *Sloop) RunTls(addr, certFile, keyFile string) error {
defer s.Cleanup()
if s.app == nil {
return errors.New("dawn: app is nil")
}
s.Setup().registerRoutes()
return s.app.ListenTLS(addr, certFile, keyFile)
}
// Shutdown gracefully shuts down the server without interrupting any active connections.
func (s *Sloop) Shutdown() error {
if s.app == nil {
return fmt.Errorf("shutdown: fiber app is not found")
}
return s.app.Shutdown()
}
// Router returns the server router
func (s *Sloop) Router() fiber.Router {
return s.app
}
// Setup initializes all modules and then boots them
func (s *Sloop) Setup() *Sloop {
return s.init().boot()
}
func (s *Sloop) init() *Sloop {
for _, mod := range s.mods {
if cleanup := mod.Init(); cleanup != nil {
s.cleanups = append(s.cleanups, cleanup)
}
}
return s
}
func (s *Sloop) boot() *Sloop {
for _, mod := range s.mods {
mod.Boot()
}
return s
}
func (s *Sloop) registerRoutes() *Sloop {
for _, mod := range s.mods {
mod.RegisterRoutes(s.app)
}
return s
}
// Cleanup releases resources
func (s *Sloop) Cleanup() {
for _, fn := range s.cleanups {
fn()
}
}
// Watch listens to signals and waits to exit
func (s *Sloop) Watch() {
signal.Notify(s.sigCh,
syscall.SIGTERM, syscall.SIGINT,
syscall.SIGHUP, syscall.SIGQUIT)
<-s.sigCh
}