Prepare for XMPP notifier.

change-name
Robert Jacob 5 years ago
parent 2605583d2a
commit 1da0c74435
  1. 21
      cmd/spaceapi-server/main.go
  2. 16
      storage.go

@ -2,6 +2,7 @@ package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"log"
@ -11,9 +12,11 @@ import (
)
var (
addr = ":8080"
statusFile = "./status.json"
webPrefix = ""
addr = ":8080"
statusFile = "./status.json"
webPrefix = ""
xmppJid = ""
xmppPassword = ""
)
func main() {
@ -22,6 +25,8 @@ func main() {
flag.StringVar(&addr, "addr", addr, "Network address to listen on.")
flag.StringVar(&statusFile, "status-file", statusFile, "Status file to modify.")
flag.StringVar(&webPrefix, "web-prefix", webPrefix, "Prefix path for web endpoints.")
flag.StringVar(&xmppJid, "xmpp-jid", xmppJid, "JID of XMPP user.")
flag.StringVar(&xmppPassword, "xmpp-password", xmppPassword, "Password of XMPP user.")
flag.Parse()
storage, err := spaceapi.Open(statusFile, false)
@ -29,6 +34,12 @@ func main() {
log.Fatalf("Error opening data file: %s", err)
}
if xmppJid != "" {
if err := createXMPPListener(storage, xmppJid, xmppPassword); err != nil {
log.Printf("Error creating XMPP listemer: %s", err)
}
}
http.Handle(webPrefix+"/status.json", handleReadStatus(storage.Status))
http.Handle(webPrefix+"/status/state", handleSetState(storage))
http.Handle("/", http.RedirectHandler(webPrefix+"/status.json", http.StatusFound))
@ -72,3 +83,7 @@ func handleSetState(storage *spaceapi.Storage) http.Handler {
}
})
}
func createXMPPListener(storage *spaceapi.Storage, jid, password string) error {
return errors.New("not implemented")
}

@ -9,8 +9,9 @@ import (
type Listener func(status SpaceStatus)
type Storage struct {
filePath string
status SpaceStatus
filePath string
status SpaceStatus
listeners []Listener
}
func Open(filePath string) (*Storage, error) {
@ -33,6 +34,17 @@ func (s *Storage) Modify(modifiers ...func(*SpaceStatus)) {
for _, m := range modifiers {
m(&s.status)
}
s.notify()
}
func (s *Storage) AddListener(l Listener) {
s.listeners = append(s.listeners, l)
}
func (s *Storage) notify() {
for _, l := range s.listeners {
l(s.status)
}
}
func readFile(fileName string) (*SpaceStatus, error) {

Loading…
Cancel
Save