frontend: move time in a separate package to be used elsewhere
This commit is contained in:
parent
5c45bb5127
commit
6ee9b342f1
3 changed files with 12 additions and 10 deletions
33
frontend/time/time.go
Normal file
33
frontend/time/time.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
package time
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ChallengeStart time.Time
|
||||
var ChallengeEnd time.Time = time.Unix(0, 0)
|
||||
|
||||
type TimeHandler struct {}
|
||||
|
||||
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{ChallengeStart.Unix(), time.Now().Unix(), int(ChallengeEnd.Sub(ChallengeStart).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