Skip to content

Commit

Permalink
Slight tweaks (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
NickyMateev authored and georgifarashev committed Sep 19, 2018
1 parent f9027b3 commit 5d0ad17
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
4 changes: 2 additions & 2 deletions docs/plugins.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Filters and Plugins

The main extension points of the service manager are filters, plugins and controllers. The
interfaces that need to be implement in order to provide an extension point can be found
interfaces that need to be implemented in order to provide an extension point can be found
in the `pkg/web` package.

## Filters

Filters provide means to intercept any HTTP request to the Service Manager. It allows adding
custom logic before the request reaches the actual handler (HTTP Endpoint logic) and also
before it returns the response. Filters can either propagate a request to the next filter
in the chain or stop the request and write their own response
in the chain or stop the request and write their own response.

Service Manager HTTP endpoints are described in the [API Specification](https://github.com/Peripli/specification/blob/master/api.md).

Expand Down
26 changes: 12 additions & 14 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/Peripli/service-manager/pkg/log"
"github.com/Peripli/service-manager/pkg/web"
"github.com/gorilla/mux"
)
)

// Settings type to be loaded from the environment
type Settings struct {
Expand Down Expand Up @@ -77,7 +77,6 @@ type Server struct {
*mux.Router

Config *Settings
api *web.API
}

// New creates a new server with the provided REST api configuration and server configuration
Expand All @@ -87,9 +86,18 @@ func New(config *Settings, api *web.API) *Server {
registerControllers(api, router)

return &Server{
Config: config,
Router: router,
api: api,
Config: config,
}
}

func registerControllers(API *web.API, router *mux.Router) {
for _, ctrl := range API.Controllers {
for _, route := range ctrl.Routes() {
log.D().Debugf("Registering endpoint: %s %s", route.Endpoint.Method, route.Endpoint.Path)
handler := web.Filters(API.Filters).ChainMatching(route)
router.Handle(route.Endpoint.Path, api.NewHTTPHandler(handler)).Methods(route.Endpoint.Method)
}
}
}

Expand All @@ -107,16 +115,6 @@ func (s *Server) Run(ctx context.Context) {
startServer(ctx, handler, s.Config.ShutdownTimeout)
}

func registerControllers(API *web.API, router *mux.Router) {
for _, ctrl := range API.Controllers {
for _, route := range ctrl.Routes() {
log.D().Debugf("Registering endpoint: %s %s", route.Endpoint.Method, route.Endpoint.Path)
handler := web.Filters(API.Filters).ChainMatching(route)
router.Handle(route.Endpoint.Path, api.NewHTTPHandler(handler)).Methods(route.Endpoint.Method)
}
}
}

func startServer(ctx context.Context, server *http.Server, shutdownTimeout time.Duration) {
go gracefulShutdown(ctx, server, shutdownTimeout)

Expand Down
2 changes: 1 addition & 1 deletion pkg/web/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (fs Filters) Chain(h Handler) Handler {
return wrappedFilters[0]
}

// Matching returns a subset of Filters that match the specified route
// Matching returns a subset of Filters that match the specified endpoint
func (fs Filters) Matching(endpoint Endpoint) Filters {
matchedFilters := make([]Filter, 0)
matchedNames := make([]string, 0)
Expand Down

0 comments on commit 5d0ad17

Please sign in to comment.