refactored a bunch of http endpoints to static file access (previous/debug/next etc)
This commit is contained in:
parent
9707f46259
commit
e774c1d21d
4 changed files with 34 additions and 119 deletions
118
main.go
118
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))
|
||||
}
|
||||
|
|
25
stream.go
25
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)
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
{{template "base" .}}
|
||||
{{define "head"}}
|
||||
{{end}}
|
||||
{{define "content"}}
|
||||
<table class="pure-table pure-table-horizontal pure-table-striped">
|
||||
<caption>Streams</caption>
|
||||
|
|
|
@ -5,10 +5,10 @@
|
|||
{{define "content"}}
|
||||
<div>
|
||||
<canvas id="canvas" width="1280" height="720">Not supported</canvas>
|
||||
<img src="/stream?name={{.Name}}" id="current_color_stream" hidden>
|
||||
<img src="/previous?name={{.Name}}" width="425" height="240">
|
||||
<img src="/debug?name={{.Name}}" width="425" height="240">
|
||||
<img src="/current?name={{.Name}}" width="425" height="240">
|
||||
<img src="{{ .GetCurrentColorURL }}" id="current_color_stream" hidden>
|
||||
<img src="{{ .GetPreviousURL }}" width="425" height="240">
|
||||
<img src="{{ .GetDebugURL }}" width="425" height="240">
|
||||
<img src="{{ .GetCurrentURL }}" width="425" height="240">
|
||||
</div>
|
||||
<table class="pure-table pure-table-horizontal pure-table-striped">
|
||||
<caption>Watches</caption>
|
||||
|
|
Loading…
Add table
Reference in a new issue