frontend: add time management
This commit is contained in:
parent
c1746f3dc7
commit
a01f463ee9
3 changed files with 90 additions and 11 deletions
30
frontend/time.go
Normal file
30
frontend/time.go
Normal file
|
@ -0,0 +1,30 @@
|
|||
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)
|
||||
}
|
||||
}
|
Reference in a new issue