about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEmile <hanemile@protonmail.com>2019-11-04 19:19:18 +0100
committerEmile <hanemile@protonmail.com>2019-11-04 19:19:18 +0100
commiteb1d57d0803cb88c99b8a63a3db7ac8ce3704783 (patch)
treebe85790867774b4792dcbf111cda7f500ba949a3
parent7ae400319c0dfeb09e67e1f4de5d8bfe0dd33299 (diff)
moved the getChallenges function
-rw-r--r--src/companion.go33
-rw-r--r--src/http.go33
2 files changed, 32 insertions, 34 deletions
diff --git a/src/companion.go b/src/companion.go
index 521df80..8852dfa 100644
--- a/src/companion.go
+++ b/src/companion.go
@@ -6,13 +6,44 @@ running containers and starting and stopping challenge containers.
 
 package main
 
-import "net/http"
+import (
+	"encoding/json"
+	"fmt"
+	"log"
+	"net/http"
+)
 
 // getChallenges returns some json containing a list of all challenges
 //
 // .../api/companion/getChallenges
 func getChallenges(w http.ResponseWriter, r *http.Request) {
+	challenges := dbGetAllChallenges()
 
+	var strippedChallenges []StrippedChallenge
+	categories := map[string]int{}
+
+	// build the strippedChallenges list
+	for _, challenge := range challenges {
+		strippedChallenges = append(strippedChallenges, stripChallenge(challenge))
+
+		categories[challenge.Category]++
+	}
+
+	// marshal the challenges to json
+	marshalled, marshalError := json.Marshal(map[string]interface{}{
+		"challenges": strippedChallenges,
+		"categories": categories,
+	})
+	if marshalError != nil {
+		log.Println(marshalError)
+		return
+	}
+
+	// set the json header and write the marshaled challenges to the response
+	// writer
+	w.Header().Set("Content-Type", "application/json")
+	w.Header().Set("Access-Control-Allow-Origin", "*")
+	fmt.Fprintf(w, string(marshalled))
 }
 
 // getRunningContainers returns a list of all running containers in the network
diff --git a/src/http.go b/src/http.go
index 0eadd57..16361c6 100644
--- a/src/http.go
+++ b/src/http.go
@@ -2,7 +2,6 @@ package main
 
 import (
 	"crypto/sha256"
-	"encoding/json"
 	"flag"
 	"fmt"
 	"html/template"
@@ -257,38 +256,6 @@ func readFileToResponse(w http.ResponseWriter, path string) {
 	}
 }
 
-// getChallenges returns all challenges
-func getChallenges(w http.ResponseWriter, r *http.Request) {
-
-	challenges := dbGetAllChallenges()
-
-	var strippedChallenges []StrippedChallenge
-	categories := map[string]int{}
-
-	// build the strippedChallenges list
-	for _, challenge := range challenges {
-		strippedChallenges = append(strippedChallenges, stripChallenge(challenge))
-
-		categories[challenge.Category]++
-	}
-
-	// marshal the challenges to json
-	marshalled, marshalError := json.Marshal(map[string]interface{}{
-		"challenges": strippedChallenges,
-		"categories": categories,
-	})
-	if marshalError != nil {
-		log.Println(marshalError)
-		return
-	}
-
-	// set the json header and write the marshaled challenges to the response
-	// writer
-	w.Header().Set("Content-Type", "application/json")
-	w.Header().Set("Access-Control-Allow-Origin", "*")
-	fmt.Fprintf(w, string(marshalled))
-}
-
 func stripChallenge(challenge Challenge) StrippedChallenge {
 	// Hash the flag using sha256
 	FlagSha256Sum := fmt.Sprintf("%x", sha256.Sum256([]byte(challenge.Flag)))