-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
65 lines (52 loc) · 1.43 KB
/
Copy pathmain.go
File metadata and controls
65 lines (52 loc) · 1.43 KB
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
package main
import (
"encoding/json"
"net/http"
"strings"
"github.com/zegl/kube-score/config"
"github.com/zegl/kube-score/domain"
"github.com/zegl/kube-score/parser"
"github.com/zegl/kube-score/score"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
type inputReader struct {
*strings.Reader
}
func (inputReader) Name() string {
return "input"
}
func handleRequest(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
fail := func(err error) events.APIGatewayProxyResponse {
return events.APIGatewayProxyResponse{StatusCode: http.StatusBadRequest, Body: err.Error()}
}
reader := &inputReader{
Reader: strings.NewReader(req.Body),
}
cnf := config.Configuration{
AllFiles: []domain.NamedReader{reader},
}
allObjs, err := parser.ParseFiles(cnf)
if err != nil {
return fail(err), nil
}
card, err := score.Score(allObjs, cnf)
cardJson, err := json.Marshal(card)
if err != nil {
return fail(err), nil
}
res := events.APIGatewayProxyResponse{
StatusCode: http.StatusOK,
Headers: map[string]string{
"Content-Type": "application/json; charset=utf-8",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "OPTIONS,POST",
"Access-Control-Allow-Headers": "Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token",
},
Body: string(cardJson),
}
return res, nil
}
func main() {
lambda.Start(handleRequest)
}