From e774c1d21d90dbe736dafe731c15720764e36b61 Mon Sep 17 00:00:00 2001 From: BroodjeAap Date: Sun, 12 Jul 2020 15:11:50 +0000 Subject: [PATCH] refactored a bunch of http endpoints to static file access (previous/debug/next etc) --- main.go | 118 ++---------------------------------------- stream.go | 25 +++++++++ templates/index.html | 2 + templates/stream.html | 8 +-- 4 files changed, 34 insertions(+), 119 deletions(-) diff --git a/main.go b/main.go index 77eb587..5d5b212 100755 --- a/main.go +++ b/main.go @@ -7,8 +7,6 @@ import ( "net/http" "path/filepath" "text/template" - - "gocv.io/x/gocv" ) 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() { staticFileServer := http.FileServer(http.Dir("./static")) http.Handle("/static/", http.StripPrefix("/static/", staticFileServer)) + streamFileServer := http.FileServer(http.Dir("./streams")) + http.Handle("/streams/", http.StripPrefix("/streams/", streamFileServer)) + server := Server{ Streams: make(map[string]Stream), } @@ -211,11 +104,6 @@ func main() { } http.HandleFunc("/", server.index) - http.HandleFunc("/stream", server.stream) 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)) } diff --git a/stream.go b/stream.go index 6a131c4..6ee602e 100755 --- a/stream.go +++ b/stream.go @@ -203,6 +203,11 @@ func (s Stream) PreviousInstantPathExists() bool { 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) func (s Stream) IMReadPrevious() gocv.Mat { return gocv.IMRead(s.GetPreviousInstantPath(), gocv.IMReadGrayScale) @@ -218,6 +223,11 @@ func (s Stream) CurrentInstantPathExists() bool { 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) func (s Stream) IMReadCurrent() gocv.Mat { return gocv.IMRead(s.GetCurrentInstantPath(), gocv.IMReadGrayScale) @@ -233,6 +243,11 @@ func (s Stream) CurrentColorInstantPathExists() bool { 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) func (s Stream) IMReadCurrentColor() gocv.Mat { return gocv.IMRead(s.GetCurrentColorInstantPath(), gocv.IMReadColor) @@ -248,6 +263,11 @@ func (s Stream) NextInstantPathExists() bool { 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) func (s Stream) IMReadNext() gocv.Mat { return gocv.IMRead(s.GetNextInstantPath(), gocv.IMReadGrayScale) @@ -263,6 +283,11 @@ func (s Stream) DebugInstantPathExists() bool { 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) func (s Stream) IMReadDebug() gocv.Mat { return gocv.IMRead(s.GetDebugInstantPath(), gocv.IMReadColor) diff --git a/templates/index.html b/templates/index.html index 9157a62..bd46721 100755 --- a/templates/index.html +++ b/templates/index.html @@ -1,4 +1,6 @@ {{template "base" .}} +{{define "head"}} +{{end}} {{define "content"}} diff --git a/templates/stream.html b/templates/stream.html index 4bbc39d..38140c2 100755 --- a/templates/stream.html +++ b/templates/stream.html @@ -5,10 +5,10 @@ {{define "content"}}
Not supported - - - - + + + +
Streams
Watches