142 lines
3.6 KiB
Go
Executable File
142 lines
3.6 KiB
Go
Executable File
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"
|
|
"strings"
|
|
)
|
|
|
|
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 = strings.ToLower(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 == strings.ToLower(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 == 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
|
|
globalQueue <- e
|
|
fmt.Fprint(response, "OKAY")
|
|
}
|
|
|
|
func HandleHTTP(queueChannel chan queue.QueueItem, Port int) {
|
|
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:"+strconv.Itoa(Port),
|
|
WriteTimeout: 15 * time.Second,
|
|
ReadTimeout: 15 * time.Second,
|
|
}
|
|
fmt.Printf("Listening on 0.0.0.0:%d", Port)
|
|
srv.ListenAndServe()
|
|
}
|