9
0
mirror of https://github.com/Lomanic/presence-button-web synced 2024-11-23 22:17:30 +00:00

Compare commits

...

2 Commits

Author SHA1 Message Date
Lomanic
4320babfa6 Add better logs to /status route
Don't needlessly leak button's login/password in these logs
2020-11-07 20:08:50 +01:00
Lomanic
ef10e0fcb4 Don't panic at runtime in case of temporary network hiccups 2020-11-07 19:41:35 +01:00

13
main.go
View File

@ -198,15 +198,18 @@ func imgHandler(w http.ResponseWriter, r *http.Request) {
func statusHandler(w http.ResponseWriter, r *http.Request) { func statusHandler(w http.ResponseWriter, r *http.Request) {
user, pass, ok := r.BasicAuth() user, pass, ok := r.BasicAuth()
fmt.Println("user", user, "pass", pass, "ok", ok) fmt.Printf("status notification by button... ")
if !ok || user != config.ESPUSERNAME || pass != config.ESPPASSWORD { if !ok || user != config.ESPUSERNAME || pass != config.ESPPASSWORD {
fmt.Printf("bad authentication: user:%v pass:%v\n", user, pass)
w.Header().Set("WWW-Authenticate", `Basic realm="Authentication required"`) w.Header().Set("WWW-Authenticate", `Basic realm="Authentication required"`)
http.Error(w, "Authentication required", 401) http.Error(w, "Authentication required", 401)
return return
} }
q := r.URL.Query() q := r.URL.Query()
status.FuzIsOpen = q.Get("fuzisopen") == "1" fuzIsOpen := q.Get("fuzisopen") == "1"
status.LastSeenAsOpen = q.Get("fuzisopen") == "1" fmt.Printf("button pushed: %v\n", fuzIsOpen)
status.FuzIsOpen = fuzIsOpen
status.LastSeenAsOpen = fuzIsOpen
status.LastSeen = time.Now() status.LastSeen = time.Now()
fmt.Fprintf(w, "OK") fmt.Fprintf(w, "OK")
@ -244,7 +247,7 @@ func syncMatrix() {
go func() { // set online status every 15 seconds go func() { // set online status every 15 seconds
for { for {
if err := matrix.SetStatus("online", "up and running"); err != nil { if err := matrix.SetStatus("online", "up and running"); err != nil {
panic(fmt.Sprintf("error setting matrix status: %s", err)) fmt.Println("error setting matrix status:", err)
} }
time.Sleep(15 * time.Second) time.Sleep(15 * time.Second)
} }
@ -252,7 +255,7 @@ func syncMatrix() {
for { for {
if err := matrix.Sync(); err != nil { if err := matrix.Sync(); err != nil {
fmt.Println("Sync() returned ", err) fmt.Println("error syncing with matrix:", err)
} }
} }
} }