-
Notifications
You must be signed in to change notification settings - Fork 0
/
front_federate.go
125 lines (109 loc) · 2.74 KB
/
front_federate.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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/aws/aws-lambda-go/events"
"github.com/rodaine/hclencoder"
federate "github.com/strawberryutopia/federate-fronter"
"gopkg.in/yaml.v2"
)
type Federate struct {
Updated bool `json:"updated"`
Fronter string `json:"fronter"`
Error *Error `json:"error,omitempty"`
}
func (f Federate) ToJSON() string {
var buf bytes.Buffer
body, _ := json.Marshal(f)
json.HTMLEscape(&buf, body)
return buf.String()
}
func (f Federate) ToHCL() string {
hcl, _ := hclencoder.Encode(f)
return string(hcl)
}
func (f Federate) ToYAML() string {
yaml, _ := yaml.Marshal(f)
return string(yaml)
}
// FrontFederateHandler handles requests for /front/federate etc.
func FrontFederateHandler(req events.APIGatewayProxyRequest, format string) (Response, error) {
var outputString, outputType, handlerName string
statusCode := 200
var fed Federate
// Need scopes to be able to write to this
_, err := Authorize(req, Scopes{"fronter.federate:write"})
// TODO: This is kind of a mess.
// Rather than having lots of nested IFs, do everythign in another function,
// and return the first error we get
if err != nil {
fed = Federate{
Updated: false,
Error: &Error{
Message: err.Error(),
Type: "AuthError",
},
}
statusCode = 401
} else {
client, err := federate.NewClient()
if err != nil {
fed = Federate{
Updated: false,
Error: &Error{
Message: err.Error(),
Type: "ClientError",
},
}
statusCode = 401
} else {
fronter, err := client.UpdateFromFront()
if err != nil {
fed = Federate{
Updated: false,
Error: &Error{
Message: err.Error(),
Type: "FederateError",
},
}
statusCode = 401
} else {
fed = Federate{
Updated: true,
Fronter: fronter,
}
}
}
}
// TODO: got to the point where this is in every function now, huh.
// We should make an APIResponse interface, return that instead, and have
// the Handler function in main.go deal with all this
switch format {
case "json":
handlerName = "fed.ToJSON()"
outputString = fed.ToJSON()
outputType = "application/json"
case "yaml":
handlerName = "fed.ToYAML()"
outputString = fed.ToYAML()
outputType = "text/plain; charset=UTF-8"
default:
handlerName = "fed.ToHCL()"
outputString = fed.ToHCL()
outputType = "text/plain; charset=UTF-8"
}
fmt.Printf("%v", outputString)
resp := Response{
StatusCode: statusCode,
IsBase64Encoded: false,
Body: outputString,
Headers: map[string]string{
"Access-Control-Allow-Origin": "*",
"Content-Type": outputType,
"X-LMHD-Func-Reply": handlerName,
"X-LMHD-Req-String": RequestToJSON(req),
},
}
return resp, nil
}