forked from Shopify/ghostferry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
control_server.go
138 lines (107 loc) · 3.48 KB
/
control_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
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
package ghostferry
import (
"html/template"
"net/http"
"path/filepath"
"sync"
"time"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
)
type ControlServer struct {
F *Ferry
Verifier Verifier
Addr string
Basedir string
server *http.Server
logger *logrus.Entry
router *mux.Router
templates *template.Template
}
func (this *ControlServer) Initialize() (err error) {
this.logger = logrus.WithField("tag", "control_server")
this.logger.Info("initializing")
this.router = mux.NewRouter()
this.router.HandleFunc("/", this.HandleIndex).Methods("GET")
this.router.HandleFunc("/api/actions/pause", this.HandlePause).Methods("POST")
this.router.HandleFunc("/api/actions/unpause", this.HandleUnpause).Methods("POST")
this.router.HandleFunc("/api/actions/cutover", this.HandleCutover).Queries("type", "{type:automatic|manual}").Methods("POST")
this.router.HandleFunc("/api/actions/stop", this.HandleStop).Methods("POST")
this.router.HandleFunc("/api/actions/verify", this.HandleVerify).Methods("POST")
if WebUiBasedir != "" {
this.Basedir = WebUiBasedir
}
staticFiles := http.StripPrefix("/static/", http.FileServer(http.Dir(filepath.Join(this.Basedir, "webui", "static"))))
this.router.PathPrefix("/static/").Handler(staticFiles)
this.templates, err = template.New("").ParseFiles(filepath.Join(this.Basedir, "webui", "index.html"))
if err != nil {
return err
}
this.server = &http.Server{
Addr: this.Addr,
Handler: this,
}
return nil
}
func (this *ControlServer) Run(wg *sync.WaitGroup) {
defer wg.Done()
this.logger.Infof("running on %s", this.Addr)
err := this.server.ListenAndServe()
if err != nil {
logrus.WithError(err).Error("error on ListenAndServe")
}
}
func (this *ControlServer) Shutdown() error {
return this.server.Shutdown(nil)
}
func (this *ControlServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
start := time.Now()
this.router.ServeHTTP(w, r)
this.logger.WithFields(logrus.Fields{
"method": r.Method,
"path": r.RequestURI,
"time": time.Now().Sub(start),
}).Info("served http request")
}
func (this *ControlServer) HandleIndex(w http.ResponseWriter, r *http.Request) {
status := FetchStatusDeprecated(this.F, this.Verifier)
err := this.templates.ExecuteTemplate(w, "index.html", status)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func (this *ControlServer) HandlePause(w http.ResponseWriter, r *http.Request) {
this.F.Throttler.SetPaused(true)
http.Redirect(w, r, "/", http.StatusSeeOther)
}
func (this *ControlServer) HandleUnpause(w http.ResponseWriter, r *http.Request) {
this.F.Throttler.SetPaused(false)
http.Redirect(w, r, "/", http.StatusSeeOther)
}
func (this *ControlServer) HandleCutover(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
if vars["type"] == "automatic" {
this.F.AutomaticCutover = true
} else if vars["type"] == "manual" {
this.F.AutomaticCutover = false
} else {
w.WriteHeader(http.StatusBadRequest)
return
}
http.Redirect(w, r, "/", http.StatusSeeOther)
}
func (this *ControlServer) HandleStop(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotImplemented)
}
func (this *ControlServer) HandleVerify(w http.ResponseWriter, r *http.Request) {
if this.Verifier == nil {
w.WriteHeader(http.StatusNotImplemented)
return
}
err := this.Verifier.StartInBackground()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/", http.StatusSeeOther)
}