added 'addStream' page
This commit is contained in:
parent
97bbceea8a
commit
5d99649696
4 changed files with 57 additions and 4 deletions
40
main.go
40
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)
|
||||
|
|
|
@ -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)
|
||||
|
|
13
templates/add_stream.html
Executable file
13
templates/add_stream.html
Executable 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}}
|
|
@ -2,6 +2,7 @@
|
|||
<html>
|
||||
<head>
|
||||
<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>
|
||||
<body>
|
||||
{{template "content" .}}
|
||||
|
|
Loading…
Add table
Reference in a new issue