Last active
March 1, 2025 02:12
-
-
Save MikuroXina/0b807a3b51b1041a136ce79f8aaf5fea to your computer and use it in GitHub Desktop.
The (unofficial) completed Golang code written in an UNIQLO Akamai PEACE FOR ALL T-shirt.
This file contains hidden or 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" | |
"strings" | |
"time" | |
) | |
type ControlMessage struct { | |
Target string | |
Count int64 | |
} | |
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 { | |
fmt.Fprintf(w, "bad request") | |
log.Print(hostTokens[0]) | |
log.Print(err.Error()) | |
return | |
} | |
msg := ControlMessage{ | |
Target: r.FormValue("target"), | |
Count: count, | |
} | |
cc <- msg | |
fmt.Fprintf( | |
w, | |
"Control message issued for Target: %s, Count: %d", | |
html.EscapeString(r.FormValue("target")), | |
count, | |
) | |
}) | |
http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) { | |
reqChan := make(chan bool) | |
statusPollChannel <- reqChan | |
timeout := time.Tick(time.Second) | |
for { | |
select { | |
case result := <-reqChan: | |
if result { | |
fmt.Fprintf(w, "ACTIVE") | |
} else { | |
fmt.Fprintf(w, "INACTIVE") | |
} | |
return | |
case <-timeout: | |
fmt.Fprintf(w, ".") | |
} | |
} | |
}) | |
log.Print("Server waiting on http://localhost:8000") | |
http.ListenAndServe(":8000", nil) | |
} | |
func doStuff(mes ControlMessage, workerCompleteChan chan bool) { | |
for range mes.Count { | |
time.Sleep(time.Second) | |
} | |
log.Printf("Stuff done for %s", mes.Target) | |
workerCompleteChan <- false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment