/
configuration.go
50 lines (41 loc) · 1.73 KB
/
configuration.go
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
package controller
import (
"encoding/json"
"github.com/auto-staging/tower/config"
"github.com/auto-staging/tower/model"
"github.com/auto-staging/tower/types"
"github.com/aws/aws-lambda-go/events"
)
// GetConfigurationController is the controller function for the GET /configuration endpoint.
func GetConfigurationController(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
obj := types.TowerConfiguration{}
err := model.GetConfiguration(&obj)
if err != nil {
return types.InternalServerErrorResponse, nil
}
body, err := json.Marshal(obj)
if err != nil {
config.Logger.Log(err, map[string]string{"module": "controller/GetConfigurationController", "operation": "marshal"}, 0)
return types.InternalServerErrorResponse, nil
}
return events.APIGatewayProxyResponse{Body: string(body), StatusCode: 200}, nil
}
// PutConfigurationController is the controller function for the PUT /configuration endpoint.
// The request body with the update information gets read from the APIGatewayProxyRequest struct.
func PutConfigurationController(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
configuration := types.TowerConfiguration{}
err := json.Unmarshal([]byte(request.Body), &configuration)
if err != nil {
return types.InvalidRequestBodyResponse, nil
}
err = model.UpdateConfiguration(&configuration)
if err != nil {
return types.InternalServerErrorResponse, nil
}
body, err := json.Marshal(configuration)
if err != nil {
config.Logger.Log(err, map[string]string{"module": "controller/PutConfigurationController", "operation": "marshal"}, 0)
return types.InternalServerErrorResponse, nil
}
return events.APIGatewayProxyResponse{Body: string(body), StatusCode: 200}, nil
}