From f4f55f37ee3690383e11da0f5ac2bdde8f14e195 Mon Sep 17 00:00:00 2001 From: nemunaire Date: Fri, 9 Dec 2016 11:55:44 +0100 Subject: [PATCH] frontend: able to receive opening hint --- frontend/hint.go | 39 +++++++++++++++++++++++++++++++++++++++ frontend/main.go | 1 + 2 files changed, 40 insertions(+) create mode 100644 frontend/hint.go diff --git a/frontend/hint.go b/frontend/hint.go new file mode 100644 index 00000000..de138647 --- /dev/null +++ b/frontend/hint.go @@ -0,0 +1,39 @@ +package main + +import ( + "log" + "net/http" + "strings" +) + +type HintHandler struct {} + +func (h HintHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + log.Printf("Handling %s opening hint 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) != 1 && len(sURL) != 2 { + http.Error(w, "{\"errmsg\":\"Requête invalide.\"}", http.StatusBadRequest) + return + } + + team := sURL[0] + + // Enqueue file for backend treatment + if saveTeamFile(SubmissionDir, team, "hint", w, r) { + http.Error(w, "{\"errmsg\":\"Demande d'astuce acceptée...\"}", http.StatusAccepted) + } +} diff --git a/frontend/main.go b/frontend/main.go index 306549a9..6ff766a2 100644 --- a/frontend/main.go +++ b/frontend/main.go @@ -79,6 +79,7 @@ func main() { if !*denyChName { http.Handle(fmt.Sprintf("%s/chname/", *prefix), http.StripPrefix(fmt.Sprintf("%s/chname/", *prefix), ChNameHandler{})) } + http.Handle(fmt.Sprintf("%s/openhint/", *prefix), http.StripPrefix(fmt.Sprintf("%s/openhint/", *prefix), HintHandler{})) http.Handle(fmt.Sprintf("%s/submission/", *prefix), http.StripPrefix(fmt.Sprintf("%s/submission/", *prefix), SubmissionHandler{end})) log.Println(fmt.Sprintf("Ready, listening on %s", *bind))