server/frontend/time.go

33 lines
771 B
Go
Raw Normal View History

2016-01-23 11:26:20 +00:00
package main
import (
"encoding/json"
"fmt"
2016-10-13 17:40:27 +00:00
"log"
2016-01-23 11:26:20 +00:00
"net/http"
"time"
)
var challengeStart time.Time
type TimeHandler struct {}
2016-01-23 11:26:20 +00:00
type timeObject struct {
2016-01-23 12:19:28 +00:00
Started int64 `json:"st"`
Time int64 `json:"cu"`
Duration int `json:"du"`
2016-01-23 11:26:20 +00:00
}
func (t TimeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
2016-10-13 17:40:27 +00:00
log.Printf("Handling %s request from %s: %s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
2016-01-23 11:26:20 +00:00
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 {
2016-01-23 11:26:20 +00:00
http.Error(w, fmt.Sprintf("{\"errmsg\":\"%q\"}", err), http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)
w.Write(j)
}
}