Skip to content

Commit

Permalink
ProxyProtocol, validator v10 (#74)
Browse files Browse the repository at this point in the history
* Support proxyprotocol in tonic.ListenAndServe.
Remove obsolete swagger generation package.
Update validator to v10

* tonic Listen: use address of anon listener struct
  • Loading branch information
loopfz authored Mar 5, 2021
1 parent df6ee68 commit 00ab55e
Show file tree
Hide file tree
Showing 21 changed files with 267 additions and 1,390 deletions.
10 changes: 7 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ module github.com/loopfz/gadgeto
require (
github.com/gin-gonic/gin v1.6.3
github.com/go-gorp/gorp v2.2.0+incompatible
github.com/go-playground/validator/v10 v10.2.0
github.com/go-sql-driver/mysql v1.5.0 // indirect
github.com/golang/protobuf v1.3.5 // indirect
github.com/google/uuid v1.1.1
github.com/juju/errors v0.0.0-20200330140219-3fe23663418f
github.com/juju/testing v0.0.0-20210302031854-2c7ee8570c07 // indirect
github.com/lib/pq v1.9.0 // indirect
github.com/mattn/go-sqlite3 v2.0.3+incompatible
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd // indirect
gopkg.in/go-playground/validator.v8 v8.18.2 // indirect
gopkg.in/go-playground/validator.v9 v9.31.0
github.com/pires/go-proxyproto v0.5.0
github.com/poy/onpar v1.1.2 // indirect
github.com/ziutek/mymysql v1.5.4 // indirect
)

go 1.13
253 changes: 225 additions & 28 deletions go.sum

Large diffs are not rendered by default.

20 changes: 1 addition & 19 deletions tonic/README
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,4 @@ Example of the same application as before, using juju errors:
}


You can also easily serve auto-generated (using tonic data) swagger documentation:

import (
"fmt"

"github.com/gin-gonic/gin"
"github.com/juju/errors"
"github.com/loopfz/gadgeto/tonic"
"github.com/loopfz/gadgeto/tonic/utils/jujerr"
"github.com/loopfz/gadgeto/tonic/utils/swag"
)

func main() {
tonic.SetErrorHook(jujerr.ErrHook)
r := gin.Default()
r.GET("/hello/:name", tonic.Handler(GreetUser, 200))
r.GET("/swagger.json", swag.Swagger(r, "MyAPI", swag.Version("v1.0"), swag.BasePath("/foo/bar")))
r.Run(":8080")
}
You can also easily serve auto-generated swagger documentation (using tonic data) with https://github.com/wi2l/fizz
2 changes: 1 addition & 1 deletion tonic/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"sync"

"github.com/gin-gonic/gin"
validator "github.com/go-playground/validator/v10"
"github.com/google/uuid"
validator "gopkg.in/go-playground/validator.v9"
)

var (
Expand Down
27 changes: 21 additions & 6 deletions tonic/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tonic

import (
"context"
"net"
"net/http"
"os"
"os/signal"
Expand All @@ -20,9 +21,12 @@ var defaultOpts = []ListenOptFunc{

func ListenAndServe(handler http.Handler, errorHandler func(error), opt ...ListenOptFunc) {

listener := struct {
net.Listener
}{}
srv := &http.Server{Handler: handler}

listenOpt := &ListenOpt{Server: srv}
listenOpt := &ListenOpt{Listener: &listener, Server: srv}

for _, o := range defaultOpts {
err := o(listenOpt)
Expand All @@ -48,11 +52,19 @@ func ListenAndServe(handler http.Handler, errorHandler func(error), opt ...Liste

go func() {
var err error
if srv.TLSConfig != nil && len(srv.TLSConfig.Certificates) > 0 {
// ListenAndServeTLS without cert files lets listenOpts set srv.TLSConfig.Certificates
err = srv.ListenAndServeTLS("", "")
} else {
err = srv.ListenAndServe()
var ln net.Listener

ln, err = net.Listen("tcp", listenOpt.Server.Addr)
if err == nil {
// delayed listen, store it in the original listener object so any wrapping listener from listenOpt
// will have a correct reference
listener.Listener = ln
if srv.TLSConfig != nil && len(srv.TLSConfig.Certificates) > 0 {
// ServeTLS without cert files lets listenOpts set srv.TLSConfig.Certificates
err = listenOpt.Server.ServeTLS(listenOpt.Listener, "", "")
} else {
err = listenOpt.Server.Serve(listenOpt.Listener)
}
}
if err != nil && err != http.ErrServerClosed && errorHandler != nil {
errorHandler(err)
Expand Down Expand Up @@ -82,7 +94,10 @@ func ListenAndServe(handler http.Handler, errorHandler func(error), opt ...Liste
}
}

// ListenOpt exposes the Server object so you may change its configuration
// e.g. TLSConfig, and a Listener so that you may wrap it e.g. proxyprotocol
type ListenOpt struct {
Listener net.Listener
Server *http.Server
Signals []os.Signal
ShutdownTimeout time.Duration
Expand Down
2 changes: 1 addition & 1 deletion tonic/tonic.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
validator "gopkg.in/go-playground/validator.v9"
validator "github.com/go-playground/validator/v10"
)

// DefaultMaxBodyBytes is the maximum allowed size of a request body in bytes.
Expand Down
57 changes: 0 additions & 57 deletions tonic/utils/bootstrap/bootstrap.go

This file was deleted.

29 changes: 0 additions & 29 deletions tonic/utils/eis/eis.go

This file was deleted.

11 changes: 11 additions & 0 deletions tonic/utils/listenproxyproto/listener.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package listenproxyproto

import (
"github.com/loopfz/gadgeto/tonic"
"github.com/pires/go-proxyproto"
)

func ListenProxyProtocol(o *tonic.ListenOpt) error {
o.Listener = &proxyproto.Listener{Listener: o.Listener}
return nil
}
144 changes: 0 additions & 144 deletions tonic/utils/swag/doc/doc.go

This file was deleted.

Loading

0 comments on commit 00ab55e

Please sign in to comment.