-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathweb.go
More file actions
37 lines (32 loc) · 730 Bytes
/
web.go
File metadata and controls
37 lines (32 loc) · 730 Bytes
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
package bot
import (
"context"
"net/http"
"time"
)
type server struct{ http.Server }
func newServer(address string) *server {
if address == "" || address == ":" {
address = ":9090"
}
return &server{Server: http.Server{
Addr: address,
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
}}
}
func (h *server) Load(r *Robot) {
h.Handler = r.Router
go func() {
if err := h.ListenAndServe(); err != nil {
r.Logger.Errorf("Server error: %s", err.Error())
}
}()
}
func (h *server) Unload(r *Robot) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
defer cancel()
r.Logger.Info("Shutting down web server")
h.Shutdown(ctx)
}