Skip to content

Commit 021820a

Browse files
authored
Merge pull request #8 from moul/dev/moul/lambda
feat: lambda API
2 parents 3f9f42d + fd15fca commit 021820a

File tree

9 files changed

+151
-1
lines changed

9 files changed

+151
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*#
44
.#*
55
coverage.txt
6+
/lambda-build/
67

78
# Vendors
89
package-lock.json

Makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,22 @@ GOPKG ?= moul.io/guilhunize
22
DOCKER_IMAGE ?= moul/guilhunize
33
GOBINS ?= .
44
NPM_PACKAGES ?= .
5+
LAMBDA_PORT ?= 8080
56

67
all: test install
78

89
-include rules.mk
10+
11+
.PHONY: netlify
12+
netlify: lambda-build
13+
14+
.PHONY: lambda-build
15+
lambda-build:
16+
rm -rf lambda-build
17+
cd lambda && go get
18+
cd lambda && GOOS=linux GOARCH=amd64 $(GO) build -o ../lambda-build/guilhunize guilhunize.go
19+
20+
.PHONY: lambda-dev
21+
lambda-dev: lambda-build
22+
@echo "Open http://localhost:$(LAMBDA_PORT)/guilhunize"
23+
sam local start-api --host=0.0.0.0 --port=$(LAMBDA_PORT) --static-dir=web

lambda/go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module moul.io/guilhunize/lambda
2+
3+
go 1.13
4+
5+
require (
6+
github.com/aws/aws-lambda-go v1.13.2
7+
moul.io/guilhunize v1.0.0
8+
)
9+
10+
replace moul.io/guilhunize => ../

lambda/go.sum

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
2+
github.com/aws/aws-lambda-go v1.13.2 h1:8lYuRVn6rESoUNZXdbCmtGB4bBk4vcVYojiHjE4mMrM=
3+
github.com/aws/aws-lambda-go v1.13.2/go.mod h1:4UKl9IzQMoD+QF79YdCuzCwp8VbmG4VAQwij/eHl5CU=
4+
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
5+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
6+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
8+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
9+
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
10+
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
11+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
12+
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
13+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
14+
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
15+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
16+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
17+
gopkg.in/urfave/cli.v2 v2.0.0-20190806201727-b62605953717 h1:OvXt/p4cdwNl+mwcWMq/AxaKFkhdxcjx+tx+qf4EOvY=
18+
gopkg.in/urfave/cli.v2 v2.0.0-20190806201727-b62605953717/go.mod h1:cKXr3E0k4aosgycml1b5z33BVV6hai1Kh7uDgFOkbcs=
19+
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
20+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

lambda/guilhunize.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
}

netlify.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[build]
2+
command = "make netlify"
3+
functions = "./lambda-build"
4+
publish = "web"
5+
6+
[build.environment]
7+
GO_IMPORT_PATH = "moul.io/guilhunize"
8+
NPM_FLAGS = "nop" # disable npm install to avoid "missing go" error
9+
10+
[[redirects]]
11+
from = "/"
12+
to = "/api/quote"
13+
14+
[[redirects]]
15+
from = "/api/*"
16+
to = "/.netlify/functions/guilhunize?handler=:splat"
17+
status = 200

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@
1717
"url": "https://github.com/moul/guilhunize.git"
1818
},
1919
"bugs": "https://github.com/moul/guilhunize/issues",
20-
"homepage": "https://moul.io/guilhunize"
20+
"homepage": "https://moul.io/guilhunize",
21+
"dependencies": {
22+
"cool": "0.0.1"
23+
}
2124
}

template.yaml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
4+
Globals:
5+
Function:
6+
Timeout: 5
7+
8+
Resources:
9+
GuilhunizeFunction:
10+
Type: AWS::Serverless::Function
11+
Properties:
12+
Handler: lambda-build/guilhunize
13+
Runtime: go1.x
14+
Tracing: Active
15+
Events:
16+
Request:
17+
Type: Api
18+
Properties:
19+
Path: /api/guilhunize
20+
Method: POST
21+
QuoteFunction:
22+
Type: AWS::Serverless::Function
23+
Properties:
24+
Handler: lambda-build/guilhunize
25+
Runtime: go1.x
26+
Tracing: Active
27+
Events:
28+
Request:
29+
Type: Api
30+
Properties:
31+
Path: /api/quote
32+
Method: GET
33+
34+
Outputs:
35+
GraphmanAPI:
36+
GuilhunizeFunction:
37+
Value: !GetAtt GuilhunizeFunction.Arn

web/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)