diff options
Diffstat (limited to 'src/companion.go')
-rw-r--r-- | src/companion.go | 33 |
1 files changed, 32 insertions, 1 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 |