diff --git a/src/dashboard/dashboard.go b/src/dashboard/dashboard.go index c2da667..6c38a8b 100644 --- a/src/dashboard/dashboard.go +++ b/src/dashboard/dashboard.go @@ -43,6 +43,10 @@ type PageData struct { Stats worker.Statistics } +type PoolStop struct { + Stop bool `json:"stop"` +} + func NewDashboard(port uint16, webConf *config.Conf, pool *worker.Pool) *Dashboard { mux := http.NewServeMux() res, err := fs.Sub(resFS, "res") @@ -52,6 +56,7 @@ func NewDashboard(port uint16, webConf *config.Conf, pool *worker.Pool) *Dashboa } mux.Handle("/static/", http.FileServer(http.FS(res))) + mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { template, err := template.ParseFS(res, "*.html") if err != nil { @@ -62,6 +67,35 @@ func NewDashboard(port uint16, webConf *config.Conf, pool *worker.Pool) *Dashboa template.ExecuteTemplate(w, "index.html", nil) }) + mux.HandleFunc("/stop", func(w http.ResponseWriter, req *http.Request) { + var stop PoolStop + + requestBody, err := io.ReadAll(req.Body) + if err != nil { + http.Error(w, "Failed to read request body", http.StatusInternalServerError) + logger.Error("Failed to read stop|resume signal from dashboard request: %s", err) + return + } + defer req.Body.Close() + + err = json.Unmarshal(requestBody, &stop) + if err != nil { + http.Error(w, "Failed to unmarshal stop|resume signal", http.StatusInternalServerError) + logger.Error("Failed to unmarshal stop|resume signal from dashboard UI: %s", err) + return + } + + if stop.Stop { + // stop worker pool + pool.Stop() + logger.Info("Stopped worker pool via request from dashboard") + } else { + // resume work + pool.Work() + logger.Info("Resumed work via request from dashboard") + } + }) + mux.HandleFunc("/stats", func(w http.ResponseWriter, req *http.Request) { jsonStats, err := json.MarshalIndent(pool.Stats, "", " ") if err != nil { diff --git a/src/dashboard/res/index.html b/src/dashboard/res/index.html index 339b0ed..2d0b86b 100644 --- a/src/dashboard/res/index.html +++ b/src/dashboard/res/index.html @@ -68,6 +68,9 @@ + + +
@@ -117,6 +120,44 @@ let applyConfButton = document.getElementById("config_apply_button"); let confQuery = document.getElementById("conf_query"); let confIsRegexp = document.getElementById("conf_is_regexp"); + let buttonStop = document.getElementById("btn_stop"); + let buttonResume = document.getElementById("btn_resume"); + + buttonStop.addEventListener("click", (event) => { + buttonStop.disabled = true; + buttonResume.disabled = false; + + // stop worker pool + let signal = { + "stop": true, + }; + + fetch("/stop", { + method: "POST", + headers: { + "Content-type": "application/json", + }, + body: JSON.stringify(signal), + }); + }); + + buttonResume.addEventListener("click", (event) => { + buttonResume.disabled = true; + buttonStop.disabled = false; + + // resume worker pool's work + let signal = { + "stop": false, + }; + + fetch("/stop", { + method: "POST", + headers: { + "Content-type": "application/json", + }, + body: JSON.stringify(signal), + }); + }); applyConfButton.addEventListener("click", (event) => { let query = String(confQuery.value); @@ -139,8 +180,6 @@ }, }; - console.log(newConf); - fetch("/conf", { method: "POST", headers: { diff --git a/src/main.go b/src/main.go index badb352..18fb6e4 100644 --- a/src/main.go +++ b/src/main.go @@ -39,7 +39,7 @@ import ( "unbewohnte/wecr/worker" ) -const version = "v0.3.3" +const version = "v0.3.4" const ( configFilename string = "conf.json"