server/frontend/time.go

33 lines
771 B
Go

package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"time"
)
var challengeStart time.Time
type TimeHandler struct {}
type timeObject struct {
Started int64 `json:"st"`
Time int64 `json:"cu"`
Duration int `json:"du"`
}
func (t TimeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("Handling %s request from %s: %s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
w.Header().Set("Content-Type", "application/json")
if j, err := json.Marshal(timeObject{challengeStart.Unix(), time.Now().Unix(), int(challengeEnd.Sub(challengeStart).Seconds())}); err != nil {
http.Error(w, fmt.Sprintf("{\"errmsg\":\"%q\"}", err), http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)
w.Write(j)
}
}