-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.go
More file actions
54 lines (48 loc) · 1.27 KB
/
server.go
File metadata and controls
54 lines (48 loc) · 1.27 KB
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
// Package http provides basic server skeleton that listens on specified
// http:// or unix:// socket and exits correctly on keyboard breaks and OS
// signals.
//
//
package http
import (
"context"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/pkg/errors"
)
// Server is a scaffolding for basic HTTP server. The structure members are
// configured externally, through JSON deserialization or environment variables
type Server struct {
StopTimeout int `default:"5000"`
Address string `default:":8000"`
}
// Serve starts the server on the configured address
func (srv *Server) Serve(h http.Handler) (err error) {
l, err := srv.listen()
if err != nil {
return
}
serverErrors := make(chan error, 1)
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, os.Kill, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGKILL)
defer signal.Stop(interrupt)
hs := http.Server{Handler: h}
go func() {
serverErrors <- hs.Serve(l)
}()
select {
case sig := <-interrupt:
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(srv.StopTimeout)*time.Millisecond)
defer cancel()
if err = hs.Shutdown(ctx); err != nil {
return
}
err = errors.New(sig.String())
case err = <-serverErrors:
break
}
return
}