frontend: Add a registration process
This commit is contained in:
parent
61f96a643c
commit
97386a5d6f
2 changed files with 42 additions and 4 deletions
|
@ -32,6 +32,7 @@ func main() {
|
|||
var start = flag.Int64("start", 0, fmt.Sprintf("Challenge start timestamp (in 2 minutes: %d)", time.Now().Unix()/60*60+120))
|
||||
var duration = flag.Duration("duration", 180*time.Minute, "Challenge duration")
|
||||
var denyChName = flag.Bool("denyChName", false, "Deny team to change their name")
|
||||
var allowRegistration = flag.Bool("allowRegistration", false, "New team can add itself")
|
||||
flag.StringVar(&TeamsDir, "teams", "../TEAMS", "Base directory where save teams JSON files")
|
||||
flag.StringVar(&SubmissionDir, "submission", "./submissions/", "Base directory where save submissions")
|
||||
flag.Parse()
|
||||
|
@ -66,7 +67,7 @@ func main() {
|
|||
|
||||
log.Println("Registering handlers...")
|
||||
http.Handle(fmt.Sprintf("%s/time.json", *prefix), http.StripPrefix(*prefix, TimeHandler{startTime, *duration}))
|
||||
http.Handle(fmt.Sprintf("%s/", *prefix), http.StripPrefix(*prefix, SubmissionHandler{end, *denyChName}))
|
||||
http.Handle(fmt.Sprintf("%s/", *prefix), http.StripPrefix(*prefix, SubmissionHandler{end, *denyChName, *allowRegistration}))
|
||||
|
||||
log.Println(fmt.Sprintf("Ready, listening on %s", *bind))
|
||||
if err := http.ListenAndServe(*bind, nil); err != nil {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Reference in a new issue