frontend: able to receive opening hint

This commit is contained in:
nemunaire 2016-12-09 11:55:44 +01:00
parent 4b20c1dc1e
commit f4f55f37ee
2 changed files with 40 additions and 0 deletions

39
frontend/hint.go Normal file
View File

@ -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)
}
}

View File

@ -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))