Skip to content

Commit

Permalink
Replace Gin with http.ServeMux
Browse files Browse the repository at this point in the history
  • Loading branch information
djui committed Oct 28, 2019
1 parent fe4ab3e commit 827dad1
Show file tree
Hide file tree
Showing 516 changed files with 33 additions and 306,942 deletions.
3 changes: 1 addition & 2 deletions go.mod
Expand Up @@ -2,7 +2,6 @@ module github.com/voi-go/svc

require (
github.com/caarlos0/env v3.5.0+incompatible
github.com/gin-gonic/gin v1.4.0
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.16.0 // indirect
github.com/google/go-cmp v0.3.1 // indirect
Expand All @@ -13,8 +12,8 @@ require (
go.uber.org/atomic v1.4.0 // indirect
go.uber.org/multierr v1.2.0 // indirect
go.uber.org/zap v1.11.0
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
gopkg.in/go-playground/validator.v9 v9.30.0
)

Expand Down
14 changes: 0 additions & 14 deletions go.sum
Expand Up @@ -13,10 +13,6 @@ github.com/cespare/xxhash/v2 v2.1.0/go.mod h1:dgIUBU3pDso/gPgZ1osOZ0iQf77oPR28Tj
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3 h1:t8FVkw33L+wilf2QiWkw0UV77qRpcH/JHPKGpKa2E8g=
github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
github.com/gin-gonic/gin v1.4.0 h1:3tMoCCfM7ppqsR0ptz/wi1impNpT7/9wQtMZ8lr1mCQ=
github.com/gin-gonic/gin v1.4.0/go.mod h1:OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/3rZdM=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
Expand Down Expand Up @@ -48,8 +44,6 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/mattn/go-isatty v0.0.7 h1:UvyT9uN+3r7yLEYSlJsbQGdsaB/a0DlgWP3pql6iwOc=
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down Expand Up @@ -87,8 +81,6 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/ugorji/go v1.1.4 h1:j4s+tAvLfL3bZyefP2SEWmhBzmuIlH/eqNuPdFPgngw=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/multierr v1.2.0 h1:6I+W7f5VwC5SV9dNrZ3qXrDB9mD0dyGOi/ZJmYw03T4=
Expand All @@ -98,16 +90,12 @@ go.uber.org/zap v1.11.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/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 h1:Ao/3l156eZf2AW5wK8a7/smtodRU+gha3+BeqJ69lRk=
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191010194322-b09406accb47 h1:/XfQ9z7ib8eEJX2hdgFTZJ/ntt0swNk5oYBziWeTCvY=
golang.org/x/sys v0.0.0-20191010194322-b09406accb47/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -120,8 +108,6 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
gopkg.in/go-playground/validator.v8 v8.18.2 h1:lFB4DoMU6B626w8ny76MV7VX6W2VHct2GVOI3xgiMrQ=
gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y=
gopkg.in/go-playground/validator.v9 v9.30.0 h1:Wk0Z37oBmKj9/n+tPyBHZmeL19LaCoK3Qq48VwYENss=
gopkg.in/go-playground/validator.v9 v9.30.0/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
71 changes: 28 additions & 43 deletions options.go
@@ -1,12 +1,12 @@
package svc

import (
"encoding/json"
"fmt"
"net/http"
"net/http/pprof"
"time"

"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
Expand All @@ -26,29 +26,9 @@ func WithTerminationGracePeriod(d time.Duration) Option {
}
}

// WithProductionRouter is an option that uses Gin as HTTP router in release
// mode.
func WithProductionRouter() Option {
return func(s *SVC) error {
// Has to be done before `gin.New()` and overrides `GIN_MODE` env!
gin.SetMode(gin.ReleaseMode)
return WithRouter(gin.New())(s)
}
}

// WithDevelopmentRouter is an option that uses Gin as HTTP router in debug
// mode.
func WithDevelopmentRouter() Option {
return func(s *SVC) error {
// Has to be done before `gin.New()` and overrides `GIN_MODE` env!
gin.SetMode(gin.DebugMode)
return WithRouter(gin.New())(s)
}
}

// WithRouter is an option that replaces the HTTP router with the given Gin
// WithRouter is an option that replaces the HTTP router with the given http
// router.
func WithRouter(router *gin.Engine) Option {
func WithRouter(router *http.ServeMux) Option {
return func(s *SVC) error {
s.Router = router
return nil
Expand Down Expand Up @@ -95,8 +75,7 @@ func assignLogger(s *SVC, logger *zap.Logger, atom zap.AtomicLevel) error {
// log level.
func WithLogLevelHandlers() Option {
return func(s *SVC) error {
s.Router.GET("/loglevel", gin.WrapH(s.atom))
s.Router.PUT("/loglevel", gin.WrapH(s.atom))
s.Router.Handle("/loglevel", s.atom)

return nil
}
Expand Down Expand Up @@ -137,7 +116,7 @@ func WithMetrics() Option {
//Prometheus scraper.
func WithMetricsHandler() Option {
return func(s *SVC) error {
s.Router.GET("/metrics", gin.WrapH(promhttp.Handler()))
s.Router.Handle("/metrics", promhttp.Handler())

return nil
}
Expand All @@ -148,18 +127,18 @@ func WithMetricsHandler() Option {
func WithPProfHandlers() Option {
return func(s *SVC) error {
// See https://github.com/golang/go/blob/master/src/net/http/pprof/pprof.go#L72-L77
s.Router.GET("/debug/pprof/", gin.WrapF(pprof.Index))
s.Router.GET("/debug/pprof/cmdline", gin.WrapF(pprof.Cmdline))
s.Router.GET("/debug/pprof/profile", gin.WrapF(pprof.Profile))
s.Router.GET("/debug/pprof/symbol", gin.WrapF(pprof.Symbol))
s.Router.GET("/debug/pprof/trace", gin.WrapF(pprof.Trace))
s.Router.HandleFunc("/debug/pprof/", pprof.Index)
s.Router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
s.Router.HandleFunc("/debug/pprof/profile", pprof.Profile)
s.Router.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
s.Router.HandleFunc("/debug/pprof/trace", pprof.Trace)
// See https://github.com/golang/go/blob/master/src/net/http/pprof/pprof.go#L248-L258
s.Router.GET("/debug/pprof/allocs", gin.WrapH(pprof.Handler("allocs")))
s.Router.GET("/debug/pprof/block", gin.WrapH(pprof.Handler("block")))
s.Router.GET("/debug/pprof/goroutine", gin.WrapH(pprof.Handler("goroutine")))
s.Router.GET("/debug/pprof/heap", gin.WrapH(pprof.Handler("heap")))
s.Router.GET("/debug/pprof/mutex", gin.WrapH(pprof.Handler("mutex")))
s.Router.GET("/debug/pprof/threadcreate", gin.WrapH(pprof.Handler("threadcreate")))
s.Router.Handle("/debug/pprof/allocs", pprof.Handler("allocs"))
s.Router.Handle("/debug/pprof/block", pprof.Handler("block"))
s.Router.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine"))
s.Router.Handle("/debug/pprof/heap", pprof.Handler("heap"))
s.Router.Handle("/debug/pprof/mutex", pprof.Handler("mutex"))
s.Router.Handle("/debug/pprof/threadcreate", pprof.Handler("threadcreate"))

return nil
}
Expand All @@ -170,12 +149,13 @@ func WithPProfHandlers() Option {
func WithHealthz() Option {
return func(s *SVC) error {
// Register live probe handler
s.Router.GET("/live", func(ctx *gin.Context) {
ctx.JSON(http.StatusOK, gin.H{"status": "Still Alive!"})
s.Router.HandleFunc("/live", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"status": "Still Alive!"}`))
})

// Register ready probe handler
s.Router.GET("/ready", func(c *gin.Context) {
s.Router.HandleFunc("/ready", func(w http.ResponseWriter, r *http.Request) {
var errs []error
for n, w := range s.workers {
if hw, ok := w.(Healther); ok {
Expand All @@ -186,10 +166,15 @@ func WithHealthz() Option {
}
if len(errs) > 0 {
s.logger.Warn("Ready check failed", zap.Any("errs", errs))
c.JSON(http.StatusServiceUnavailable, gin.H{"errors": errs})
return
b, err := json.Marshal(map[string]interface{}{"errors": errs})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusServiceUnavailable)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(b)
}
c.Status(http.StatusOK)
})

return nil
Expand Down
9 changes: 4 additions & 5 deletions svc.go
Expand Up @@ -3,13 +3,13 @@ package svc
import (
"fmt"
"log"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"

"github.com/gin-gonic/gin"
"go.uber.org/zap"
)

Expand All @@ -21,7 +21,7 @@ type SVC struct {
Name string
Version string

Router *gin.Engine
Router *http.ServeMux

TerminationGracePeriod time.Duration
signals chan os.Signal
Expand All @@ -43,6 +43,8 @@ func New(name, version string, opts ...Option) (*SVC, error) {
Name: name,
Version: version,

Router: http.NewServeMux(),

TerminationGracePeriod: defaultTerminationGracePeriod,
signals: make(chan os.Signal, 3),

Expand All @@ -54,9 +56,6 @@ func New(name, version string, opts ...Option) (*SVC, error) {
if err := WithDevelopmentLogger()(s); err != nil {
return nil, err
}
if err := WithProductionRouter()(s); err != nil {
return nil, err
}

// Apply options
for _, o := range opts {
Expand Down
26 changes: 0 additions & 26 deletions vendor/github.com/gin-contrib/sse/.travis.yml

This file was deleted.

21 changes: 0 additions & 21 deletions vendor/github.com/gin-contrib/sse/LICENSE

This file was deleted.

58 changes: 0 additions & 58 deletions vendor/github.com/gin-contrib/sse/README.md

This file was deleted.

0 comments on commit 827dad1

Please sign in to comment.