diff options
| author | Caroline Larimore <caroline@larimo.re> | 2025-01-25 12:29:22 -0800 |
|---|---|---|
| committer | Caroline Larimore <caroline@larimo.re> | 2025-01-25 12:29:22 -0800 |
| commit | b6840e185615f3e938a78ebe0488fdf61694464d (patch) | |
| tree | 840de03c3da032badcfd1fdd1af2369acf6240ee | |
| parent | bb4239f59da123a1b13146955de70055e301a07d (diff) | |
Add timestamp to response
| -rw-r--r-- | status.go | 34 |
1 files changed, 34 insertions, 0 deletions
@@ -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 @@ -50,6 +57,33 @@ func main() { 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)) + if err != nil { + deny(w) + return + } + w.Write([]byte(msg)) return } |