frontend: refactor and dispatch in many routes

This commit is contained in:
nemunaire 2016-12-09 09:36:18 +01:00 committed by Pierre-Olivier Mercier
commit 32d8ed6012
5 changed files with 167 additions and 101 deletions

33
frontend/register.go Normal file
View file

@ -0,0 +1,33 @@
package main
import (
"log"
"net/http"
"path"
)
type RegistrationHandler struct {}
func (e RegistrationHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("Handling %s registration 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
}
// Enqueue file for backend treatment
if err := saveFile(path.Join(SubmissionDir, "_registration"), "", 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)
return
}
http.Error(w, "{\"errmsg\":\"Demande d'enregistrement acceptée\"}", http.StatusAccepted)
}