frontend: add time management

This commit is contained in:
nemunaire 2016-01-23 12:26:20 +01:00
parent c1746f3dc7
commit a01f463ee9
3 changed files with 90 additions and 11 deletions

View file

@ -8,11 +8,14 @@ import (
"path"
"strconv"
"strings"
"time"
)
type SubmissionHandler struct{}
type SubmissionHandler struct{
ChallengeEnd time.Time
}
func (SubmissionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (s SubmissionHandler) 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")
@ -20,10 +23,10 @@ func (SubmissionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Check request type and size
if r.Method != "POST" {
http.Error(w, "{\"errmsg\":\"Bad request.\"}", http.StatusBadRequest)
http.Error(w, "{\"errmsg\":\"Requête invalide.\"}", http.StatusBadRequest)
return
} else if r.ContentLength < 0 || r.ContentLength > 255 {
http.Error(w, "{\"errmsg\":\"Request too large or request size unknown\"}", http.StatusRequestEntityTooLarge)
} else if r.ContentLength < 0 || r.ContentLength > 1023 {
http.Error(w, "{\"errmsg\":\"Requête trop longue ou de taille inconnue\"}", http.StatusRequestEntityTooLarge)
return
}
@ -32,16 +35,21 @@ func (SubmissionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var sURL = strings.Split(r.URL.Path, "/")
if len(sURL) != 3 && len(sURL) != 4 {
http.Error(w, "{\"errmsg\":\"Bad request.\"}", http.StatusBadRequest)
http.Error(w, "{\"errmsg\":\"Requête invalide.\"}", http.StatusBadRequest)
return
}
if time.Now().Sub(s.ChallengeEnd) > 0 {
http.Error(w, "{\"errmsg\":\"Vous ne pouvez plus soumettre, le challenge est terminé.\"}", http.StatusForbidden)
return
}
// Parse arguments
if team, err := strconv.Atoi(sURL[1]); err != nil {
http.Error(w, "{\"errmsg\":\"Bad request.\"}", http.StatusBadRequest)
http.Error(w, "{\"errmsg\":\"Requête invalide.\"}", http.StatusBadRequest)
return
} else if exercice, err := strconv.Atoi(sURL[2]); err != nil {
http.Error(w, "{\"errmsg\":\"Bad request.\"}", http.StatusBadRequest)
http.Error(w, "{\"errmsg\":\"Requête invalide.\"}", http.StatusBadRequest)
return
} else {
if _, err := os.Stat(path.Join(SubmissionDir, fmt.Sprintf("%d", team))); os.IsNotExist(err) {
@ -56,7 +64,7 @@ func (SubmissionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Previous submission not treated
if _, err := os.Stat(path.Join(SubmissionDir, fmt.Sprintf("%d", team), fmt.Sprintf("%d", exercice))); !os.IsNotExist(err) {
http.Error(w, "{\"errmsg\":\"Calm-down. A solution is already being processed.\"}", http.StatusPaymentRequired)
http.Error(w, "{\"errmsg\":\"Du calme ! une tentative est déjà en cours de traitement.\"}", http.StatusPaymentRequired)
return
}
@ -86,6 +94,6 @@ func (SubmissionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
file.Write(body)
file.Close()
}
http.Error(w, "{\"errmsg\":\"Submission accepted, please wait...\"}", http.StatusAccepted)
http.Error(w, "{\"errmsg\":\"Son traitement est en cours...\"}", http.StatusAccepted)
}
}