Skip to content
This repository was archived by the owner on Jan 12, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.idea/
handler.so
handler.zip
.env
36 changes: 0 additions & 36 deletions Makefile

This file was deleted.

62 changes: 0 additions & 62 deletions Makefile.shim

This file was deleted.

16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,17 @@ strategic Microservices. Serving 1M+ customers with up to 500CCU.

## Usage

Clone locally into your own Serverless project:
Install locally into your own Serverless project. For a generic event driven service, use:

serverless install \
-u https://github.com/yunspace/serverless-golang \
-n my-golang-project
-u https://github.com/yunspace/serverless-golang/aws/event \
-n my-golang-event-project

For a `go/net` driven project, use:

serverless install \
-u https://github.com/yunspace/serverless-golang/net/event \
-n my-golang-net-project

Prior to running, you should have `go`, `make` and `docker` installed. Then once
in your project directory run this to get all the build dependencies:
Expand All @@ -65,9 +71,9 @@ used for authentication on AWS.

Then deploy the function by running this command:

make
make clean dockerDist deploy

To uninstall and clean up everything you just need to run:

make clean delete
make clean remove

3 changes: 3 additions & 0 deletions aws/event/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.serverless
handler.so
handler.zip
60 changes: 60 additions & 0 deletions aws/event/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
IMAGE_LAMBDA_GO=eawsy/aws-lambda-go-shim:latest

# serverless.yml env vars
ENV ?= dev
AWS_DEFAULT_REGION ?= ap-southeast-2
AWS_PROFILE ?= default

HANDLER ?= handler
PACKAGE ?= $(HANDLER)
GOPATH ?= $(HOME)/go

all: clean dist deploy
.PHONY: all

# lambda shim targets
clean:
@rm -rf $(HANDLER) $(HANDLER).so $(PACKAGE).zip
.PHONY: clean

deps:
@go get -u -d github.com/eawsy/aws-lambda-go-core/...
@docker pull $(IMAGE_LAMBDA_GO)
.PHONY: deps

dist: build pack

build:
@go build -buildmode=plugin -ldflags='-w -s' -o $(HANDLER).so

pack:
@pack $(HANDLER) $(HANDLER).so $(PACKAGE).zip
@chown $(shell stat -c '%u:%g' .) $(HANDLER).so $(PACKAGE).zip

# serverless targets
deploy:
AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION} ENV=${ENV} sls deploy
.PHONY: deploy

remove:
AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION} ENV=${ENV} sls remove -v
.PHONY: delete

# dockerised target
dockerDist:
$(call docker, make dist)
.PHONY: dockerAll

dockerShell:
$(call docker, bash)
.PHONY: dockerShell

define docker
docker run --rm -ti \
-e HANDLER=$(HANDLER) \
-e PACKAGE=$(PACKAGE) \
-v $(GOPATH):/go \
-v $(CURDIR):/tmp \
-w /tmp \
$(IMAGE_LAMBDA_GO) $1
endef
19 changes: 19 additions & 0 deletions aws/event/event_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"github.com/eawsy/aws-lambda-go-core/service/lambda/runtime"
"github.com/eawsy/aws-lambda-go-event/service/lambda/runtime/event/apigatewayproxyevt"
"log"
)

func handlAPIEvent(evt *apigatewayproxyevt.Event, ctx *runtime.Context) (*Response, error) {
log.Println(evt)
response := &Response{
StatusCode: 200,
Headers: make(map[string]string),
}
response.SetBody(&APIBody{"Hello, serverless-golang!", evt.Body})
response.Headers["X-Powered-By"] = "serverless-golang"

return response, nil
}
28 changes: 28 additions & 0 deletions aws/event/event_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"testing"
"github.com/eawsy/aws-lambda-go-event/service/lambda/runtime/event/apigatewayproxyevt"
"github.com/eawsy/aws-lambda-go-core/service/lambda/runtime"
"github.com/stretchr/testify/assert"
)

func TestCustomHeader(t *testing.T) {
// when
r, err := handlAPIEvent(&apigatewayproxyevt.Event{}, &runtime.Context{})

// then
assert.NoError(t, err)
assert.Equal(t, "serverless-golang", r.Headers["X-Powered-By"])
}

func TestAPIBody(t *testing.T) {
// when
r, err := handlAPIEvent(&apigatewayproxyevt.Event{Body: "GET"}, &runtime.Context{})

// then
assert.NoError(t, err)
assert.IsType(t, &Response{}, r)
//response, _ := r.(*Response)
assert.Equal(t, "{\"message\":\"Hello, serverless-golang!\",\"input\":\"GET\"}", r.Body)
}
10 changes: 10 additions & 0 deletions aws/event/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import (
"github.com/eawsy/aws-lambda-go-event/service/lambda/runtime/event/apigatewayproxyevt"
"github.com/eawsy/aws-lambda-go-core/service/lambda/runtime"
)

func EventHandle(evt *apigatewayproxyevt.Event, ctx *runtime.Context) (interface{}, error) {
return handlAPIEvent(evt, ctx)
}
21 changes: 21 additions & 0 deletions aws/event/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import "encoding/json"

// special format need by API Gateway response body
type APIBody struct {
Message string `json:"message"`
Input string `json:"input"`
}

type Response struct {
StatusCode int `json:"statusCode"`
Headers map[string]string `json:"headers"`
Body string `json:"body"`
}

// inspired by serverless-java
func (r *Response) SetBody(b *APIBody) {
bytes, _ := json.Marshal(b)
r.Body = string(bytes)
}
14 changes: 14 additions & 0 deletions aws/event/serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
service: serverless-golang-event
package:
artifact: handler.zip
provider:
name: aws
runtime: python2.7
stage: ${env:ENV}
region: ${env:AWS_DEFAULT_REGION}
profile: ${env:AWS_PROFILE}
functions:
hello-event:
handler: handler.EventHandle
events:
- http: GET hello-event
3 changes: 3 additions & 0 deletions aws/net/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.serverless
handler.so
handler.zip
60 changes: 60 additions & 0 deletions aws/net/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
IMAGE_LAMBDA_GO=eawsy/aws-lambda-go-shim:latest

# serverless.yml env vars
ENV ?= dev
AWS_DEFAULT_REGION ?= ap-southeast-2
AWS_PROFILE ?= default

HANDLER ?= handler
PACKAGE ?= $(HANDLER)
GOPATH ?= $(HOME)/go

all: clean dist deploy
.PHONY: all

# lambda shim targets
clean:
@rm -rf $(HANDLER) $(HANDLER).so $(PACKAGE).zip
.PHONY: clean

deps:
@go get -u -d github.com/eawsy/aws-lambda-go-core/...
@docker pull $(IMAGE_LAMBDA_GO)
.PHONY: deps

dist: build pack

build:
@go build -buildmode=plugin -ldflags='-w -s' -o $(HANDLER).so

pack:
@pack $(HANDLER) $(HANDLER).so $(PACKAGE).zip
@chown $(shell stat -c '%u:%g' .) $(HANDLER).so $(PACKAGE).zip

# serverless targets
deploy:
AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION} ENV=${ENV} sls deploy
.PHONY: deploy

remove:
AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION} ENV=${ENV} sls remove -v
.PHONY: delete

# dockerised target
dockerDist:
$(call docker, make dist)
.PHONY: dockerAll

dockerShell:
$(call docker, bash)
.PHONY: dockerShell

define docker
docker run --rm -ti \
-e HANDLER=$(HANDLER) \
-e PACKAGE=$(PACKAGE) \
-v $(GOPATH):/go \
-v $(CURDIR):/tmp \
-w /tmp \
$(IMAGE_LAMBDA_GO) $1
endef
12 changes: 12 additions & 0 deletions aws/net/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"github.com/eawsy/aws-lambda-go-net/service/lambda/runtime/net/apigatewayproxy"
)

// Handle is the exported handler called by AWS Lambda.
var NetHandle apigatewayproxy.Handler

func init() {
NetHandle = NewNetHandler()
}
Loading