Skip to content
This repository has been archived by the owner on Mar 19, 2019. It is now read-only.

Commit

Permalink
Merge pull request #7 from slok/gorestful
Browse files Browse the repository at this point in the history
Add go-restful compatible middleware
  • Loading branch information
slok authored Oct 11, 2018
2 parents e0d2ae7 + 4f106d5 commit 2afc2a1
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.x.x / 2018-xx-xx

* [FEATURE] Add gorestful compatible middleware.

## 0.3.0 / 2018-09-18

* [FEATURE] Add httprouter compatible middleware.
Expand Down
2 changes: 2 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ The middleware is mainly focused to be compatible with Go std library using http
- [Negroni][negroni-example]
- [Gin][gin-example]
- [httprouter][httprouter-example]
- [go-restful][gorestful-example]

## Benchmarks

Expand All @@ -145,3 +146,4 @@ BenchmarkMiddlewareHandler/benchmark_with_predefined_handler_ID-4 1000000
[negroni-example]: examples/negroni
[gin-example]: examples/gin
[httprouter-example]: examples/httprouter
[gorestful-example]: examples/gorestful
60 changes: 60 additions & 0 deletions examples/gorestful/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import (
"log"
"net/http"
"os"
"os/signal"
"syscall"

gorestful "github.com/emicklei/go-restful"
"github.com/prometheus/client_golang/prometheus/promhttp"
prommiddleware "github.com/slok/go-prometheus-middleware"
promgorestful "github.com/slok/go-prometheus-middleware/gorestful"
)

const (
srvAddr = ":8080"
metricsAddr = ":8081"
)

func main() {
// Create our middleware.
mdlw := prommiddleware.NewDefault()

// Create our gorestful instance.
c := gorestful.NewContainer()

// Add the middleware for all routes.
c.Filter(promgorestful.Handler("", mdlw))

// Add our handler.
ws := &gorestful.WebService{}
ws.Produces(gorestful.MIME_JSON)

ws.Route(ws.GET("/").To(func(_ *gorestful.Request, resp *gorestful.Response) {
resp.WriteEntity("Hello world")
}))
c.Add(ws)

// Serve our handler.
go func() {
log.Printf("server listening at %s", srvAddr)
if err := http.ListenAndServe(":8080", c); err != nil {
log.Panicf("error while serving: %s", err)
}
}()

// Serve our metrics.
go func() {
log.Printf("metrics listening at %s", metricsAddr)
if err := http.ListenAndServe(metricsAddr, promhttp.Handler()); err != nil {
log.Panicf("error while serving metrics: %s", err)
}
}()

// Wait until some signal is captured.
sigC := make(chan os.Signal, 1)
signal.Notify(sigC, syscall.SIGTERM, syscall.SIGINT)
<-sigC
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/slok/go-prometheus-middleware
require (
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful v2.8.0+incompatible
github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7 // indirect
github.com/gin-gonic/gin v1.3.0
github.com/gogo/protobuf v1.1.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 h1:xJ4a3vCFaGF/jqvzLM
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
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/emicklei/go-restful v2.8.0+incompatible h1:wN8GCRDPGHguIynsnBartv5GUgGUg1LAU7+xnSn1j7Q=
github.com/emicklei/go-restful v2.8.0+incompatible/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7 h1:AzN37oI0cOS+cougNAV9szl6CVoj2RYwzS3DpUQNtlY=
github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
github.com/gin-gonic/gin v1.3.0 h1:kCmZyPklC0gVdL728E6Aj20uYBJV93nj/TkwBTKhFbs=
Expand Down
41 changes: 41 additions & 0 deletions gorestful/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package gorestful_test

import (
"log"
"net/http"

gorestful "github.com/emicklei/go-restful"
"github.com/prometheus/client_golang/prometheus/promhttp"
prommiddleware "github.com/slok/go-prometheus-middleware"
promgorestful "github.com/slok/go-prometheus-middleware/gorestful"
)

// GorestfulMiddleware shows how you would create a default middleware factory and use it
// to create a Gorestful compatible middleware.
func Example_gorestfulMiddleware() {
// Create our middleware factory with the default settings.
mdlw := prommiddleware.NewDefault()

// Create our gorestful instance.
c := gorestful.NewContainer()

// Add the middleware for all routes.
c.Filter(promgorestful.Handler("", mdlw))

// Add our handler,
ws := &gorestful.WebService{}
ws.Route(ws.GET("/").To(func(_ *gorestful.Request, resp *gorestful.Response) {
resp.WriteEntity("Hello world")
}))
c.Add(ws)

// Serve metrics from the default prometheus registry.
log.Printf("serving metrics at: %s", ":8081")
go http.ListenAndServe(":8081", promhttp.Handler())

// Serve our handler.
log.Printf("listening at: %s", ":8080")
if err := http.ListenAndServe(":8080", c); err != nil {
log.Panicf("error while serving: %s", err)
}
}
28 changes: 28 additions & 0 deletions gorestful/gorestful.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Package gorestful is a helper package to get a gorestful compatible
// handler/middleware from the standard net/http Middleware factory
// (from github.com/slok/go-prometheus-middleware).
package gorestful

import (
"net/http"

gorestful "github.com/emicklei/go-restful"

prommiddleware "github.com/slok/go-prometheus-middleware"
)

// Handler returns a gorestful compatible middleware from a Middleware factory instance.
// The first handlerID argument is the same argument passed on Middleware.Handler method.
func Handler(handlerID string, m prommiddleware.Middleware) gorestful.FilterFunction {
// Create a dummy handler to wrap the middleware chain of gorestful, this way Middleware
// interface can wrap the gorestful chain.
return func(req *gorestful.Request, resp *gorestful.Response, chain *gorestful.FilterChain) {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
req.Request = r
resp.ResponseWriter = w
chain.ProcessFilter(req, resp)
})

m.Handler(handlerID, h).ServeHTTP(resp.ResponseWriter, req.Request)
}
}

0 comments on commit 2afc2a1

Please sign in to comment.