-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
98 lines (85 loc) · 2.17 KB
/
server.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
package rcom
import (
"context"
"encoding/gob"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
func ListenAndServe(port uint16, gracefulShutdown bool, allowedCMDs ...string) error {
cmds := make(map[string]bool)
for _, cmd := range allowedCMDs {
cmds[cmd] = true
}
svc := &service{allowedCMDs: cmds}
server := &http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: svc,
}
if gracefulShutdown {
gracefullyShutdownServerOnSignal(server, GracefulShutdownTimeout)
}
return server.ListenAndServe()
}
type service struct {
allowedCMDs map[string]bool
}
func (s *service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
var command *Command
err := gob.NewDecoder(r.Body).Decode(&command)
if err != nil {
log.Error("can't decode command request").
Err(err).
Log()
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if !s.allowedCMDs[command.Name] {
http.Error(w, fmt.Sprintf("command %q not allowed", command.Name), http.StatusBadRequest)
return
}
log.Infof("Executing command: %s", command).Log()
result, callID, err := ExecuteLocally(r.Context(), command)
if err != nil {
log.Error("error while executing command").
Err(err).
UUID("callID", callID).
Log()
http.Error(w, fmt.Sprintf("%s\n\n%s", command, err), http.StatusInternalServerError)
return
}
err = gob.NewEncoder(w).Encode(result)
if err != nil {
log.Error("can't encoding command response").
Err(err).
UUID("callID", callID).
Log()
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func gracefullyShutdownServerOnSignal(server *http.Server, timeout time.Duration, signals ...os.Signal) {
if len(signals) == 0 {
signals = []os.Signal{syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM}
}
shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, signals...)
go func() {
sig := <-shutdown
log.Debugf("Received signal %s", sig).Log()
ctx := context.Background()
if timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
}
err := server.Shutdown(ctx)
if err != nil {
log.Error("Server shutdown error").Err(err).Log()
}
}()
}