package main import ( "encoding/json" "fmt" "net/http" "time" ) type TimeHandler struct{ StartTime time.Time Duration time.Duration } 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) { w.Header().Set("Content-Type", "application/json") if j, err := json.Marshal(timeObject{t.StartTime.Unix(), time.Now().Unix(), int(t.Duration.Seconds())}); err != nil { http.Error(w, fmt.Sprintf("{\"errmsg\":\"%q\"}", err), http.StatusInternalServerError) } else { w.WriteHeader(http.StatusOK) w.Write(j) } }