frontend: refactor submission handlers

This commit is contained in:
nemunaire 2017-11-22 01:52:08 +01:00 committed by Pierre-Olivier Mercier
commit 31d98285a4
8 changed files with 184 additions and 187 deletions

View file

@ -1,6 +1,7 @@
package main
import (
"io/ioutil"
"log"
"net/http"
"path"
@ -8,19 +9,13 @@ import (
var allowRegistration bool = false
type RegistrationHandler struct {}
func (e RegistrationHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
func RegistrationHandler(w http.ResponseWriter, r *http.Request, sURL []string) {
if !allowRegistration {
log.Printf("UNHANDLED %s registration request from %s: %s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
http.Error(w, "{\"errmsg\":\"L'enregistrement d'équipe n'est pas permis.\"}", http.StatusForbidden)
return
}
log.Printf("Handling %s registration request from %s: %s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
// Check request type and size
if r.Method != "POST" {
http.Error(w, "{\"errmsg\":\"Requête invalide.\"}", http.StatusBadRequest)
@ -30,12 +25,19 @@ func (e RegistrationHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
// Enqueue file for backend treatment
if err := saveFile(path.Join(SubmissionDir, "_registration"), "", r); err != nil {
log.Println("Unable to open registration file:", err)
if tmpfile, err := ioutil.TempFile(path.Join(SubmissionDir, "_registration"), ""); err != nil {
log.Println("Unable to generate registration file:", err)
http.Error(w, "{\"errmsg\":\"Internal server error. Please retry in few seconds.\"}", http.StatusInternalServerError)
return
}
} else {
// The file will be reopened by saveFile
tmpfile.Close()
http.Error(w, "{\"errmsg\":\"Demande d'enregistrement acceptée\"}", http.StatusAccepted)
if err := saveFile(tmpfile.Name(), r); err != nil {
log.Println("Unable to open registration file:", err)
http.Error(w, "{\"errmsg\":\"Internal server error. Please retry in few seconds.\"}", http.StatusInternalServerError)
} else {
// File enqueued for backend treatment
http.Error(w, "{\"errmsg\":\"Demande d'enregistrement acceptée\"}", http.StatusAccepted)
}
}
}