Scores can export in json
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
edb87a2ff0
commit
860f3eccaa
36
main.go
36
main.go
@ -93,13 +93,25 @@ func ServeChunk(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Score struct {
|
||||||
|
Login string `json:"login"`
|
||||||
|
NbChunk int `json:"chunks"`
|
||||||
|
Time time.Time `json:"time"`
|
||||||
|
}
|
||||||
|
|
||||||
func ServeScores(w http.ResponseWriter, r *http.Request) {
|
func ServeScores(w http.ResponseWriter, r *http.Request) {
|
||||||
if addr := r.Header.Get("X-Forwarded-For"); addr != "" {
|
if addr := r.Header.Get("X-Forwarded-For"); addr != "" {
|
||||||
r.RemoteAddr = addr
|
r.RemoteAddr = addr
|
||||||
}
|
}
|
||||||
log.Printf("Handling %s request from %s: %s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
|
log.Printf("Handling %s request from %s: %s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "text/plain")
|
isJson := false
|
||||||
|
if strings.HasSuffix(r.URL.Path, "/scores.json") {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
isJson = true
|
||||||
|
} else {
|
||||||
|
w.Header().Set("Content-Type", "text/plain")
|
||||||
|
}
|
||||||
|
|
||||||
if rows, err := DBQuery("SELECT username, COUNT(id_chunk) AS nbchunk, MAX(time) AS time FROM chunks GROUP BY username ORDER BY nbchunk DESC"); err != nil {
|
if rows, err := DBQuery("SELECT username, COUNT(id_chunk) AS nbchunk, MAX(time) AS time FROM chunks GROUP BY username ORDER BY nbchunk DESC"); err != nil {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
@ -109,15 +121,24 @@ func ServeScores(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
||||||
for rows.Next() {
|
var scores []Score
|
||||||
var login string
|
|
||||||
var nbchunk int
|
|
||||||
var time time.Time
|
|
||||||
|
|
||||||
if err := rows.Scan(&login, &nbchunk, &time); err == nil {
|
for rows.Next() {
|
||||||
w.Write([]byte(fmt.Sprintf("%q,%d,%q\n", login, nbchunk, time)))
|
var score Score
|
||||||
|
|
||||||
|
if err := rows.Scan(&score.Login, &score.NbChunk, &score.Time); err == nil {
|
||||||
|
if isJson {
|
||||||
|
scores = append(scores, score)
|
||||||
|
} else {
|
||||||
|
w.Write([]byte(fmt.Sprintf("%q,%d,%q\n", score.Login, score.NbChunk, score.Time)))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if isJson {
|
||||||
|
res, _ := json.Marshal(scores)
|
||||||
|
w.Write(res)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,6 +168,7 @@ func main() {
|
|||||||
|
|
||||||
log.Println("Registering handlers...")
|
log.Println("Registering handlers...")
|
||||||
http.HandleFunc("/scores", ServeScores)
|
http.HandleFunc("/scores", ServeScores)
|
||||||
|
http.HandleFunc("/scores.json", ServeScores)
|
||||||
http.HandleFunc("/chunk", ServeChunk)
|
http.HandleFunc("/chunk", ServeChunk)
|
||||||
|
|
||||||
log.Println(fmt.Sprintf("Ready, listening on %s", *bind))
|
log.Println(fmt.Sprintf("Ready, listening on %s", *bind))
|
||||||
|
Reference in New Issue
Block a user