Skip to content

Commit 1af6d79

Browse files
authored
Merge pull request #1 from eexit/aws-lambda
AWS Lambda support via Serverless
2 parents 18f679c + ceac6bc commit 1af6d79

13 files changed

Lines changed: 499 additions & 27 deletions

File tree

‎.dockerignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.env*
22
.git*
3+
http2smtp

‎.github/workflows/main.yml‎

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ jobs:
99
name: Unit tests + coverage
1010
runs-on: ubuntu-latest
1111
steps:
12+
- uses: actions/checkout@v2
1213
- name: Set up Go 1.x
1314
uses: actions/setup-go@v2
1415
with:
1516
go-version: ^1.15
16-
- name: Check out code into the Go module directory
17-
uses: actions/checkout@v2
1817
- name: Get dependencies
1918
run: go get -v -t -d ./...
2019
- name: Build
@@ -34,19 +33,19 @@ jobs:
3433
runs-on: ubuntu-latest
3534
steps:
3635
- uses: actions/checkout@v2
37-
- name: golangci-lint
36+
- name: Lint the codebase
3837
uses: golangci/golangci-lint-action@v2
3938
with:
4039
version: v1.34
4140
publish:
42-
if: contains(github.ref, 'refs/tags/')
4341
name: Docker publish
44-
runs-on: ubuntu-latest
42+
if: contains(github.ref, 'refs/tags/')
4543
needs: [ tests, lint ]
44+
runs-on: ubuntu-latest
4645
steps:
4746
- uses: actions/checkout@v2
48-
- name: Set up Docker Buildx
49-
uses: docker/setup-buildx-action@v1
47+
- uses: docker/setup-qemu-action@v1
48+
- uses: docker/setup-buildx-action@v1
5049
- name: Login to DockerHub
5150
uses: docker/login-action@v1
5251
with:
@@ -58,6 +57,7 @@ jobs:
5857
- name: Build and push
5958
uses: docker/build-push-action@v2
6059
with:
60+
platforms: linux/amd64
6161
build-args: version=${{ steps.tagref.outputs.tag }}+${{ github.sha }}
6262
tags: '${{ github.repository }}:${{ steps.tagref.outputs.tag }},${{ github.repository }}:latest'
6363
push: true
@@ -67,3 +67,29 @@ jobs:
6767
username: ${{ secrets.DOCKERHUB_USERNAME }}
6868
password: ${{ secrets.DOCKERHUB_PASSWORD }}
6969
repository: ${{ github.repository }}
70+
release-bin:
71+
name: Release binaries
72+
if: contains(github.ref, 'refs/tags/')
73+
needs: [ tests, lint ]
74+
runs-on: ubuntu-latest
75+
env:
76+
CGO_ENABLED: 0
77+
strategy:
78+
matrix:
79+
target: [http2smtp, http2smtp-lambda]
80+
steps:
81+
- uses: actions/checkout@v2
82+
- name: Retrieve tag
83+
id: tagref
84+
run: echo ::set-output name=tag::${GITHUB_REF#refs/*/}
85+
- uses: wangyoucao577/go-release-action@v1.11
86+
with:
87+
github_token: ${{ secrets.GH_TOKEN }}
88+
goos: linux
89+
goarch: amd64
90+
goversion: '1.15'
91+
project_path: cmd/${{ matrix.target }}
92+
binary_name: ${{ matrix.target }}
93+
extra_files: LICENSE README.md
94+
release_tag: ${{ steps.tagref.outputs.tag }}
95+
ldflags: -X github.com/eexit/http2smtp/internal/api.Version=${{ steps.tagref.outputs.tag }}+${{ github.sha }}

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
.env
22
coverage.txt
3+
http2smtp
4+
.serverless

‎Dockerfile‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
FROM golang:1-alpine AS builder
2-
RUN apk --update add ca-certificates
1+
FROM golang:1 AS builder
32
RUN update-ca-certificates
43
ARG version
4+
ARG goos=linux
5+
ARG goarch=amd64
56
LABEL version=${version}
67
WORKDIR /go/src/github.com/eexit/http2smtp
78
COPY . .
89
# Inject the build version: https://blog.alexellis.io/inject-build-time-vars-golang/
9-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
10+
RUN CGO_ENABLED=0 GOOS=${goos} GOARCH=${goarch} go build \
1011
-ldflags "-X github.com/eexit/http2smtp/internal/api.Version=${version}" \
1112
-o /http2smtp \
1213
./cmd/http2smtp

‎README.md‎

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,41 @@
11
# 📩 HTTP to SMTP
2-
3-
[![version](https://img.shields.io/github/v/tag/eexit/http2smtp?label=version&logo=github&sort=semver)](https://github.com/eexit/http2smtp/releases) [![docker pull](https://img.shields.io/docker/pulls/eexit/http2smtp)](https://hub.docker.com/repository/docker/eexit/http2smtp) [![ci](https://github.com/eexit/http2smtp/workflows/build/badge.svg)](https://github.com/eexit/http2smtp/actions) [![codecov](https://codecov.io/gh/eexit/http2smtp/branch/master/graph/badge.svg?token=XH18EYLDLZ)](https://codecov.io/gh/eexit/http2smtp) [![license](https://img.shields.io/github/license/eexit/http2smtp)](https://github.com/eexit/http2smtp/blob/master/LICENSE)
2+
[![ci](https://github.com/eexit/http2smtp/workflows/build/badge.svg)](https://github.com/eexit/http2smtp/actions) [![codecov](https://codecov.io/gh/eexit/http2smtp/branch/master/graph/badge.svg?token=XH18EYLDLZ)](https://codecov.io/gh/eexit/http2smtp) [![version](https://img.shields.io/github/v/tag/eexit/http2smtp?label=version&logo=github&sort=semver)](https://github.com/eexit/http2smtp/releases) [![license](https://img.shields.io/github/license/eexit/http2smtp)](https://github.com/eexit/http2smtp/blob/master/LICENSE)
43

54
An API that forwards HTTP-backed vendor mailer calls to SMTP.
65

76
Plug a [MailHog](https://github.com/mailhog/MailHog) or [MailCatcher](https://mailcatcher.me/) to API email sending vendors such as [SparkPost](https://www.sparkpost.com/), [Mailgun](https://www.mailgun.com/) or [SendGrid](https://sendgrid.com/) for testing purposes.
87

8+
#### Features
9+
10+
- :white_check_mark: 100% code coverage
11+
- :whale: Light Docker image available
12+
- :zap: AWS Lambda Function 3-commands deployment
13+
914
## Usage
1015

1116
See [examples](examples).
1217

1318
:zap: ProTip: for tracing purposes, this app kinda supports [W3C Trace Context recommendation](https://www.w3.org/TR/trace-context/). Configure the env var `TRACEPARENT_HEADER` and inject any trace into this header value. All log entries will be contextualized with the given value.
1419

15-
### Docker image
20+
### Docker image [![docker pull](https://img.shields.io/docker/pulls/eexit/http2smtp)](https://hub.docker.com/repository/docker/eexit/http2smtp) [![size](https://img.shields.io/docker/image-size/eexit/http2smtp?sort=semver)](https://hub.docker.com/repository/docker/eexit/http2smtp)
1621

1722
1. Checkout this repo or only copy the `.env.dist` and `docker-compose.yml` files
1823
1. Rename `.env.dist` into `.env`
1924
1. Optional: update the values accordingly
2025
1. Pull images and run `docker-compose up http2smtp`
2126

22-
## Vendor endpoints
23-
24-
### [SparkPost](https://developers.sparkpost.com/api/)
27+
### AWS Lambda Function ![aws-lambda-ready](https://img.shields.io/badge/aws-lambda--ready-orange?logo=amazon-aws&style=flat)
2528

26-
#### Inline transmission
29+
:zap: This project is also shipped for an AWS Lambda Function-ready. Check out the [README](cmd/http2smtp-lambda).
2730

28-
API documentation: https://developers.sparkpost.com/api/transmissions/#transmissions-post-send-inline-content
31+
## Vendors
2932

30-
_:warning: Not supported yet._
31-
32-
#### RFC 822 transmission
33-
34-
API documentation: https://developers.sparkpost.com/api/transmissions/#transmissions-post-send-rfc822-content
33+
### [SparkPost](https://developers.sparkpost.com/api/)
3534

3635
POST /sparkpost/api/v1/transmissions
3736

37+
SparkPost supports either [inline](https://developers.sparkpost.com/api/transmissions/#transmissions-post-send-inline-content) or [RFC 822 transmissions](https://developers.sparkpost.com/api/transmissions/#transmissions-post-send-rfc822-content). For now, only the latter one is supported.
38+
3839
Basic validation is enforced, only the recipients list email and the RFC 822 content are used and mandatory.
3940

4041
## License
@@ -43,4 +44,4 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
4344

4445
## Contributors
4546

46-
![contributors](https://contrib.rocks/image?repo=eexit/http2smtp)
47+
[![contributors](https://contrib.rocks/image?repo=eexit/http2smtp)](https://github.com/eexit/http2smtp/graphs/contributors)

‎cmd/http2smtp-lambda/Makefile‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
URL=https://api.github.com/repos/eexit/http2smtp/releases/latest
2+
3+
.DEFAULT: usage
4+
5+
usage:
6+
@echo '+-------------------------------------------------------------------+'
7+
@echo '| Make Usage |'
8+
@echo '+-------------------------------------------------------------------+'
9+
@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "|- \033[33m%-15s\033[0m -> %s\n", $$1, $$2}'
10+
11+
install-sls: ## Installs serverless (via NPM)
12+
npm install -g serverless --ignore-scripts --loglevel error
13+
14+
config: ## Prints the resolved stack config
15+
sls print --verbose
16+
17+
download: ## Gets the latest version of the binary
18+
curl -sL $(URL) | jq -r '.assets[].browser_download_url' | grep lambda | grep -v md5 | xargs wget -qO- | tar xvz http2smtp-lambda
19+
20+
deploy: ## Deploys the stack
21+
sls deploy --verbose
22+
23+
remove: ## Removes the stack
24+
sls remove --verbose

‎cmd/http2smtp-lambda/README.md‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# :zap: http2smtp for AWS Lambda
2+
3+
Deploy in 3 commands (assuming you already have a SMTP server deployed).
4+
5+
## Usage
6+
7+
```
8+
$ make
9+
+-------------------------------------------------------------------+
10+
| Make Usage |
11+
+-------------------------------------------------------------------+
12+
|- config -> Prints the resolved stack config
13+
|- deploy -> Deploys the stack
14+
|- download -> Gets the latest version of the binary
15+
|- install-sls -> Installs serverless (via NPM)
16+
|- remove -> Removes the stack
17+
```
18+
19+
## Deploying
20+
21+
- Install [serverless](https://www.serverless.com/) [yourself](https://www.serverless.com/framework/docs/getting-started/) or run `make install-sls`
22+
- Edit the `serverless.yml` file with the [wished configuration](https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml/)
23+
- Run `make download && make deploy`
24+
25+
### Deploying a specific version
26+
27+
If you wish to download a specific version of http2smtp, you can prefix the download URL as env var prior calling `make download`:
28+
29+
```bash
30+
$ URL=https://api.github.com/repos/eexit/http2smtp/releases/tags/v1.2.3 make download
31+
```
32+
33+
Then deploy using `make deploy`.
11.7 MB
Binary file not shown.

‎cmd/http2smtp-lambda/main.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+
"math/rand"
5+
"os"
6+
"time"
7+
8+
"github.com/aws/aws-lambda-go/lambda"
9+
"github.com/awslabs/aws-lambda-go-api-proxy/httpadapter"
10+
"github.com/eexit/http2smtp/internal/api"
11+
"github.com/eexit/http2smtp/internal/converter"
12+
"github.com/eexit/http2smtp/internal/env"
13+
"github.com/eexit/http2smtp/internal/smtp"
14+
"github.com/kelseyhightower/envconfig"
15+
"github.com/rs/zerolog"
16+
)
17+
18+
func main() {
19+
rand.Seed(time.Now().UTC().UnixNano())
20+
21+
var e env.Bag
22+
envconfig.MustProcess("", &e)
23+
24+
level, err := zerolog.ParseLevel(e.LogLevel)
25+
if err != nil {
26+
panic(err)
27+
}
28+
29+
logger := zerolog.New(os.Stdout).
30+
Level(level).
31+
With().
32+
Timestamp().
33+
Str("version", api.Version).
34+
Logger()
35+
36+
smtpClient := smtp.New(e.SMTPAddr, logger)
37+
38+
converterProvider := converter.NewProvider(
39+
converter.NewRFC5322(),
40+
converter.NewSparkPost(),
41+
)
42+
43+
app := api.New(e, logger, smtpClient, converterProvider)
44+
adapter := httpadapter.New(app.Wrap(app.Mux()))
45+
46+
lambda.StartHandler(lambda.NewHandler(adapter.ProxyWithContext))
47+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
service: http2smtp-lambda
2+
frameworkVersion: '2'
3+
useDotenv: true
4+
5+
provider:
6+
name: aws
7+
runtime: go1.x
8+
9+
package:
10+
exclude:
11+
- ./**
12+
include:
13+
- http2smtp-lambda
14+
15+
functions:
16+
proxy:
17+
handler: http2smtp-lambda
18+
memorySize: 128
19+
environment:
20+
SMTP_ADDR: 'smtp:25'
21+
LOG_LEVEL: debug
22+
events:
23+
- http:
24+
path: '{proxy+}'
25+
method: any

0 commit comments

Comments
 (0)