/* companion.go contains the definitions of api endpoints mainly used by the companion containers for getting a list of all challenges, getting a list of all running containers and starting and stopping challenge containers. */ package main 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 // with the given uuid // // .../api/companion/getRunningContainers?uuid=ASDASD // // returns a list of all running containers within the uuid context func getRunningContainers(w http.ResponseWriter, r *http.Request) { } // startContainer starts a challenge container // // .../api/companion/startContainer?uuid=ASDASD&challenge=DEFDEFDEF // // ASDASDASD is the uuid of the companion container in whose network the // challenge shoud be inserted into // // DEFDEFDEF is the uuid of the challenge func startContainer(w http.ResponseWriter, r *http.Request) { } // stopContainer stops one of the challenge containers // // .../api/companion/stopContainer?uuid=ASDASD&challenge=DEFDEFDEF // // ASDASDASD is the uuid of the companion container in whose network is the // challenge is located // // DEFDEFDEF is the uuid of the challenge func stopContainer(w http.ResponseWriter, r *http.Request) { }