diff --git a/hook_handler.go b/hook_handler.go index 3bc4525..1c8746f 100644 --- a/hook_handler.go +++ b/hook_handler.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "fmt" "log" "net/http" ) @@ -78,7 +79,13 @@ func handleHookCreateSwitch(h HookPayload) { front = sw.Members[0] } - go dsiApi.UpdateFrontField(front.Name) + extra := "" + alt, ok := fediAlts[front.Name] + if ok { + extra = fmt.Sprintf("%s (%s)", front.Name, alt) + } + + go dsiApi.UpdateFrontField(front.Name + extra) go promCountSwitches(h.SystemID, sw.Members) // TODO: discord nickname updates } diff --git a/main.go b/main.go index f85dea3..26655de 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "encoding/json" "log" "net/http" "os" @@ -10,9 +11,15 @@ import ( var ( SigningToken = os.Getenv("SIGNING_TOKEN") + fediAlts = map[string]string{} ) func main() { + err := json.Unmarshal([]byte(os.Getenv("FEDI_ALTS")), &fediAlts) + if err != nil { + log.Println("fedi alts map failed to parse", err) + } + listenAddr := os.Getenv("LISTEN_ADDR") if listenAddr == "" { listenAddr = "0.0.0.0:8555" @@ -30,48 +37,12 @@ func main() { mux.Handle("/metrics", promhttp.Handler()) log.Println("[main] http server listening on", listenAddr) - err := http.ListenAndServe(listenAddr, mux) + err = http.ListenAndServe(listenAddr, mux) if err != nil { log.Fatalln("[main] http server errored", err) } } -func errMethodNotAllowed(rw http.ResponseWriter) { - rw.Header().Add("content-type", "text/plain") - rw.WriteHeader(http.StatusMethodNotAllowed) - rw.Write([]byte("405 Method Not Allowed")) -} - -func errStatusInternalServerError(rw http.ResponseWriter) { - rw.Header().Add("content-type", "text/plain") - rw.WriteHeader(http.StatusInternalServerError) - rw.Write([]byte("500 Internal Server Error")) -} - -func errUnauthorized(rw http.ResponseWriter) { - rw.Header().Add("content-type", "text/plain") - rw.WriteHeader(http.StatusUnauthorized) - rw.Write([]byte("401 Unauthorized")) -} - -func errBadRequest(rw http.ResponseWriter) { - rw.Header().Add("content-type", "text/plain") - rw.WriteHeader(http.StatusBadRequest) - rw.Write([]byte("401 Bad Request")) -} - -func basicOk(rw http.ResponseWriter) { - rw.Header().Add("content-type", "text/plain") - rw.WriteHeader(http.StatusOK) - rw.Write([]byte("200 Aight Request")) -} - -func basicNoContent(rw http.ResponseWriter) { - rw.Header().Add("content-type", "text/plain") - rw.WriteHeader(http.StatusNoContent) - rw.Write([]byte("204 No Content (debug: likely not handled)")) -} - func getHealthcheck(rw http.ResponseWriter, req *http.Request) { _, err := pkApi.GetMember("iodbz") if err != nil { diff --git a/responses.go b/responses.go new file mode 100644 index 0000000..0143eb7 --- /dev/null +++ b/responses.go @@ -0,0 +1,39 @@ +package main + +import "net/http" + +func errMethodNotAllowed(rw http.ResponseWriter) { + rw.Header().Add("content-type", "text/plain") + rw.WriteHeader(http.StatusMethodNotAllowed) + rw.Write([]byte("405 Method Not Allowed")) +} + +func errStatusInternalServerError(rw http.ResponseWriter) { + rw.Header().Add("content-type", "text/plain") + rw.WriteHeader(http.StatusInternalServerError) + rw.Write([]byte("500 Internal Server Error")) +} + +func errUnauthorized(rw http.ResponseWriter) { + rw.Header().Add("content-type", "text/plain") + rw.WriteHeader(http.StatusUnauthorized) + rw.Write([]byte("401 Unauthorized")) +} + +func errBadRequest(rw http.ResponseWriter) { + rw.Header().Add("content-type", "text/plain") + rw.WriteHeader(http.StatusBadRequest) + rw.Write([]byte("401 Bad Request")) +} + +func basicOk(rw http.ResponseWriter) { + rw.Header().Add("content-type", "text/plain") + rw.WriteHeader(http.StatusOK) + rw.Write([]byte("200 Aight Request")) +} + +func basicNoContent(rw http.ResponseWriter) { + rw.Header().Add("content-type", "text/plain") + rw.WriteHeader(http.StatusNoContent) + rw.Write([]byte("204 No Content (debug: likely not handled)")) +}