server/frontend/time.go

34 lines
763 B
Go

package main
import (
"encoding/json"
"fmt"
"log"
"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) {
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{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)
}
}