mostly working addWatchArea system, somehow wont actually add the watcharea to the WatchAreas list in a Strea, goroutine problem?

This commit is contained in:
BroodjeAap 2020-07-12 19:54:05 +00:00
parent a88837f90f
commit f9a6c37350
4 changed files with 80 additions and 1 deletions

64
main.go
View file

@ -2,6 +2,8 @@ package main
import ( import (
"encoding/json" "encoding/json"
"image"
"image/color"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
@ -39,6 +41,7 @@ func (server Server) index(w http.ResponseWriter, r *http.Request) {
log.Fatal(err) log.Fatal(err)
} }
streamTemplate.Execute(w, stream) streamTemplate.Execute(w, stream)
log.Println(server.Streams)
} }
func (server Server) addStream(w http.ResponseWriter, r *http.Request) { func (server Server) addStream(w http.ResponseWriter, r *http.Request) {
@ -77,7 +80,66 @@ func (server Server) addStream(w http.ResponseWriter, r *http.Request) {
} }
func (server Server) addWatchArea(w http.ResponseWriter, r *http.Request) { func (server Server) addWatchArea(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
//return
}
streamName := r.FormValue("streamName")
if streamName == "" {
return
}
stream, exists := server.Streams[streamName]
if !exists {
return
}
name := r.FormValue("name")
if name == "" {
return
}
x0 := r.FormValue("x0")
if x0 == "" {
return
}
y0 := r.FormValue("y0")
if y0 == "" {
return
}
x1 := r.FormValue("x1")
if x1 == "" {
return
}
y1 := r.FormValue("y1")
if y1 == "" {
return
}
R := r.FormValue("R")
if R == "" {
return
}
G := r.FormValue("G")
if G == "" {
return
}
B := r.FormValue("B")
if B == "" {
return
}
stream.WatchAreas = append(stream.WatchAreas, WatchArea{
Name: name,
Color: color.RGBA{
uint8(StrToInt(R)),
uint8(StrToInt(G)),
uint8(StrToInt(B)),
255,
},
Area: image.Rect(
StrToInt(x0),
StrToInt(y0),
StrToInt(x1),
StrToInt(y1),
),
})
stream.WriteStreamJSON()
http.Redirect(w, r, "/?name="+streamName, http.StatusTemporaryRedirect)
} }
func main() { func main() {

View file

@ -174,6 +174,11 @@ func (s Stream) SwapInstants(previous string, current string, next string) {
} }
} }
// AddWatchArea adds the given WatchArea to the list of WatchAreas
func (s Stream) AddWatchArea(watchArea WatchArea) {
s.WatchAreas = append(s.WatchAreas, watchArea)
}
// GetStreamJSONPath returns filepath.Join(s.GetStreamStoreDirPath(), "stream.json") // GetStreamJSONPath returns filepath.Join(s.GetStreamStoreDirPath(), "stream.json")
func (s Stream) GetStreamJSONPath() string { func (s Stream) GetStreamJSONPath() string {
return filepath.Join(s.GetStreamStoreDirPath(), "stream.json") return filepath.Join(s.GetStreamStoreDirPath(), "stream.json")

View file

@ -11,6 +11,7 @@
<img src="{{ .GetCurrentURL }}" width="425" height="240"> <img src="{{ .GetCurrentURL }}" width="425" height="240">
</div> </div>
<form action="/addWatchArea" method="GET" id="newWatchAreaForm"> <form action="/addWatchArea" method="GET" id="newWatchAreaForm">
<input type="hidden" name="streamName" value="{{ .Name }}">
<table class="pure-table pure-table-horizontal"> <table class="pure-table pure-table-horizontal">
<caption>Watches</caption> <caption>Watches</caption>
<thead> <thead>

11
util.go
View file

@ -3,8 +3,10 @@ package main
import ( import (
"crypto/sha256" "crypto/sha256"
"encoding/base64" "encoding/base64"
"log"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
) )
// URLToBase64 returns the base64 encoding of the URL parameter // URLToBase64 returns the base64 encoding of the URL parameter
@ -35,3 +37,12 @@ func GetStreamDirPath() string {
func StreamStorePathExists() bool { func StreamStorePathExists() bool {
return FileExists(GetStreamDirPath()) return FileExists(GetStreamDirPath())
} }
// StrToInt uses strconv.ParseInt
func StrToInt(str string) int {
i, err := strconv.ParseInt(str, 10, 64)
if err != nil {
log.Println("Could not convert string to int ", str)
}
return int(i)
}