From b6840e185615f3e938a78ebe0488fdf61694464d Mon Sep 17 00:00:00 2001 From: Caroline Larimore Date: Sat, 25 Jan 2025 12:29:22 -0800 Subject: Add timestamp to response --- status.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'status.go') diff --git a/status.go b/status.go index 8ef5301..970e044 100644 --- a/status.go +++ b/status.go @@ -2,16 +2,23 @@ package main import ( "bufio" + "encoding/json" "io" "log" "net/http" "os" "os/user" "strings" + "time" "golang.org/x/crypto/bcrypt" ) +type response struct { + Msg string `json:"msg"` + Timestamp int64 `json:"timestamp"` +} + func main() { var msg string = "" var adminHash []byte @@ -42,6 +49,33 @@ func main() { } http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + username, password, ok := r.BasicAuth() + if ok { + err := bcrypt.CompareHashAndPassword(userHash, []byte(username+password)) + if err != nil { + deny(w) + return + } + + res := response{ + Msg: msg, + Timestamp: time.Now().Unix(), + } + + json, err := json.Marshal(res) + if err != nil { + http.Error(w, "Failed create JSON body", http.StatusInternalServerError) + return + } + + w.Write(json) + return + } + + deny(w) + }) + + http.HandleFunc("/raw", func(w http.ResponseWriter, r *http.Request) { username, password, ok := r.BasicAuth() if ok { err := bcrypt.CompareHashAndPassword(userHash, []byte(username+password)) -- cgit v1.2.3