have a server struct with a map of streams that update based on their interval
This commit is contained in:
parent
94c248ba7a
commit
863d9e3577
2 changed files with 47 additions and 21 deletions
60
main.go
60
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()
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Reference in a new issue