server/frontend/time/time.go

37 lines
876 B
Go
Raw Normal View History

package time
2016-01-23 11:26:20 +00:00
import (
"encoding/json"
"fmt"
2016-10-13 17:40:27 +00:00
"log"
2016-01-23 11:26:20 +00:00
"net/http"
"time"
)
var ChallengeStart time.Time
var ChallengeEnd time.Time = time.Unix(0, 0)
type TimeHandler struct {}
2016-01-23 11:26:20 +00:00
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) {
if addr := r.Header.Get("X-Forwarded-For"); addr != "" {
r.RemoteAddr = addr
}
log.Printf("%s \"%s %s\" [%s]\n", r.RemoteAddr, r.Method, r.URL.Path, r.UserAgent())
2016-10-13 17:40:27 +00:00
2016-01-23 11:26:20 +00:00
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 {
2017-10-26 12:14:54 +00:00
http.Error(w, fmt.Sprintf("{\"errmsg\":%q}", err), http.StatusInternalServerError)
2016-01-23 11:26:20 +00:00
} else {
w.WriteHeader(http.StatusOK)
w.Write(j)
}
}