8000 Create http and http/middleware pkgs by LucasRoesler · Pull Request #7 · contiamo/go-base · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Jun 12, 2024. It is now read-only.

Create http and http/middleware pkgs #7

Merged
merged 1 commit into from
Aug 15, 2019
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
19 changes: 13 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
module github.com/contiamo/go-base

require (
github.com/bakins/net-http-recover v0.0.0-20141007104922-6cba69d01459
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd // indirect
github.com/golang/protobuf v1.3.2
github.com/google/uuid v1.1.1
github.com/gorilla/websocket v1.4.0
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/lib/pq v1.0.0
github.com/lib/pq v1.1.1
github.com/opentracing/opentracing-go v1.1.0
github.com/pkg/errors v0.8.0 // indirect
github.com/pkg/errors v0.8.1 // indirect
github.com/prometheus/client_golang v1.1.0
github.com/rs/cors v1.7.0
github.com/satori/go.uuid v1.2.0
github.com/sirupsen/logrus v1.2.0
github.com/sirupsen/logrus v1.4.2
github.com/stretchr/testify v1.3.0
github.com/uber-go/atomic v1.4.0 // indirect
github.com/uber/jaeger-client-go v2.16.0+incompatible
github.com/uber/jaeger-lib v2.0.0+incompatible // indirect
github.com/urfave/negroni v1.0.0
go.uber.org/atomic v1.4.0 // indirect
golang.org/x/lint v0.0.0-20190409202823-959b441ac422
golang.org/x/sys v0.0.0-20190801041406-cbf593c0f2f3 // indirect
google.golang.org/genproto v0.0.0-20181202183823-bd91e49a0898 // indirect
google.golang.org/grpc v1.17.0
golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b // indirect
google.golang.org/genproto v0.0.0-20190716160619-c506a9f90610 // indirect
google.golang.org/grpc v1.22.0
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
)
108 changes: 93 additions & 15 deletions go.sum

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions pkg/http/middleware/cors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package middleware

import (
"net/http"

server "github.com/contiamo/go-base/pkg/http"

"github.com/rs/cors"
)

// WithCORS configures CORS on the webserver
func WithCORS(allowedOrigins, allowedMethods, allowedHeaders []string, allowCredentials bool) server.Option {
return &corsOption{allowedOrigins, allowedMethods, allowedHeaders, allowCredentials}
}

// WithCORSWideOpen allows requests from all origins with all methods and all headers/cookies/credentials allowed.
func WithCORSWideOpen() server.Option {
return &corsOption{
allowedOrigins: []string{"*"},
allowedMethods: []string{"HEAD", "GET", "POST", "PUT", "PATCH", "DELETE"},
allowedHeaders: []string{"*"},
allowCredentials: true,
}
}

type corsOption struct {
allowedOrigins []string
allowedMethods []string
allowedHeaders []string
allowCredentials bool
}

func (opt *corsOption) WrapHandler(handler http.Handler) http.Handler {
c := cors.New(cors.Options{
AllowedOrigins: opt.allowedOrigins,
AllowedMethods: opt.allowedMethods,
AllowedHeaders: opt.allowedHeaders,
AllowCredentials: opt.allowCredentials,
})
return c.Handler(handler)
}
46 changes: 46 additions & 0 deletions pkg/http/middleware/cors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package middleware

import (
"net/http"
"net/http/httptest"
"testing"

server "github.com/contiamo/go-base/pkg/http"
"github.com/stretchr/testify/require"
)

func Test_CORSMiddleware(t *testing.T) {
t.Run("should be possible to configure custom CORS rules", func(t *testing.T) {
allowedOrigins := []string{"foo.bar"}
allowedMethods := []string{"HEAD"}
allowedHeaders := []string{"Content-Type"}
allowCredentials := true
srv, err := createServer([]server.Option{WithCORS(allowedOrigins, allowedMethods, allowedHeaders, allowCredentials)})
require.NoError(t, err)

ts := httptest.NewServer(srv.Handler)
defer ts.Close()

req, _ := http.NewRequest(http.MethodOptions, ts.URL+"/cors", nil)
req.Header.Set("Access-Control-Request-Method", "HEAD")
req.Header.Set("Origin", "foo.bar")

resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)

require.Equal(t, "true", resp.Header.Get("Access-Control-Allow-Credentials"))
require.Equal(t, "foo.bar", resp.Header.Get("Access-Control-Allow-Origin"))
require.Equal(t, "HEAD", resp.Header.Get("Access-Control-Allow-Methods"))
})

t.Run("should support websockets", func(t *testing.T) {
srv, err := createServer([]server.Option{WithCORSWideOpen()})
require.NoError(t, err)

ts := httptest.NewServer(srv.Handler)
defer ts.Close()

err = testWebsocketEcho(ts.URL)
require.NoError(t, err)
})
}
20 changes: 20 additions & 0 deletions pkg/http/middleware/hostname.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package middleware

import (
"fmt"
"os"
)

var hostname = ""

func getHostname() string {
if len(hostname) == 0 {
var err error
hostname, err = os.Hostname()
if err != nil {
_ = fmt.Errorf("unable to retrieve hostname - setting to unknown")
hostname = "unknown"
}
}
return hostname
}
45 changes: 45 additions & 0 deletions 45 pkg/http/middleware/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package middleware

import (
"net/http"
"time"

"github.com/sirupsen/logrus"
"github.com/urfave/negroni"

server "github.com/contiamo/go-base/pkg/http"
)

// WithLogging configures a logrus middleware for that server
func WithLogging(app string) server.Option {
return &loggingOption{app}
}

type loggingOption struct{ app string }

func (opt *loggingOption) WrapHandler(handler http.Handler) http.Handler {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
handler.ServeHTTP(w, r)
resp := w.(negroni.ResponseWriter)
duration := time.Since(start)
status := resp.Status()
if status == 0 {
status = 200
}
logger := logrus.WithFields(logrus.Fields{
"app": opt.app,
"duration_millis": duration.Nanoseconds() / 1000000,
"status_code": status,
"path": r.URL.EscapedPath(),
})
if status >= 200 && status < 400 {
logger.Info("successfully handled request")
} else {
logger.Warn("problem while handling request")
}
})
n := negroni.New()
n.UseHandler(h)
return n
}
49 changes: 49 additions & 0 deletions pkg/http/middleware/logging_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package middleware

import (
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/stretchr/testify/require"

server "github.com/contiamo/go-base/pkg/http"
utils "github.com/contiamo/go-base/pkg/testing"
)

func Test_LoggingMiddleware(t *testing.T) {

t.Run("should be possible to configure logging", func(t *testing.T) {
buf, restore := utils.SetupLoggingBuffer()
defer restore()

srv, err := createServer([]server.Option{WithLogging("test")})
require.NoError(t, err)

ts := httptest.NewServer(srv.Handler)
defer ts.Close()

_, err = http.Get(ts.URL + "/logging")
require.NoError(t, err)

require.Contains(t, buf.String(), "successfully handled request" 9E81 ;)
})

t.Run("should support websockets", func(t *testing.T) {
buf, restore := utils.SetupLoggingBuffer()
defer restore()

srv, err := createServer([]server.Option{WithLogging("test")})
require.NoError(t, err)

ts := httptest.NewServer(srv.Handler)
defer ts.Close()

err = testWebsocketEcho(ts.URL)
require.NoError(t, err)

time.Sleep(100 * time.Millisecond)
require.Contains(t, buf.String(), "successfully handled request")
})
}
113 changes: 113 additions & 0 deletions pkg/http/middleware/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package middleware

import (
"net/http"
"strconv"
"strings"
"time"

uuid "github.com/google/uuid"
"github.com/urfave/negroni"

server "github.com/contiamo/go-base/pkg/http"
"github.com/prometheus/client_golang/prometheus"
)

var (
durationMsBuckets = []float64{10, 50, 100, 200, 300, 500, 1000, 2000, 3000, 5000, 10000, 15000, 20000, 30000}
sizeBytesBuckets = []float64{1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304}
)

// WithMetrics configures metrics collection
func WithMetrics(app string, opNameFunc func(r *http.Request) string) server.Option {
if opNameFunc == nil {
opNameFunc = PathWithCleanID
}

constLabels := prometheus.Labels{"service": app, "instance": getHostname()}

requestDuration := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "http",
Subsystem: "request",
Name: "duration_ms",
Help: "The duration of a request in milliseconds by status, method, and path.",
ConstLabels: constLabels,
Buckets: durationMsBuckets,
},
[]string{"code", "method", "path"},
)
requestCounter := prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "http",
Subsystem: "request",
Name: "total",
Help: "Count of the requests by status, method, and path.",
ConstLabels: constLabels,
},
[]string{"code", "method", "path"},
)
responseSize := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "http",
Subsystem: "response",
Name: "size_bytes",
Help: "The size of the response in bytes by status, method, and path.",
ConstLabels: constLabels,
Buckets: sizeBytesBuckets,
},
[]string{"code", "method", "path"},
)
prometheus.Unregister(requestDuration)
prometheus.Unregister(requestCounter)
prometheus.Unregister(responseSize)

prometheus.MustRegister(requestDuration, requestCounter, responseSize)

return &metricsOption{app, opNameFunc, requestDuration, requestCounter, responseSize}
}

type metricsOption struct {
app string
opNameFunc func(r *http.Request) string
requestDuration *prometheus.HistogramVec
requestCounter *prometheus.CounterVec
responseSize *prometheus.HistogramVec
}

func (opt *metricsOption) WrapHandler(handler http.Handler) http.Handler {

mw := http.HandlerFunc(func(writer http.ResponseWriter, r *http.Request) {
instrumentedWriter := negroni.NewResponseWriter(writer)

defer func(begun time.Time) {
l := prometheus.Labels{
"code": strconv.Itoa(instrumentedWriter.Status()),
"method": strings.ToLower(r.Method),
"path": opt.opNameFunc(r),
}

opt.requestCounter.With(l).Inc()
opt.requestDuration.With(l).Observe(float64(time.Since(begun).Seconds() * 1000))
opt.responseSize.With(l).Observe(float64(instrumentedWriter.Size()))
}(time.Now())

handler.ServeHTTP(instrumentedWriter, r)
})

return mw
}

// PathWithCleanID replace string values that look like ids (uuids and int) with "*"
func PathWithCleanID(r *http.Request) string {
pathParts := strings.Split(r.URL.Path, "/")
for i, part := range pathParts {
if _, err := uuid.Parse(part); err == nil {
pathParts[i] = "*"
continue
}
if _, err := strconv.Atoi(part); err == nil {
pathParts[i] = "*"
}

}
return strings.Join(pathParts, "/")
}
Loading
0