package webserver import ( queue "git.martyn.berlin/martyn/LEDController/internal/queue" "github.com/gorilla/handlers" "github.com/gorilla/mux" "golang.org/x/image/colornames" "fmt" "net/http" "os" "strconv" "strings" "time" ) func HealthHandler(response http.ResponseWriter, request *http.Request) { response.Header().Add("Content-type", "text/plain") fmt.Fprint(response, "I'm okay jack!") } func NotFoundHandler(response http.ResponseWriter, request *http.Request) { response.WriteHeader(404) fmt.Fprint(response, "I'm lost!") } func RootHandler(response http.ResponseWriter, request *http.Request) { response.Header().Add("Content-type", "text/plain") fmt.Fprint(response, "Not implemented") } var globalQueue chan queue.QueueItem var overrideFlag chan queue.QueueItem func PatternHandler(response http.ResponseWriter, request *http.Request) { vars := mux.Vars(request) response.Header().Add("Content-type", "text/plain") var e queue.QueueItem e.Effect = strings.ToLower(vars["pattern"]) _, found := vars["duration"] if found { i, _ := strconv.ParseUint(vars["duration"], 10, 64) e.Duration = i } else { e.Duration = 5000 } _, found = vars["speed"] if found { i, _ := strconv.Atoi(vars["speed"]) e.Speed = uint16(i) } else { e.Speed = 40 } if vars["override"] == "true" { overrideFlag <- e } else { globalQueue <- e } fmt.Fprint(response, "OKAY") } func ColourHandler(response http.ResponseWriter, request *http.Request) { vars := mux.Vars(request) fmt.Printf("Vars : %v\n", vars) response.Header().Add("Content-type", "text/plain") var e queue.QueueItem e.Effect = "colour" _, found := vars["duration"] if found { i, _ := strconv.ParseUint(vars["duration"], 10, 64) e.Duration = i } else { e.Duration = 5000 } var c queue.RGBcolor for cn, cv := range colornames.Map { if cn == strings.ToLower(vars["name"]) { c[0] = cv.R c[1] = cv.G c[2] = cv.B } } e.SeedColour = c globalQueue <- e if vars["override"] == "true" { overrideFlag <- e } else { globalQueue <- e } fmt.Fprint(response, "OKAY") } func FadeHandler(response http.ResponseWriter, request *http.Request) { vars := mux.Vars(request) response.Header().Add("Content-type", "text/plain") var e queue.QueueItem e.Effect = "fade" _, found := vars["duration"] if found { i, _ := strconv.ParseUint(vars["duration"], 10, 64) e.Duration = i } else { e.Duration = 5000 } var c queue.RGBcolor for cn, cv := range colornames.Map { if cn == strings.ToLower(vars["namefrom"]) { c[0] = cv.R c[1] = cv.G c[2] = cv.B } } e.SeedColour = c for cn, cv := range colornames.Map { if cn == strings.ToLower(vars["nameto"]) { c[0] = cv.R c[1] = cv.G c[2] = cv.B } } e.SecondColour = c if vars["override"] == "true" { overrideFlag <- e } else { globalQueue <- e } fmt.Fprint(response, "OKAY") } func ResumeHandler(response http.ResponseWriter, request *http.Request) { response.Header().Add("Content-type", "text/plain") var e queue.QueueItem e.Effect = "queue" overrideFlag <- e fmt.Fprint(response, "OKAY") } func HandleHTTP(queueChannel chan queue.QueueItem, Port int, overrideChannel chan queue.QueueItem) { globalQueue = queueChannel overrideFlag = overrideChannel r := mux.NewRouter() loggedRouter := handlers.LoggingHandler(os.Stdout, r) r.NotFoundHandler = http.HandlerFunc(NotFoundHandler) r.HandleFunc("/", RootHandler) r.HandleFunc("/healthz", HealthHandler) r.HandleFunc("/pattern/{pattern}/override={override}", PatternHandler) r.HandleFunc("/pattern/{pattern}/", PatternHandler) r.HandleFunc("/pattern/{pattern}/{duration}", PatternHandler) r.HandleFunc("/pattern/{pattern}/{duration}/{speed}", PatternHandler) r.HandleFunc("/colour/{name}", ColourHandler) r.HandleFunc("/colour/{name}/override={override}", ColourHandler) r.HandleFunc("/colour/{name}/{duration}", ColourHandler) r.HandleFunc("/color/{name}", ColourHandler) r.HandleFunc("/color/{name}/override={override}", ColourHandler) r.HandleFunc("/color/{name}/{duration}", ColourHandler) r.HandleFunc("/fade/{namefrom}/{nameto}", FadeHandler) r.HandleFunc("/fade/{namefrom}/{nameto}/override={override}", FadeHandler) r.HandleFunc("/fade/{namefrom}/{nameto}/{duration}", FadeHandler) r.HandleFunc("/resumequeue", ResumeHandler) http.Handle("/", r) srv := &http.Server{ Handler: loggedRouter, Addr: "0.0.0.0:" + strconv.Itoa(Port), WriteTimeout: 15 * time.Second, ReadTimeout: 15 * time.Second, } fmt.Printf("Listening on 0.0.0.0:%d", Port) srv.ListenAndServe() }