feat: auto TLS with Let's Encrypt

This commit is contained in:
Daniel G. Taylor 2020-03-15 15:01:40 -07:00
parent c4ce0f03db
commit 3ddd49ccbb
No known key found for this signature in database
GPG key ID: 7BD6DC99C9A87E22
4 changed files with 23 additions and 2 deletions

View file

@ -7,11 +7,14 @@ A modern, simple, fast & opinionated REST API framework for Go with batteries in
- A modern REST API backend framework for Go developers
- Described by [OpenAPI 3](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md) & [JSON Schema](https://json-schema.org/)
- First class support for middleware, JSON, and other features
- Guard rails to prevent common mistakes
- Documentation that can't get out of date
- High-quality developer tooling
Features include:
- HTTP, HTTPS (TLS), and [HTTP/2](https://http2.github.io/) built-in
- Let's Encrypt auto-updating certificates via `--autotls`
- Declarative interface on top of [Gin](https://github.com/gin-gonic/gin)
- Operation & model documentation
- Request params (path, query, or header)
@ -23,10 +26,12 @@ Features include:
- Automatically handle CORS headers
- Structured logging middleware using [Zap](https://github.com/uber-go/zap)
- Annotated Go types for input and output models
- Automatic input model validation
- Generates JSON Schema from Go types
- Automatic input model validation & error handling
- Dependency injection for loggers, datastores, etc
- Documentation generation using [Redoc](https://github.com/Redocly/redoc)
- CLI built-in, configured via arguments or environment variables
- Set via e.g. `-p 8000`, `--port=8000`, or `SERVICE_PORT=8000`
- Generates OpenAPI JSON for access to a rich ecosystem of tools
- Mocks with [API Sprout](https://github.com/danielgtaylor/apisprout)
- SDKs with [OpenAPI Generator](https://github.com/OpenAPITools/openapi-generator)

12
cli.go
View file

@ -9,6 +9,7 @@ import (
"path/filepath"
"strings"
"github.com/gin-gonic/autotls"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap/zapcore"
@ -59,7 +60,15 @@ func (r *Router) setupCLI() {
}
// Start either an HTTP or HTTPS server based on whether TLS cert/key
// paths were given.
// paths were given or Let's Encrypt is used.
autoTLS := viper.GetString("autotls")
if autoTLS != "" {
domains := strings.Split(autoTLS, ",")
if err := autotls.Run(r, domains...); err != nil {
panic(err)
}
}
cert := viper.GetString("cert")
key := viper.GetString("key")
if cert == "" && key == "" {
@ -103,5 +112,6 @@ func (r *Router) setupCLI() {
r.AddGlobalFlag("port", "p", "Port", 8888)
r.AddGlobalFlag("cert", "", "SSL certificate file path", "")
r.AddGlobalFlag("key", "", "SSL key file path", "")
r.AddGlobalFlag("autotls", "", "Let's Encrypt automatic TLS domains (ignores port)", "")
r.AddGlobalFlag("debug", "d", "Enable debug logs", false)
}

1
go.mod
View file

@ -5,6 +5,7 @@ go 1.13
require (
github.com/Jeffail/gabs v1.4.0
github.com/gin-contrib/cors v1.3.1
github.com/gin-gonic/autotls v0.0.0-20200314141124-cc69476aef2a
github.com/gin-gonic/gin v1.5.0
github.com/gosimple/slug v1.9.0
github.com/mattn/go-isatty v0.0.9

5
go.sum
View file

@ -29,6 +29,8 @@ github.com/gin-contrib/cors v1.3.1 h1:doAsuITavI4IOcd0Y19U4B+O0dNWihRyX//nn4sEmg
github.com/gin-contrib/cors v1.3.1/go.mod h1:jjEJ4268OPZUcU7k9Pm653S7lXUGcqMADzFA61xsmDk=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/autotls v0.0.0-20200314141124-cc69476aef2a h1:xdM4UWm9hrdOPTMTTXRNDAsln3LdXr0Ry1XRFUpbpjc=
github.com/gin-gonic/autotls v0.0.0-20200314141124-cc69476aef2a/go.mod h1:GTnUDNd5zRw/BceSPHICHoH9fNaTSPHsFTU72wuK0YE=
github.com/gin-gonic/gin v1.5.0 h1:fi+bqFAx/oLK54somfCtEZs9HeH1LHVoEPUgARpTqyc=
github.com/gin-gonic/gin v1.5.0/go.mod h1:Nd6IXA8m5kNZdNEHMBd93KT+mdY3+bewLgRvmCsR2Do=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
@ -86,8 +88,10 @@ github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQz
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
@ -157,6 +161,7 @@ go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/
go.uber.org/zap v1.10.0 h1:ORx85nbTijNz8ljznvCMR1ZBIPKFn3jQrag10X2AsuM=
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=