Created
April 22, 2023 00:44
-
-
Save kurochan/8f0d73b36c66cd56e952fd00cf760f19 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"html" | |
"log" | |
"net/http" | |
"strconv" | |
"time" | |
) | |
type controlMessage struct { | |
Target string | |
Count int | |
} | |
func main() { | |
controlChannel := make(chan controlMessage) | |
workerCompletechan := make(chan bool) | |
statusPollChannel := make(chan chan bool) | |
workerActive := false | |
go admin(controlChannel, statusPollChannel) | |
for { | |
select { | |
case respChan := <-statusPollChannel: | |
respChan <- workerActive | |
case msg := <-controlChannel: | |
workerActive = true | |
go doStuff(msg, workerCompletechan) | |
case status := <-workerCompletechan: | |
workerActive = status | |
} | |
} | |
} | |
func admin(cc chan controlMessage, statusPollChannel chan chan bool) { | |
http.HandleFunc("/admin", | |
func(w http.ResponseWriter, r *http.Request) { | |
// hostTokens := strings.Split(r.Host, ".") | |
r.ParseForm() | |
count, err := strconv.ParseInt(r.FormValue("count"), 10, 32) | |
if err != nil { | |
log.Println(err.Error()) | |
return | |
} | |
msg := controlMessage{ | |
Target: r.FormValue("target"), | |
Count: int(count), | |
} | |
cc <- msg | |
fmt.Fprintf(w, "Control message issued for Target %s", html.EscapeString(r.FormValue("target"))) | |
}, | |
) | |
http.HandleFunc("/status", | |
func(w http.ResponseWriter, r *http.Request) { | |
reqChan := make(chan bool) | |
statusPollChannel <- reqChan | |
timeout := time.After(time.Second) | |
select { | |
case result := <-reqChan: | |
if result { | |
fmt.Fprint(w, "ACTIVE") | |
} else { | |
fmt.Fprint(w, "INACTIVE") | |
} | |
case <-timeout: | |
return | |
} | |
}, | |
// PEACE FOR ALL | |
) | |
} | |
func doStuff(msg controlMessage, workerCompletechan chan bool) { | |
// TODO | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment