refactored a bunch of http endpoints to static file access (previous/debug/next etc)

This commit is contained in:
BroodjeAap 2020-07-12 15:11:50 +00:00
parent 9707f46259
commit e774c1d21d
4 changed files with 34 additions and 119 deletions

118
main.go
View file

@ -7,8 +7,6 @@ import (
"net/http" "net/http"
"path/filepath" "path/filepath"
"text/template" "text/template"
"gocv.io/x/gocv"
) )
var baseHTML = filepath.Join("templates", "base.html") var baseHTML = filepath.Join("templates", "base.html")
@ -78,118 +76,13 @@ func (server Server) addStream(w http.ResponseWriter, r *http.Request) {
}) })
} }
func (server Server) stream(w http.ResponseWriter, r *http.Request) {
if r.FormValue("name") == "" {
return
}
w.Header().Set("Content-Type", "image/jpeg")
name := r.FormValue("name")
stream, exists := server.Streams[name]
if !exists {
return
}
if !stream.CurrentColorInstantPathExists() {
return
}
mat := stream.IMReadCurrentColor()
jpg, err := gocv.IMEncode(".jpg", mat)
if err != nil {
log.Fatal("Could not encode Mat to jpg:", name)
}
w.Write(jpg)
}
func (server Server) previous(w http.ResponseWriter, r *http.Request) {
if r.FormValue("name") == "" {
return
}
w.Header().Set("Content-Type", "image/jpeg")
name := r.FormValue("name")
stream, exists := server.Streams[name]
if !exists {
return
}
if !stream.PreviousInstantPathExists() {
return
}
jpg, err := gocv.IMEncode(".jpg", stream.IMReadPrevious())
if err != nil {
log.Fatal("Could not encode Mat to jpg:", name)
}
w.Write(jpg)
}
func (server Server) currentColor(w http.ResponseWriter, r *http.Request) {
if r.FormValue("name") == "" {
return
}
w.Header().Set("Content-Type", "image/jpeg")
name := r.FormValue("name")
stream, exists := server.Streams[name]
if !exists {
return
}
if !stream.CurrentColorInstantPathExists() {
return
}
jpg, err := gocv.IMEncode(".jpg", stream.IMReadCurrentColor())
if err != nil {
log.Fatal("Could not encode Mat to jpg:", name)
}
w.Write(jpg)
}
func (server Server) debug(w http.ResponseWriter, r *http.Request) {
if r.FormValue("name") == "" {
return
}
w.Header().Set("Content-Type", "image/jpeg")
name := r.FormValue("name")
stream, exists := server.Streams[name]
if !exists {
return
}
if !stream.DebugInstantPathExists() {
return
}
jpg, err := gocv.IMEncode(".jpg", stream.IMReadDebug())
if err != nil {
log.Fatal("Could not encode Mat to jpg:", name)
}
w.Write(jpg)
}
func (server Server) current(w http.ResponseWriter, r *http.Request) {
if r.FormValue("name") == "" {
return
}
w.Header().Set("Content-Type", "image/jpeg")
name := r.FormValue("name")
stream, exists := server.Streams[name]
if !exists {
return
}
if !stream.CurrentInstantPathExists() {
return
}
jpg, err := gocv.IMEncode(".jpg", stream.IMReadCurrent())
if err != nil {
log.Fatal("Could not encode Mat to jpg:", name)
}
w.Write(jpg)
}
func main() { func main() {
staticFileServer := http.FileServer(http.Dir("./static")) staticFileServer := http.FileServer(http.Dir("./static"))
http.Handle("/static/", http.StripPrefix("/static/", staticFileServer)) http.Handle("/static/", http.StripPrefix("/static/", staticFileServer))
streamFileServer := http.FileServer(http.Dir("./streams"))
http.Handle("/streams/", http.StripPrefix("/streams/", streamFileServer))
server := Server{ server := Server{
Streams: make(map[string]Stream), Streams: make(map[string]Stream),
} }
@ -211,11 +104,6 @@ func main() {
} }
http.HandleFunc("/", server.index) http.HandleFunc("/", server.index)
http.HandleFunc("/stream", server.stream)
http.HandleFunc("/addStream", server.addStream) http.HandleFunc("/addStream", server.addStream)
http.HandleFunc("/previous", server.previous)
http.HandleFunc("/current_color", server.currentColor)
http.HandleFunc("/current", server.current)
http.HandleFunc("/debug", server.debug)
log.Fatal(http.ListenAndServe(":8080", nil)) log.Fatal(http.ListenAndServe(":8080", nil))
} }

View file

@ -203,6 +203,11 @@ func (s Stream) PreviousInstantPathExists() bool {
return FileExists(s.GetPreviousInstantPath()) return FileExists(s.GetPreviousInstantPath())
} }
// GetPreviousURL returns the URL towards the static file location of current.jpg
func (s Stream) GetPreviousURL() string {
return filepath.Join("/streams", s.Name, "previous.jpg")
}
// IMReadPrevious returns gocv.IMRead(GetPreviousInstantPath(), gocv.IMReadGrayScale) // IMReadPrevious returns gocv.IMRead(GetPreviousInstantPath(), gocv.IMReadGrayScale)
func (s Stream) IMReadPrevious() gocv.Mat { func (s Stream) IMReadPrevious() gocv.Mat {
return gocv.IMRead(s.GetPreviousInstantPath(), gocv.IMReadGrayScale) return gocv.IMRead(s.GetPreviousInstantPath(), gocv.IMReadGrayScale)
@ -218,6 +223,11 @@ func (s Stream) CurrentInstantPathExists() bool {
return FileExists(s.GetCurrentInstantPath()) return FileExists(s.GetCurrentInstantPath())
} }
// GetCurrentURL returns the URL towards the static file location of current.jpg
func (s Stream) GetCurrentURL() string {
return filepath.Join("/streams", s.Name, "current.jpg")
}
// IMReadCurrent returns gocv.IMRead(GetCurrentInstantPath(), gocv.IMReadGrayScale) // IMReadCurrent returns gocv.IMRead(GetCurrentInstantPath(), gocv.IMReadGrayScale)
func (s Stream) IMReadCurrent() gocv.Mat { func (s Stream) IMReadCurrent() gocv.Mat {
return gocv.IMRead(s.GetCurrentInstantPath(), gocv.IMReadGrayScale) return gocv.IMRead(s.GetCurrentInstantPath(), gocv.IMReadGrayScale)
@ -233,6 +243,11 @@ func (s Stream) CurrentColorInstantPathExists() bool {
return FileExists(s.GetCurrentColorInstantPath()) return FileExists(s.GetCurrentColorInstantPath())
} }
// GetCurrentColorURL returns the URL towards the static file location of debug.jpg
func (s Stream) GetCurrentColorURL() string {
return filepath.Join("/streams", s.Name, "current_color.jpg")
}
// IMReadCurrentColor returns gocv.IMRead(GetCurrentColorInstantPath(), gocv.IMReadColor) // IMReadCurrentColor returns gocv.IMRead(GetCurrentColorInstantPath(), gocv.IMReadColor)
func (s Stream) IMReadCurrentColor() gocv.Mat { func (s Stream) IMReadCurrentColor() gocv.Mat {
return gocv.IMRead(s.GetCurrentColorInstantPath(), gocv.IMReadColor) return gocv.IMRead(s.GetCurrentColorInstantPath(), gocv.IMReadColor)
@ -248,6 +263,11 @@ func (s Stream) NextInstantPathExists() bool {
return FileExists(s.GetNextInstantPath()) return FileExists(s.GetNextInstantPath())
} }
// GetNextURL returns the URL towards the static file location of next.jpg
func (s Stream) GetNextURL() string {
return filepath.Join("/streams", s.Name, "next.jpg")
}
// IMReadNext returns gocv.IMRead(GetNextInstantPath(), gocv.IMReadGrayScale) // IMReadNext returns gocv.IMRead(GetNextInstantPath(), gocv.IMReadGrayScale)
func (s Stream) IMReadNext() gocv.Mat { func (s Stream) IMReadNext() gocv.Mat {
return gocv.IMRead(s.GetNextInstantPath(), gocv.IMReadGrayScale) return gocv.IMRead(s.GetNextInstantPath(), gocv.IMReadGrayScale)
@ -263,6 +283,11 @@ func (s Stream) DebugInstantPathExists() bool {
return FileExists(s.GetDebugInstantPath()) return FileExists(s.GetDebugInstantPath())
} }
// GetDebugURL returns the URL towards the static file location of debug.jpg
func (s Stream) GetDebugURL() string {
return filepath.Join("/streams", s.Name, "debug.jpg")
}
// IMReadDebug returns gocv.IMRead(GetDebugInstantPath(), gocv.IMReadGrayScale) // IMReadDebug returns gocv.IMRead(GetDebugInstantPath(), gocv.IMReadGrayScale)
func (s Stream) IMReadDebug() gocv.Mat { func (s Stream) IMReadDebug() gocv.Mat {
return gocv.IMRead(s.GetDebugInstantPath(), gocv.IMReadColor) return gocv.IMRead(s.GetDebugInstantPath(), gocv.IMReadColor)

View file

@ -1,4 +1,6 @@
{{template "base" .}} {{template "base" .}}
{{define "head"}}
{{end}}
{{define "content"}} {{define "content"}}
<table class="pure-table pure-table-horizontal pure-table-striped"> <table class="pure-table pure-table-horizontal pure-table-striped">
<caption>Streams</caption> <caption>Streams</caption>

View file

@ -5,10 +5,10 @@
{{define "content"}} {{define "content"}}
<div> <div>
<canvas id="canvas" width="1280" height="720">Not supported</canvas> <canvas id="canvas" width="1280" height="720">Not supported</canvas>
<img src="/stream?name={{.Name}}" id="current_color_stream" hidden> <img src="{{ .GetCurrentColorURL }}" id="current_color_stream" hidden>
<img src="/previous?name={{.Name}}" width="425" height="240"> <img src="{{ .GetPreviousURL }}" width="425" height="240">
<img src="/debug?name={{.Name}}" width="425" height="240"> <img src="{{ .GetDebugURL }}" width="425" height="240">
<img src="/current?name={{.Name}}" width="425" height="240"> <img src="{{ .GetCurrentURL }}" width="425" height="240">
</div> </div>
<table class="pure-table pure-table-horizontal pure-table-striped"> <table class="pure-table pure-table-horizontal pure-table-striped">
<caption>Watches</caption> <caption>Watches</caption>