|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/aws/aws-lambda-go/events" |
| 8 | + "github.com/aws/aws-lambda-go/lambda" |
| 9 | + "moul.io/guilhunize/guilhunize" |
| 10 | +) |
| 11 | + |
| 12 | +func handler(request events.APIGatewayProxyRequest) (*events.APIGatewayProxyResponse, error) { |
| 13 | + switch { |
| 14 | + case request.HTTPMethod == "GET" && request.Path == "/api/quote": |
| 15 | + return &events.APIGatewayProxyResponse{ |
| 16 | + StatusCode: 200, |
| 17 | + Body: guilhunize.Quote(), |
| 18 | + Headers: map[string]string{ |
| 19 | + "Content-Type": "text/plain; charset=utf-8", |
| 20 | + }, |
| 21 | + }, nil |
| 22 | + case request.HTTPMethod == "POST" && request.Path == "/api/guilhunize": |
| 23 | + if request.Body == "" { |
| 24 | + return nil, fmt.Errorf("missing body; you can use something like:\n\necho refactor | http POST https://guilhunize.moul.io/api/guilhunize --body") |
| 25 | + } |
| 26 | + return &events.APIGatewayProxyResponse{ |
| 27 | + StatusCode: 200, |
| 28 | + Body: guilhunize.Guilhunize(request.Body), |
| 29 | + Headers: map[string]string{ |
| 30 | + "Content-Type": "text/plain; charset=utf-8", |
| 31 | + }, |
| 32 | + }, nil |
| 33 | + default: |
| 34 | + out, _ := json.MarshalIndent(request, "", " ") |
| 35 | + return &events.APIGatewayProxyResponse{ |
| 36 | + StatusCode: 500, |
| 37 | + Body: fmt.Sprintf("error: unknown method/path.\n\n%s", string(out)), |
| 38 | + Headers: map[string]string{ |
| 39 | + "Content-Type": "text/plain; charset=utf-8", |
| 40 | + }, |
| 41 | + }, nil |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func main() { |
| 46 | + lambda.Start(handler) |
| 47 | +} |
0 commit comments