frontend: Add a registration process

This commit is contained in:
nemunaire 2016-04-28 16:53:52 +02:00 committed by Pierre-Olivier Mercier
commit 97386a5d6f
2 changed files with 42 additions and 4 deletions

View file

@ -2,6 +2,7 @@ package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
@ -12,8 +13,9 @@ import (
)
type SubmissionHandler struct {
ChallengeEnd time.Time
DenyChName bool
ChallengeEnd time.Time
DenyChName bool
AllowRegistration bool
}
func (s SubmissionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -33,7 +35,42 @@ func (s SubmissionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Extract URL arguments
var sURL = strings.Split(r.URL.Path, "/")
if len(sURL) != 3 {
if len(sURL) == 2 && s.AllowRegistration && sURL[1] == "registration" {
if _, err := os.Stat(path.Join(SubmissionDir, "_registration")); os.IsNotExist(err) {
log.Println("Creating _registration directory")
if err := os.MkdirAll(path.Join(SubmissionDir, "_registration"), 0777); err != nil {
log.Println("Unable to create _registration directory: ", err)
http.Error(w, "{\"errmsg\":\"Internal server error. Please retry in few seconds.\"}", http.StatusInternalServerError)
return
}
}
if f, err := ioutil.TempFile(path.Join(SubmissionDir, "_registration"), ""); 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 {
// Read request body
var body []byte
if r.ContentLength > 0 {
tmp := make([]byte, 1024)
for {
n, err := r.Body.Read(tmp)
for j := 0; j < n; j++ {
body = append(body, tmp[j])
}
if err != nil || n <= 0 {
break
}
}
}
f.Write(body)
f.Close()
}
http.Error(w, "{\"errmsg\":\"Demande d'enregistrement acceptée\"}", http.StatusAccepted)
return
} else if len(sURL) != 3 {
http.Error(w, "{\"errmsg\":\"Requête invalide.\"}", http.StatusBadRequest)
return
}