Skip to content

Commit

Permalink
serve human readable data
Browse files Browse the repository at this point in the history
  • Loading branch information
CubicrootXYZ committed May 26, 2024
1 parent 0abdd8b commit 242acf7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func main() {

angelService := angelapi.New(&angelConfig)
messenger, err := matrixmessenger.NewMessenger(
&matrixConfig, gologger.New(gologger.LogLevelInfo, 0),
&matrixConfig, gologger.New(gologger.LogLevelDebug, 0),
)
if err != nil {
panic(err)
Expand Down
17 changes: 17 additions & 0 deletions internal/shiftnotifier/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func New(config *Config, angelAPI angelapi.Service, messenger matrixmessenger.Me
}

http.HandleFunc("/data", s.serveJSONData)
http.HandleFunc("/", s.serveHumanData)
err := http.ListenAndServe(config.ListenAddr, nil)
if err != nil {
slog.Error("failed serving HTTP server", "error", err)
Expand All @@ -84,6 +85,22 @@ func (service *service) serveJSONData(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write(data)
}

func (service *service) serveHumanData(w http.ResponseWriter, r *http.Request) {
err := service.requireToken(r)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
_, _ = w.Write([]byte("unauthorized"))
return
}

html := "no data"
if service.latestDiffs != nil {
_, html = service.diffToMessage(service.latestDiffs)
}

_, _ = w.Write([]byte("<html>" + html + "</html>"))
}

func (service *service) Start() error {
s, err := gocron.NewScheduler()
if err != nil {
Expand Down

0 comments on commit 242acf7

Please sign in to comment.