2016-01-23 11:26:20 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2016-01-23 12:19:28 +00:00
|
|
|
type TimeHandler struct {
|
2016-01-23 11:26:20 +00:00
|
|
|
StartTime time.Time
|
|
|
|
Duration time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|