This repository has been archived by the owner on Mar 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from slok/gorestful
Add go-restful compatible middleware
- Loading branch information
Showing
7 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |