about summary refs log tree commit diff
path: root/src/companion.go
blob: df6affbc8634eb72d45252b40c36a90786845f70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
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"
)

func newCompanion(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "%s", "newCompanion")
}

// getChallenges returns some json containing a list of all challenges
//
// .../api/companion/getChallenges
func getChallenges(w http.ResponseWriter, r *http.Request) {
	// get a list of all challenges from the database
	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) {
	fmt.Fprintf(w, "%s", "getRunningContainers")
}

// 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) {
	fmt.Fprintf(w, "%s", "startContainer")
}

// 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) {
	fmt.Fprintf(w, "%s", "stopContainer")
}