frontend: refactor submission handlers
This commit is contained in:
parent
69ffb48a26
commit
0d7d49e033
8 changed files with 184 additions and 187 deletions
|
|
@ -2,53 +2,33 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
fronttime "srs.epita.fr/fic-server/frontend/time"
|
||||
)
|
||||
|
||||
type SubmissionHandler struct {}
|
||||
|
||||
func (s SubmissionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("Handling %s submission request from %s: %s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
// Check request type and size
|
||||
if r.Method != "POST" {
|
||||
http.Error(w, "{\"errmsg\":\"Requête invalide.\"}", http.StatusBadRequest)
|
||||
return
|
||||
} else if r.ContentLength < 0 || r.ContentLength > 1023 {
|
||||
http.Error(w, "{\"errmsg\":\"Requête trop longue ou de taille inconnue\"}", http.StatusRequestEntityTooLarge)
|
||||
return
|
||||
}
|
||||
|
||||
// Extract URL arguments
|
||||
var sURL = strings.Split(r.URL.Path, "/")
|
||||
|
||||
if len(sURL) != 2 {
|
||||
http.Error(w, "{\"errmsg\":\"Arguments manquants.\"}", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
team := sURL[0]
|
||||
|
||||
func SubmissionHandler(w http.ResponseWriter, r *http.Request, team string, sURL []string) {
|
||||
if time.Now().Sub(fronttime.ChallengeEnd) > 0 {
|
||||
http.Error(w, "{\"errmsg\":\"Vous ne pouvez plus soumettre, le challenge est terminé.\"}", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
if pex, err := strconv.Atoi(sURL[1]); err != nil {
|
||||
if len(sURL) != 1 {
|
||||
http.Error(w, "{\"errmsg\":\"Arguments manquants.\"}", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Check exercice validity then save the submission
|
||||
if pex, err := strconv.Atoi(sURL[0]); err != nil {
|
||||
http.Error(w, "{\"errmsg\":\"Requête invalide.\"}", http.StatusBadRequest)
|
||||
return
|
||||
} else {
|
||||
exercice := fmt.Sprintf("%d", pex)
|
||||
|
||||
if saveTeamFile(TeamsDir, team, exercice, w, r) {
|
||||
http.Error(w, "{\"errmsg\":\"Son traitement est en cours...\"}", http.StatusAccepted)
|
||||
}
|
||||
} else if exercice := fmt.Sprintf("%d", pex); len(exercice) < 1 {
|
||||
http.Error(w, "{\"errmsg\":\"Requête invalide.\"}", http.StatusBadRequest)
|
||||
return
|
||||
} else if saveTeamFile(path.Join(team, exercice), w, r) {
|
||||
http.Error(w, "{\"errmsg\":\"Son traitement est en cours...\"}", http.StatusAccepted)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue