Middleware (#502)

* API endpoint extensions working.

extensions example.

extensions example.

* Added server.NewEnv and some docs for the API extensions example.

extensions example.

extensions example.

* Uncommented special handler stuff.

* First example of middleware.

easier to use.

* Added a special Middleware context to make middleware easier to use.

* Fix tests.

* Cleanup based on PR comments.
This commit is contained in:
Travis Reeder
2017-01-30 14:43:23 -08:00
committed by C Cirello
parent 37efa47bdf
commit ce26f665ea
10 changed files with 213 additions and 48 deletions

View File

@@ -44,6 +44,7 @@ type Server struct {
specialHandlers []SpecialHandler
appListeners []AppListener
middlewares []Middleware
runnerListeners []RunnerListener
mu sync.Mutex // protects hotroutes
@@ -95,7 +96,7 @@ func New(ctx context.Context, ds models.Datastore, mq models.MessageQueue, apiUR
}
s.Router.Use(prepareMiddleware(ctx))
s.bindHandlers()
s.bindHandlers(ctx)
for _, opt := range opts {
opt(s)
@@ -103,6 +104,7 @@ func New(ctx context.Context, ds models.Datastore, mq models.MessageQueue, apiUR
return s
}
// todo: remove this or change name
func prepareMiddleware(ctx context.Context) gin.HandlerFunc {
return func(c *gin.Context) {
ctx, _ := common.LoggerWithFields(ctx, extractFields(c))
@@ -239,7 +241,7 @@ func (s *Server) startGears(ctx context.Context) {
svr.Serve(ctx)
}
func (s *Server) bindHandlers() {
func (s *Server) bindHandlers(ctx context.Context) {
engine := s.Router
engine.GET("/", handlePing)
@@ -247,6 +249,7 @@ func (s *Server) bindHandlers() {
engine.GET("/stats", s.handleStats)
v1 := engine.Group("/v1")
v1.Use(s.middlewareWrapperFunc(ctx))
{
v1.GET("/apps", s.handleAppList)
v1.POST("/apps", s.handleAppCreate)