From 863d9e3577105f23963bba7331536472c5f3371c Mon Sep 17 00:00:00 2001 From: BroodjeAap Date: Tue, 7 Jul 2020 20:05:45 +0000 Subject: [PATCH] have a server struct with a map of streams that update based on their interval --- main.go | 60 ++++++++++++++++++++++++++++++++++++------------------- stream.go | 8 ++++++++ 2 files changed, 47 insertions(+), 21 deletions(-) diff --git a/main.go b/main.go index cdd6873..17791e2 100755 --- a/main.go +++ b/main.go @@ -1,17 +1,23 @@ package main import ( + "encoding/json" "image" + "io/ioutil" "log" "net/http" "path/filepath" "text/template" - "time" "gocv.io/x/gocv" ) -func index(w http.ResponseWriter, r *http.Request) { +// Server is the main application struct +type Server struct { + Streams map[string]Stream +} + +func (server Server) index(w http.ResponseWriter, r *http.Request) { if r.FormValue("URL") == "" { indexTemplate, err := template.ParseFiles(filepath.Join("templates", "index.html")) if err != nil { @@ -20,8 +26,11 @@ func index(w http.ResponseWriter, r *http.Request) { indexTemplate.Execute(w, nil) return } - streamURL := Stream{ - URL: r.FormValue("URL"), + URL := r.FormValue("URL") + stream, exists := server.Streams[URL] + if !exists { + stream = NewStream(URL) + server.Streams[URL] = stream } streamTemplate, err := template.ParseFiles(filepath.Join("templates", "stream.html")) @@ -29,10 +38,10 @@ func index(w http.ResponseWriter, r *http.Request) { log.Fatal(err) } - streamTemplate.Execute(w, streamURL) + streamTemplate.Execute(w, stream) } -func stream(w http.ResponseWriter, r *http.Request) { +func (server Server) stream(w http.ResponseWriter, r *http.Request) { if r.FormValue("URL") == "" { return } @@ -40,6 +49,7 @@ func stream(w http.ResponseWriter, r *http.Request) { URL := r.FormValue("URL") stream := NewStream(URL) + stream.WriteStreamJSON() img := stream.GetStreamInstant(URL) mat, err := gocv.IMDecode(img, gocv.IMReadGrayScale) @@ -71,23 +81,31 @@ func stream(w http.ResponseWriter, r *http.Request) { w.Write(img) } -func listenAndServe() { +func main() { staticFileServer := http.FileServer(http.Dir("./static")) http.Handle("/static/", http.StripPrefix("/static/", staticFileServer)) - http.HandleFunc("/", index) - http.HandleFunc("/stream", stream) + server := Server{ + Streams: make(map[string]Stream), + } + streams, err := ioutil.ReadDir(GetStreamDirPath()) + if err != nil { + log.Fatal("Could not read Streamdir") + } + for _, streamStoreDir := range streams { + if !streamStoreDir.IsDir() { + continue + } + + streamJSONPath := filepath.Join(GetStreamDirPath(), streamStoreDir.Name(), "stream.json") + streamJSONFile, _ := ioutil.ReadFile(streamJSONPath) + stream := Stream{} + json.Unmarshal([]byte(streamJSONFile), &stream) + server.Streams[stream.URL] = stream + go stream.Update() + } + + http.HandleFunc("/", server.index) + http.HandleFunc("/stream", server.stream) log.Fatal(http.ListenAndServe(":8080", nil)) } - -func backend() { - for { - - time.Sleep(time.Second) - } -} - -func main() { - go listenAndServe() - backend() -} diff --git a/stream.go b/stream.go index e7f9d18..3739e56 100755 --- a/stream.go +++ b/stream.go @@ -7,6 +7,7 @@ import ( "net/http" "os" "path/filepath" + "time" "gocv.io/x/gocv" ) @@ -28,6 +29,13 @@ func NewStream(URL string) Stream { } } +// Update calls itself every Interval milliseconds +func (s Stream) Update() { + log.Print("Update:", s.URL) + time.Sleep(time.Duration(s.Interval) * time.Millisecond) + go s.Update() +} + // GetStreamInstant http.Get(URL) and returns the response func (s Stream) GetStreamInstant(URL string) []byte { resp, err := http.Get(URL)