| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package dockerfunc |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "fmt" |
| 8 | |
| 9 | "github.com/netdata/netdata/go/plugins/pkg/funcapi" |
| 10 | ) |
| 11 | |
| 12 | // router routes method calls to appropriate function handlers. |
| 13 | type router struct { |
| 14 | deps Deps |
| 15 | |
| 16 | handlers map[string]funcapi.MethodHandler |
| 17 | } |
| 18 | |
| 19 | func newRouter(deps Deps) *router { |
| 20 | r := &router{ |
| 21 | deps: deps, |
| 22 | handlers: make(map[string]funcapi.MethodHandler), |
| 23 | } |
| 24 | r.handlers[containersMethodID] = newFuncContainers(r) |
| 25 | return r |
| 26 | } |
| 27 | |
| 28 | // Compile-time interface check. |
| 29 | var _ funcapi.MethodHandler = (*router)(nil) |
| 30 | |
| 31 | func (r *router) MethodParams(ctx context.Context, method string) ([]funcapi.ParamConfig, error) { |
| 32 | if h, ok := r.handlers[method]; ok { |
| 33 | return h.MethodParams(ctx, method) |
| 34 | } |
| 35 | return nil, fmt.Errorf("unknown method: %s", method) |
| 36 | } |
| 37 | |
| 38 | func (r *router) Handle(ctx context.Context, method string, params funcapi.ResolvedParams) *funcapi.FunctionResponse { |
| 39 | if h, ok := r.handlers[method]; ok { |
| 40 | return h.Handle(ctx, method, params) |
| 41 | } |
| 42 | return funcapi.NotFoundResponse(method) |
| 43 | } |
| 44 | |
| 45 | func (r *router) Cleanup(ctx context.Context) { |
| 46 | for _, h := range r.handlers { |
| 47 | h.Cleanup(ctx) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func Methods() []funcapi.MethodConfig { |
| 52 | return []funcapi.MethodConfig{ |
| 53 | containersMethodConfig(), |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | func NewRouter(deps Deps) funcapi.MethodHandler { |
| 58 | return newRouter(deps) |
| 59 | } |