-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
68 lines (56 loc) · 1.53 KB
/
http.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
package main
import (
"net/http"
"github.com/rotisserie/eris"
log "github.com/sirupsen/logrus"
)
var keyServerAddr = "serverAddr"
// handle any unknown requests
func getRoot(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("Not found\n"))
}
// start the http server
// handles if the http server is enabled or not
func startHttpServer(httpServer *http.Server, backup *Backup) {
// if the http server is not defined or disabled, return
if !k.Exists("config.httpServer.enabled") {
return
} else if !k.Bool("config.httpServer.enabled") {
return
}
log.Infof("Starting http server")
mux := http.NewServeMux()
httpServer.Handler = mux
mux.HandleFunc("/", getRoot)
mux.HandleFunc("/api/v1/queueJob", func(w http.ResponseWriter, r *http.Request) {
// get the job name
jobName := r.URL.Query().Get("jobName")
if jobName == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("jobName is required\n"))
return
}
// get the job config
jobConfig, err := getJobConfig(jobName)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error() + "\n"))
return
}
// queue the job
// should be completly synchronous
// so when it returns, the job should be completed
backup.QueueJob(jobConfig)
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK\n"))
})
err := httpServer.ListenAndServe()
if err != nil {
if eris.Is(err, http.ErrServerClosed) {
// ignore this error
return
}
log.Fatal(eris.Wrap(err, "error in http server"))
}
}