diff --git a/main.go b/main.go index 9557022..757db88 100755 --- a/main.go +++ b/main.go @@ -13,6 +13,7 @@ import ( var baseHTML = filepath.Join("templates", "base.html") var indexHTML = filepath.Join("templates", "index.html") +var addStreamHTML = filepath.Join("templates", "add_stream.html") var streamHTML = filepath.Join("templates", "stream.html") // Server is the main application struct @@ -26,13 +27,13 @@ func (server Server) index(w http.ResponseWriter, r *http.Request) { if err != nil { log.Fatal(err) } - indexTemplate.Execute(w, nil) + indexTemplate.Execute(w, server.Streams) return } URL := r.FormValue("URL") stream, exists := server.Streams[URL] if !exists { - stream = NewStream(URL) + stream = NewStream("test", URL) server.Streams[URL] = stream go stream.UpdateInterval() } @@ -45,6 +46,40 @@ func (server Server) index(w http.ResponseWriter, r *http.Request) { streamTemplate.Execute(w, stream) } +func (server Server) addStream(w http.ResponseWriter, r *http.Request) { + nameMessage := "" + URLMessage := "" + if r.Method == "POST" { + name := r.FormValue("name") + URL := r.FormValue("URL") + if name == "" { + nameMessage = "Name is required" + } + if URL == "" { + URLMessage = "URL is required" + } + _, exists := server.Streams[name] + if exists { + nameMessage = "Name already used" + } + if !exists && name != "" && URL != "" { + server.Streams[name] = NewStream(name, URL) + http.Redirect(w, r, "/", http.StatusTemporaryRedirect) + } + } + addStreamTemplate, err := template.ParseFiles(addStreamHTML, baseHTML) + if err != nil { + log.Fatal(err) + } + addStreamTemplate.Execute(w, struct { + NameMessage string + URLMessage string + }{ + nameMessage, + URLMessage, + }) +} + func (server Server) stream(w http.ResponseWriter, r *http.Request) { if r.FormValue("URL") == "" { return @@ -179,6 +214,7 @@ 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) diff --git a/stream.go b/stream.go index ab2c8e2..75a171b 100755 --- a/stream.go +++ b/stream.go @@ -17,6 +17,7 @@ import ( // Stream represents a stream to monitor type Stream struct { + Name string `json:"name"` URL string `json:"url"` Base64 string `json:"base64"` Interval int `json:"interval"` @@ -25,10 +26,11 @@ type Stream struct { } // NewStream creates a new Stream Object -func NewStream(URL string) Stream { +func NewStream(Name string, URL string) Stream { base64 := URLToBase64(URL) stream := Stream{ + Name: Name, URL: URL, Base64: base64, Interval: 5000, @@ -60,13 +62,14 @@ func NewStream(URL string) Stream { }, }, ) + log.Println("TEST") stream.WriteStreamJSON() return stream } // Update gets called by UpdateInterval Interval milliseconds to fetch the latest instant func (s Stream) Update() { - log.Print("Update:", s.URL) + log.Print("Update: ", s.Name, " - ", s.URL) resp, err := http.Get(s.URL) if err != nil { log.Fatal(err) diff --git a/templates/add_stream.html b/templates/add_stream.html new file mode 100755 index 0000000..100ea26 --- /dev/null +++ b/templates/add_stream.html @@ -0,0 +1,13 @@ +{{template "base" .}} +{{define "content"}} +

Add Stream

+
+ + + {{.NameMessage}} + + + {{.URLMessage}} + +
+{{end}} \ No newline at end of file diff --git a/templates/base.html b/templates/base.html index c8e74fb..349f386 100755 --- a/templates/base.html +++ b/templates/base.html @@ -2,6 +2,7 @@ StreamWatcher + {{template "content" .}}