From 0d2fe61af698a0e1d1f650d80f296bd88fc3cb6e Mon Sep 17 00:00:00 2001 From: Emile Date: Wed, 9 Oct 2019 18:34:23 +0200 Subject: gofmt and added user to stats struct --- src/http.go | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) (limited to 'src/http.go') diff --git a/src/http.go b/src/http.go index 480e356..839f6ea 100644 --- a/src/http.go +++ b/src/http.go @@ -1,16 +1,17 @@ package main import ( + "encoding/json" "flag" - "github.com/gorilla/mux" - "net/http" "fmt" - "strings" "io/ioutil" - "time" "log" - "encoding/json" + "net/http" "strconv" + "strings" + "time" + + "github.com/gorilla/mux" ) var ( @@ -21,7 +22,7 @@ func registerHTTPFlags() { port = flag.Int("port", 8080, "The port for HTTP") } -func setupHTTPServer() (http.Server) { +func setupHTTPServer() http.Server { r := mux.NewRouter() r.HandleFunc("/", indexHandler) @@ -40,7 +41,7 @@ func setupHTTPServer() (http.Server) { r.HandleFunc("/api/getStats", getStatsHandler).Methods("GET") return http.Server{ - Addr: fmt.Sprintf("0.0.0.0:%d", *port), + Addr: fmt.Sprintf("0.0.0.0:%d", *port), Handler: r, } } @@ -96,6 +97,9 @@ func loginPostHandler(w http.ResponseWriter, r *http.Request) { accessCode := r.Form.Get("accesscode") session, cookieNotFoundError := r.Cookie("session") + // store the name of the user for the scoreboard scraper + user = username + validRedirect := false // we need to verify that the client doesn't already have a valid session @@ -109,9 +113,9 @@ func loginPostHandler(w http.ResponseWriter, r *http.Request) { // credentials valid, create session newSessionToken := createSession() http.SetCookie(w, &http.Cookie{ - Name: "session", - Value: newSessionToken, - Path: "/", + Name: "session", + Value: newSessionToken, + Path: "/", Expires: time.Now().Add(time.Hour * 24), }) validRedirect = true @@ -149,9 +153,9 @@ func logoutHandler(w http.ResponseWriter, r *http.Request) { // and delete the cookie http.SetCookie(w, &http.Cookie{ - Name: "session", - Value: "", - Path: "/", + Name: "session", + Value: "", + Path: "/", Expires: time.Unix(0, 0), }) } @@ -263,7 +267,7 @@ func submitFlagHandler(w http.ResponseWriter, r *http.Request) { // inform our client jsonAnswer, _ := json.Marshal(map[string]string{ "correctFlag": strconv.FormatBool(correctFlag), - "error": errorString, + "error": errorString, }) w.Write([]byte(jsonAnswer)) } @@ -308,7 +312,7 @@ func startContainerHandler(w http.ResponseWriter, r *http.Request) { // inform our client jsonAnswer, _ := json.Marshal(map[string]string{ - "error": errorString, + "error": errorString, "address": addressString, }) w.Write([]byte(jsonAnswer)) @@ -369,7 +373,7 @@ func getTimeLimitHandler(w http.ResponseWriter, r *http.Request) { jsonAnswer, _ := json.Marshal(map[string]string{ "endTimestamp": fmt.Sprintf("%d", *endTimestamp), - "endAfter": fmt.Sprintf("%d", *endAfter), + "endAfter": fmt.Sprintf("%d", *endAfter), }) w.Write([]byte(jsonAnswer)) } -- cgit 1.4.1