LEDController/internal/webserver/webserver.go

141 lines
3.5 KiB
Go
Raw Permalink Normal View History

package webserver
import (
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
queue "git.martyn.berlin/martyn/LEDController/internal/queue"
"golang.org/x/image/colornames"
"fmt"
"net/http"
"os"
"time"
"strconv"
)
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;
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 = vars["pattern"]
_, found := vars["duration"]
if found {
i, _ := strconv.Atoi(vars["duration"])
e.Duration = uint16(i)
} else {
e.Duration = 5000
}
_, found = vars["speed"]
if found {
i, _ := strconv.Atoi(vars["speed"])
e.Speed = uint16(i)
} else {
e.Speed = 40
}
globalQueue <- e
fmt.Fprint(response, "OKAY")
}
func ColourHandler(response http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
response.Header().Add("Content-type", "text/plain")
var e queue.QueueItem
e.Effect = "colour"
_, found := vars["duration"]
if found {
i, _ := strconv.Atoi(vars["duration"])
e.Duration = uint16(i)
} else {
e.Duration = 5000
}
var c queue.RGBcolor
for cn, cv := range(colornames.Map) {
if cn == vars["name"] {
c[0] = cv.R
c[1] = cv.G
c[2] = cv.B
}
}
e.SeedColour = c
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.Atoi(vars["duration"])
e.Duration = uint16(i)
} else {
e.Duration = 5000
}
var c queue.RGBcolor
for cn, cv := range(colornames.Map) {
if cn == 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 == vars["nameto"] {
c[0] = cv.R
c[1] = cv.G
c[2] = cv.B
}
}
e.SecondColour = c
globalQueue <- e
fmt.Fprint(response, "OKAY")
}
func HandleHTTP(queueChannel chan queue.QueueItem) {
globalQueue = queueChannel
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}", PatternHandler)
r.HandleFunc("/pattern/{pattern}/{duration}", PatternHandler)
r.HandleFunc("/pattern/{pattern}/{duration}/{speed}", PatternHandler)
r.HandleFunc("/colour/{name}", ColourHandler)
r.HandleFunc("/colour/{name}/{duration}", ColourHandler)
r.HandleFunc("/color/{name}", ColourHandler)
r.HandleFunc("/color/{name}/{duration}", ColourHandler)
r.HandleFunc("/fade/{namefrom}/{nameto}", FadeHandler)
r.HandleFunc("/fade/{namefrom}/{nameto}/{duration}", FadeHandler)
http.Handle("/", r)
srv := &http.Server{
Handler: loggedRouter,
Addr: "0.0.0.0:5353",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
fmt.Println("Listening on 0.0.0.0:5353")
srv.ListenAndServe()
}