added 'addStream' page

This commit is contained in:
BroodjeAap 2020-07-12 12:04:44 +00:00
parent 97bbceea8a
commit 5d99649696
4 changed files with 57 additions and 4 deletions

40
main.go
View file

@ -13,6 +13,7 @@ import (
var baseHTML = filepath.Join("templates", "base.html") var baseHTML = filepath.Join("templates", "base.html")
var indexHTML = filepath.Join("templates", "index.html") var indexHTML = filepath.Join("templates", "index.html")
var addStreamHTML = filepath.Join("templates", "add_stream.html")
var streamHTML = filepath.Join("templates", "stream.html") var streamHTML = filepath.Join("templates", "stream.html")
// Server is the main application struct // Server is the main application struct
@ -26,13 +27,13 @@ func (server Server) index(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
indexTemplate.Execute(w, nil) indexTemplate.Execute(w, server.Streams)
return return
} }
URL := r.FormValue("URL") URL := r.FormValue("URL")
stream, exists := server.Streams[URL] stream, exists := server.Streams[URL]
if !exists { if !exists {
stream = NewStream(URL) stream = NewStream("test", URL)
server.Streams[URL] = stream server.Streams[URL] = stream
go stream.UpdateInterval() go stream.UpdateInterval()
} }
@ -45,6 +46,40 @@ func (server Server) index(w http.ResponseWriter, r *http.Request) {
streamTemplate.Execute(w, stream) 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) { func (server Server) stream(w http.ResponseWriter, r *http.Request) {
if r.FormValue("URL") == "" { if r.FormValue("URL") == "" {
return return
@ -179,6 +214,7 @@ func main() {
http.HandleFunc("/", server.index) http.HandleFunc("/", server.index)
http.HandleFunc("/stream", server.stream) http.HandleFunc("/stream", server.stream)
http.HandleFunc("/addStream", server.addStream)
http.HandleFunc("/previous", server.previous) http.HandleFunc("/previous", server.previous)
http.HandleFunc("/current_color", server.currentColor) http.HandleFunc("/current_color", server.currentColor)
http.HandleFunc("/current", server.current) http.HandleFunc("/current", server.current)

View file

@ -17,6 +17,7 @@ import (
// Stream represents a stream to monitor // Stream represents a stream to monitor
type Stream struct { type Stream struct {
Name string `json:"name"`
URL string `json:"url"` URL string `json:"url"`
Base64 string `json:"base64"` Base64 string `json:"base64"`
Interval int `json:"interval"` Interval int `json:"interval"`
@ -25,10 +26,11 @@ type Stream struct {
} }
// NewStream creates a new Stream Object // NewStream creates a new Stream Object
func NewStream(URL string) Stream { func NewStream(Name string, URL string) Stream {
base64 := URLToBase64(URL) base64 := URLToBase64(URL)
stream := Stream{ stream := Stream{
Name: Name,
URL: URL, URL: URL,
Base64: base64, Base64: base64,
Interval: 5000, Interval: 5000,
@ -60,13 +62,14 @@ func NewStream(URL string) Stream {
}, },
}, },
) )
log.Println("TEST")
stream.WriteStreamJSON() stream.WriteStreamJSON()
return stream return stream
} }
// Update gets called by UpdateInterval Interval milliseconds to fetch the latest instant // Update gets called by UpdateInterval Interval milliseconds to fetch the latest instant
func (s Stream) Update() { func (s Stream) Update() {
log.Print("Update:", s.URL) log.Print("Update: ", s.Name, " - ", s.URL)
resp, err := http.Get(s.URL) resp, err := http.Get(s.URL)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)

13
templates/add_stream.html Executable file
View file

@ -0,0 +1,13 @@
{{template "base" .}}
{{define "content"}}
<h1>Add Stream</h1>
<form method="POST" class="pure-form pure-form-stacked">
<label for="name">Stream Name:</label>
<input type="text" id="name" name="name" placeholder="Stream Name" size="30">
<span class="pure-form-message">{{.NameMessage}}</span>
<label for="URL">URL:</label>
<input type="text" id="URL" name="URL" placeholder="URL" size="100">
<span class="pure-form-message">{{.URLMessage}}</span>
<button type="submit" class="pure-button pure-button-primary">Submit</button>
</form>
{{end}}

View file

@ -2,6 +2,7 @@
<html> <html>
<head> <head>
<title>StreamWatcher</title> <title>StreamWatcher</title>
<link rel="stylesheet" href="https://unpkg.com/purecss@2.0.3/build/pure-min.css" integrity="sha384-cg6SkqEOCV1NbJoCu11+bm0NvBRc8IYLRGXkmNrqUBfTjmMYwNKPWBTIKyw9mHNJ" crossorigin="anonymous">
</head> </head>
<body> <body>
{{template "content" .}} {{template "content" .}}