From d72870a77d220b7444a6cf3a55c65e6e2aa599a2 Mon Sep 17 00:00:00 2001 From: Peter Grlica Date: Mon, 21 Mar 2022 10:10:27 +0100 Subject: [PATCH] Fixed options parameter, more verbose info output, hit handling on panic --- app/boot_levels.go | 2 +- app/options/apigw.cue | 10 ++++--- pkg/apigw/ctx/ctx.go | 16 +++++----- pkg/apigw/filter/postfilter.go | 12 ++++---- pkg/apigw/filter/postfilter_test.go | 6 ++-- pkg/apigw/filter/prefilter.go | 14 ++++----- pkg/apigw/filter/prefilter_test.go | 14 ++++----- pkg/apigw/filter/processer.go | 8 ++--- pkg/apigw/filter/processer_test.go | 4 +-- pkg/apigw/filter/proxy/proxy.go | 4 +-- pkg/apigw/filter/proxy/proxy_test.go | 2 +- pkg/apigw/helpers.go | 23 ++++++++------- pkg/apigw/registry/registry.go | 4 +-- pkg/apigw/registry/registry_test.go | 8 ++--- pkg/apigw/route.go | 2 +- pkg/apigw/route_test.go | 2 +- pkg/apigw/service.go | 44 +++++++++++++++++++++------- pkg/apigw/service_test.go | 2 +- pkg/apigw/types/handler.go | 2 +- pkg/apigw/types/test.go | 2 +- tests/apigw/main_test.go | 2 +- 21 files changed, 106 insertions(+), 77 deletions(-) diff --git a/app/boot_levels.go b/app/boot_levels.go index 1bbda9d3b..2a52bc845 100644 --- a/app/boot_levels.go +++ b/app/boot_levels.go @@ -491,7 +491,7 @@ func (app *CortezaApp) InitServices(ctx context.Context) (err error) { corredor.Service().SetRoleFinder(sysService.DefaultRole) // Initialize API GW bits - apigw.Setup(options.Apigw(), app.Log, app.Store) + apigw.Setup(*options.Apigw(), app.Log, app.Store) if err = apigw.Service().Reload(ctx); err != nil { return err } diff --git a/app/options/apigw.cue b/app/options/apigw.cue index 39993aa2f..72dffe347 100644 --- a/app/options/apigw.cue +++ b/app/options/apigw.cue @@ -26,12 +26,14 @@ apigw: schema.#optionsGroup & { description: "Enable extra logging" } profiler_enabled: { - type: "bool" - description: "Enable profiler" + type: "bool" + defaultGoExpr: "true" + description: "Enable profiler" } profiler_global: { - type: "bool" - description: "Profiler enabled for all routes" + type: "bool" + defaultGoExpr: "false" + description: "Profiler enabled for all routes" } log_request_body: { type: "bool" diff --git a/pkg/apigw/ctx/ctx.go b/pkg/apigw/ctx/ctx.go index 63fbaa40b..a1853ae45 100644 --- a/pkg/apigw/ctx/ctx.go +++ b/pkg/apigw/ctx/ctx.go @@ -7,17 +7,17 @@ import ( "github.com/cortezaproject/corteza-server/pkg/apigw/types" ) -type ContextKey string - -const ContextKeyScope ContextKey = "scope" -const ContextKeyProfiler ContextKey = "profiler" +type ( + scopeCtxKey struct{} + profilerCtxKey struct{} +) func ScopeToContext(ctx context.Context, s *types.Scp) context.Context { - return context.WithValue(ctx, ContextKeyScope, s) + return context.WithValue(ctx, scopeCtxKey{}, s) } func ScopeFromContext(ctx context.Context) (ss *types.Scp) { - s := ctx.Value(ContextKeyScope) + s := ctx.Value(scopeCtxKey{}) if s == nil { return &types.Scp{} @@ -27,11 +27,11 @@ func ScopeFromContext(ctx context.Context) (ss *types.Scp) { } func ProfilerToContext(ctx context.Context, h interface{}) context.Context { - return context.WithValue(ctx, ContextKeyProfiler, h) + return context.WithValue(ctx, profilerCtxKey{}, h) } func ProfilerFromContext(ctx context.Context) (h *profiler.Hit) { - hh := ctx.Value(ContextKeyProfiler) + hh := ctx.Value(profilerCtxKey{}) if hh == nil { return nil diff --git a/pkg/apigw/filter/postfilter.go b/pkg/apigw/filter/postfilter.go index 044f1e100..ee0153684 100644 --- a/pkg/apigw/filter/postfilter.go +++ b/pkg/apigw/filter/postfilter.go @@ -56,7 +56,7 @@ type ( } ) -func NewRedirection(opts *options.ApigwOpt) (e *redirection) { +func NewRedirection(opts options.ApigwOpt) (e *redirection) { e = &redirection{} e.Name = "redirection" @@ -79,7 +79,7 @@ func NewRedirection(opts *options.ApigwOpt) (e *redirection) { return } -func (h redirection) New(opts *options.ApigwOpt) types.Handler { +func (h redirection) New(opts options.ApigwOpt) types.Handler { return NewRedirection(opts) } @@ -129,7 +129,7 @@ func (h redirection) Handler() types.HandlerFunc { } } -func NewDefaultJsonResponse(opts *options.ApigwOpt) (e *defaultJsonResponse) { +func NewDefaultJsonResponse(opts options.ApigwOpt) (e *defaultJsonResponse) { e = &defaultJsonResponse{} e.Name = "defaultJsonResponse" @@ -139,7 +139,7 @@ func NewDefaultJsonResponse(opts *options.ApigwOpt) (e *defaultJsonResponse) { return } -func (j defaultJsonResponse) New(opts *options.ApigwOpt) types.Handler { +func (j defaultJsonResponse) New(opts options.ApigwOpt) types.Handler { return NewDefaultJsonResponse(opts) } @@ -181,7 +181,7 @@ func checkStatus(typ string, status int) bool { } } -func NewJsonResponse(opts *options.ApigwOpt, reg typesRegistry) (e *jsonResponse) { +func NewJsonResponse(opts options.ApigwOpt, reg typesRegistry) (e *jsonResponse) { e = &jsonResponse{} e.Name = "jsonResponse" @@ -201,7 +201,7 @@ func NewJsonResponse(opts *options.ApigwOpt, reg typesRegistry) (e *jsonResponse return } -func (j jsonResponse) New(opts *options.ApigwOpt) types.Handler { +func (j jsonResponse) New(opts options.ApigwOpt) types.Handler { return NewJsonResponse(opts, j.reg) } diff --git a/pkg/apigw/filter/postfilter_test.go b/pkg/apigw/filter/postfilter_test.go index 2da9958ce..b748f5322 100644 --- a/pkg/apigw/filter/postfilter_test.go +++ b/pkg/apigw/filter/postfilter_test.go @@ -35,7 +35,7 @@ func Test_redirectionMerge(t *testing.T) { ) for _, tc := range tcc { - t.Run(tc.name, testMerge(NewRedirection(&options.ApigwOpt{}), tc)) + t.Run(tc.name, testMerge(NewRedirection(options.ApigwOpt{}), tc)) } } @@ -75,7 +75,7 @@ func Test_redirection(t *testing.T) { rc = httptest.NewRecorder() ) - h := getHandler(NewRedirection(&options.ApigwOpt{})) + h := getHandler(NewRedirection(options.ApigwOpt{})) h, err := h.Merge([]byte(tc.expr)) req.NoError(err) @@ -145,7 +145,7 @@ func Test_jsonResponse(t *testing.T) { r = r.WithContext(agctx.ScopeToContext(context.Background(), scope)) - h := getHandler(NewJsonResponse(&options.ApigwOpt{}, &mockHandlerRegistry{})) + h := getHandler(NewJsonResponse(options.ApigwOpt{}, &mockHandlerRegistry{})) h, err := h.Merge([]byte(tc.expr)) req.NoError(err) diff --git a/pkg/apigw/filter/prefilter.go b/pkg/apigw/filter/prefilter.go index 45bb720a8..9f99b6c4e 100644 --- a/pkg/apigw/filter/prefilter.go +++ b/pkg/apigw/filter/prefilter.go @@ -41,12 +41,12 @@ type ( } profiler struct { - opts *options.ApigwOpt + opts options.ApigwOpt types.FilterMeta } ) -func NewHeader(opts *options.ApigwOpt) (v *header) { +func NewHeader(opts options.ApigwOpt) (v *header) { v = &header{} v.Name = "header" @@ -64,7 +64,7 @@ func NewHeader(opts *options.ApigwOpt) (v *header) { return } -func (h header) New(opts *options.ApigwOpt) types.Handler { +func (h header) New(opts options.ApigwOpt) types.Handler { return NewHeader(opts) } @@ -134,7 +134,7 @@ func (h header) Handler() types.HandlerFunc { } } -func NewQueryParam(opts *options.ApigwOpt) (v *queryParam) { +func NewQueryParam(opts options.ApigwOpt) (v *queryParam) { v = &queryParam{} v.Name = "queryParam" @@ -152,7 +152,7 @@ func NewQueryParam(opts *options.ApigwOpt) (v *queryParam) { return } -func (qp queryParam) New(opts *options.ApigwOpt) types.Handler { +func (qp queryParam) New(opts options.ApigwOpt) types.Handler { return NewQueryParam(opts) } @@ -222,7 +222,7 @@ func (qp *queryParam) Handler() types.HandlerFunc { } } -func NewProfiler(opts *options.ApigwOpt) (pp *profiler) { +func NewProfiler(opts options.ApigwOpt) (pp *profiler) { pp = &profiler{} pp.opts = opts @@ -233,7 +233,7 @@ func NewProfiler(opts *options.ApigwOpt) (pp *profiler) { return } -func (pr profiler) New(opts *options.ApigwOpt) types.Handler { +func (pr profiler) New(opts options.ApigwOpt) types.Handler { return NewProfiler(opts) } diff --git a/pkg/apigw/filter/prefilter_test.go b/pkg/apigw/filter/prefilter_test.go index cab0b439d..14709ffa5 100644 --- a/pkg/apigw/filter/prefilter_test.go +++ b/pkg/apigw/filter/prefilter_test.go @@ -37,7 +37,7 @@ func Test_headerMerge(t *testing.T) { ) for _, tc := range tcc { - t.Run(tc.name, testMerge(NewHeader(&options.ApigwOpt{}), tc)) + t.Run(tc.name, testMerge(NewHeader(options.ApigwOpt{}), tc)) } } @@ -83,7 +83,7 @@ func Test_headerHandle(t *testing.T) { r := httptest.NewRequest(http.MethodGet, "/foo", http.NoBody) r.Header = tc.headers - t.Run(tc.name, testHandle(NewHeader(&options.ApigwOpt{}), r, tc)) + t.Run(tc.name, testHandle(NewHeader(options.ApigwOpt{}), r, tc)) } } @@ -115,7 +115,7 @@ func Test_queryParamMerge(t *testing.T) { ) for _, tc := range tcc { - t.Run(tc.name, testMerge(NewQueryParam(&options.ApigwOpt{}), tc)) + t.Run(tc.name, testMerge(NewQueryParam(options.ApigwOpt{}), tc)) } } @@ -148,7 +148,7 @@ func Test_queryParamHandle(t *testing.T) { for _, tc := range tcc { r := httptest.NewRequest(http.MethodGet, tc.url, http.NoBody) - t.Run(tc.name, testHandle(NewQueryParam(&options.ApigwOpt{}), r, tc)) + t.Run(tc.name, testHandle(NewQueryParam(options.ApigwOpt{}), r, tc)) } } @@ -156,7 +156,7 @@ func Test_profilerHandle_profilerGlobal(t *testing.T) { type ( tfp struct { name string - opts *options.ApigwOpt + opts options.ApigwOpt r *http.Request exp *h.Request } @@ -167,13 +167,13 @@ func Test_profilerHandle_profilerGlobal(t *testing.T) { tcc = []tfp{ { name: "skip profiler hit on profiler global = true", - opts: &options.ApigwOpt{ProfilerGlobal: true}, + opts: options.ApigwOpt{ProfilerGlobal: true}, r: rr, exp: nil, }, { name: "add profiler hit on profiler global = false", - opts: &options.ApigwOpt{ProfilerGlobal: false}, + opts: options.ApigwOpt{ProfilerGlobal: false}, r: rr, exp: createRequest(rr), }, diff --git a/pkg/apigw/filter/processer.go b/pkg/apigw/filter/processer.go index 679f231e8..489d5425b 100644 --- a/pkg/apigw/filter/processer.go +++ b/pkg/apigw/filter/processer.go @@ -48,7 +48,7 @@ type ( } ) -func NewWorkflow(opts *options.ApigwOpt, wf WfExecer) (p *workflow) { +func NewWorkflow(opts options.ApigwOpt, wf WfExecer) (p *workflow) { p = &workflow{} p.d = wf @@ -68,7 +68,7 @@ func NewWorkflow(opts *options.ApigwOpt, wf WfExecer) (p *workflow) { return } -func (h workflow) New(opts *options.ApigwOpt) types.Handler { +func (h workflow) New(opts options.ApigwOpt) types.Handler { return NewWorkflow(opts, h.d) } @@ -152,7 +152,7 @@ func (h workflow) Handler() types.HandlerFunc { } } -func NewPayload(opts *options.ApigwOpt, l *zap.Logger) (p *processerPayload) { +func NewPayload(opts options.ApigwOpt, l *zap.Logger) (p *processerPayload) { p = &processerPayload{} p.vm = jsenv.New(jsenv.NewTransformer(jsenv.LoaderJS, jsenv.TargetES2016)) @@ -176,7 +176,7 @@ func NewPayload(opts *options.ApigwOpt, l *zap.Logger) (p *processerPayload) { return } -func (h processerPayload) New(opts *options.ApigwOpt) types.Handler { +func (h processerPayload) New(opts options.ApigwOpt) types.Handler { return NewPayload(opts, h.log) } diff --git a/pkg/apigw/filter/processer_test.go b/pkg/apigw/filter/processer_test.go index 0b81dae15..12a53387f 100644 --- a/pkg/apigw/filter/processer_test.go +++ b/pkg/apigw/filter/processer_test.go @@ -78,7 +78,7 @@ func Test_processerWorkflow(t *testing.T) { rc = httptest.NewRecorder() rq, _ = http.NewRequest("POST", "/foo", http.NoBody) ar, err = h.NewRequest(rq) - pp = NewWorkflow(&options.ApigwOpt{}, tc.wfs) + pp = NewWorkflow(options.ApigwOpt{}, tc.wfs) ) _, err = pp.Merge([]byte(tc.params)) @@ -173,7 +173,7 @@ func Test_processerPayload(t *testing.T) { ar, err = h.NewRequest(tc.rq) ) - pp := NewPayload(&options.ApigwOpt{}, zap.NewNop()) + pp := NewPayload(options.ApigwOpt{}, zap.NewNop()) _, err = pp.Merge([]byte(tc.params)) if tc.errv != "" { diff --git a/pkg/apigw/filter/proxy/proxy.go b/pkg/apigw/filter/proxy/proxy.go index 3ed55d4db..cb1ca659d 100644 --- a/pkg/apigw/filter/proxy/proxy.go +++ b/pkg/apigw/filter/proxy/proxy.go @@ -46,7 +46,7 @@ type ( } ) -func New(opts *options.ApigwOpt, l *zap.Logger, c *http.Client, s types.SecureStorager) (p *proxy) { +func New(opts options.ApigwOpt, l *zap.Logger, c *http.Client, s types.SecureStorager) (p *proxy) { p = &proxy{} p.c = c @@ -68,7 +68,7 @@ func New(opts *options.ApigwOpt, l *zap.Logger, c *http.Client, s types.SecureSt return } -func (h proxy) New(opts *options.ApigwOpt) types.Handler { +func (h proxy) New(opts options.ApigwOpt) types.Handler { return New(opts, h.log, h.c, h.s) } diff --git a/pkg/apigw/filter/proxy/proxy_test.go b/pkg/apigw/filter/proxy/proxy_test.go index 5c6abbf63..236b16bd9 100644 --- a/pkg/apigw/filter/proxy/proxy_test.go +++ b/pkg/apigw/filter/proxy/proxy_test.go @@ -174,7 +174,7 @@ func Test_proxy(t *testing.T) { rq = httptest.NewRequest("POST", "/foo", strings.NewReader(`custom request body`)) } - proxy := New(&options.ApigwOpt{}, zap.NewNop(), c, struct{}{}) + proxy := New(options.ApigwOpt{}, zap.NewNop(), c, struct{}{}) _, err := proxy.Merge([]byte(tc.params)) req.NoError(err) diff --git a/pkg/apigw/helpers.go b/pkg/apigw/helpers.go index 5a9cba2cf..925e461ea 100644 --- a/pkg/apigw/helpers.go +++ b/pkg/apigw/helpers.go @@ -6,29 +6,31 @@ import ( "github.com/cortezaproject/corteza-server/pkg/apigw/profiler" h "github.com/cortezaproject/corteza-server/pkg/http" "github.com/cortezaproject/corteza-server/pkg/options" + "go.uber.org/zap" ) const ( devHelperResponseBody string = `Hey developer!` ) -func helperDefaultResponse(opt *options.ApigwOpt, pr *profiler.Profiler) http.HandlerFunc { +func helperDefaultResponse(opt options.ApigwOpt, pr *profiler.Profiler, log *zap.Logger) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - addToProfiler(opt, pr, r, http.StatusNotFound) + addToProfiler(opt, pr, log, r, http.StatusNotFound) + + responseBody := "" if opt.LogEnabled { // Say something friendly when logging is enabled - http.Error(w, devHelperResponseBody, http.StatusTeapot) - } else { - // Default 404 response - http.Error(w, "", http.StatusNotFound) + responseBody = devHelperResponseBody } + + http.Error(w, responseBody, http.StatusNotFound) } } -func helperMethodNotAllowed(opt *options.ApigwOpt, pr *profiler.Profiler) http.HandlerFunc { +func helperMethodNotAllowed(opt options.ApigwOpt, pr *profiler.Profiler, log *zap.Logger) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - addToProfiler(opt, pr, r, http.StatusMethodNotAllowed) + addToProfiler(opt, pr, log, r, http.StatusMethodNotAllowed) if opt.LogEnabled { // Say something friendly when logging is enabled @@ -40,7 +42,7 @@ func helperMethodNotAllowed(opt *options.ApigwOpt, pr *profiler.Profiler) http.H } } -func addToProfiler(opt *options.ApigwOpt, pr *profiler.Profiler, r *http.Request, status int) { +func addToProfiler(opt options.ApigwOpt, pr *profiler.Profiler, log *zap.Logger, r *http.Request, status int) { if !(opt.ProfilerEnabled && opt.ProfilerGlobal) { return } @@ -49,7 +51,8 @@ func addToProfiler(opt *options.ApigwOpt, pr *profiler.Profiler, r *http.Request ar, err := h.NewRequest(r) if err != nil { - panic(err) + log.Warn("could not create request wrapper, not adding to profiler") + return } h := pr.Hit(ar) diff --git a/pkg/apigw/registry/registry.go b/pkg/apigw/registry/registry.go index 746d7a883..770f722f5 100644 --- a/pkg/apigw/registry/registry.go +++ b/pkg/apigw/registry/registry.go @@ -13,14 +13,14 @@ import ( type ( Registry struct { - opts *options.ApigwOpt + opts options.ApigwOpt h map[string]types.Handler } secureStorageTodo struct{} ) -func NewRegistry(opts *options.ApigwOpt) *Registry { +func NewRegistry(opts options.ApigwOpt) *Registry { return &Registry{ h: map[string]types.Handler{}, opts: opts, diff --git a/pkg/apigw/registry/registry_test.go b/pkg/apigw/registry/registry_test.go index 3f3fe47dc..fa0ba603e 100644 --- a/pkg/apigw/registry/registry_test.go +++ b/pkg/apigw/registry/registry_test.go @@ -11,7 +11,7 @@ import ( func Test_registryAddGet(t *testing.T) { var ( req = require.New(t) - r = NewRegistry(&options.ApigwOpt{}) + r = NewRegistry(options.ApigwOpt{}) ) r.Add("mockHandler", types.MockHandler{}) @@ -26,7 +26,7 @@ func Test_registryAddGet(t *testing.T) { func Test_registryAddGetErr(t *testing.T) { var ( req = require.New(t) - r = NewRegistry(&options.ApigwOpt{}) + r = NewRegistry(options.ApigwOpt{}) ) r.Add("mockHandler", types.MockHandler{}) @@ -72,7 +72,7 @@ func Test_registryMerge(t *testing.T) { for _, tc := range tcc { var ( req = require.New(t) - r = NewRegistry(&options.ApigwOpt{}) + r = NewRegistry(options.ApigwOpt{}) ) m, err := r.Merge(types.MockHandler{}, []byte(tc.params)) @@ -90,7 +90,7 @@ func Test_registryMerge(t *testing.T) { func Test_registryAll(t *testing.T) { var ( req = require.New(t) - r = NewRegistry(&options.ApigwOpt{}) + r = NewRegistry(options.ApigwOpt{}) ) r.Add("mockHandler", types.MockHandler{}) diff --git a/pkg/apigw/route.go b/pkg/apigw/route.go index e0b5976c4..c66a907b5 100644 --- a/pkg/apigw/route.go +++ b/pkg/apigw/route.go @@ -21,7 +21,7 @@ type ( method string meta routeMeta - opts *options.ApigwOpt + opts options.ApigwOpt log *zap.Logger pr *profiler.Profiler diff --git a/pkg/apigw/route_test.go b/pkg/apigw/route_test.go index 552dc30e1..059588362 100644 --- a/pkg/apigw/route_test.go +++ b/pkg/apigw/route_test.go @@ -95,7 +95,7 @@ func Test_pl(t *testing.T) { method: tc.method, endpoint: tc.endpoint, log: zap.NewNop(), - opts: options.Apigw(), + opts: *options.Apigw(), handler: pipe.Handler(), errHandler: pipe.Error(), } diff --git a/pkg/apigw/service.go b/pkg/apigw/service.go index 382e969c4..31a807cab 100644 --- a/pkg/apigw/service.go +++ b/pkg/apigw/service.go @@ -28,7 +28,7 @@ type ( } apigw struct { - opts *options.ApigwOpt + opts options.ApigwOpt log *zap.Logger reg *registry.Registry routes []*route @@ -48,7 +48,7 @@ func Service() *apigw { } // Setup handles the singleton service -func Setup(opts *options.ApigwOpt, log *zap.Logger, storer storer) { +func Setup(opts options.ApigwOpt, log *zap.Logger, storer storer) { if apiGw != nil { return } @@ -56,7 +56,7 @@ func Setup(opts *options.ApigwOpt, log *zap.Logger, storer storer) { apiGw = New(opts, log, storer) } -func New(opts *options.ApigwOpt, logger *zap.Logger, storer storer) *apigw { +func New(opts options.ApigwOpt, logger *zap.Logger, storer storer) *apigw { var ( pr = profiler.New() reg = registry.NewRegistry(opts) @@ -66,7 +66,7 @@ func New(opts *options.ApigwOpt, logger *zap.Logger, storer storer) *apigw { return &apigw{ opts: opts, - log: logger, + log: logger.Named("http.apigw"), storer: storer, reg: reg, pr: pr, @@ -84,7 +84,7 @@ func (s *apigw) ServeHTTP(w http.ResponseWriter, r *http.Request) { } if len(s.routes) == 0 { - helperDefaultResponse(s.opts, s.pr)(w, r) + helperDefaultResponse(s.opts, s.pr, s.log)(w, r) return } @@ -105,12 +105,10 @@ func (s *apigw) Reload(ctx context.Context) (err error) { routes, err := s.loadRoutes(ctx) if err != nil { - s.log.Error("could not reload API Gateway routes", zap.Error(err)) + s.log.Error("could not reload Integration Gateway routes", zap.Error(err)) return } - s.log.Debug("reloading API Gateway routes and functions", zap.Int("count", len(routes))) - s.Init(ctx, routes...) // Rebuild the mux @@ -125,8 +123,8 @@ func (s *apigw) Reload(ctx context.Context) (err error) { // profiler gets the missed hit info also { var ( - defaultMethodResponse = helperMethodNotAllowed(s.opts, s.pr) - defaultResponse = helperDefaultResponse(s.opts, s.pr) + defaultMethodResponse = helperMethodNotAllowed(s.opts, s.pr, s.log) + defaultResponse = helperDefaultResponse(s.opts, s.pr, s.log) ) s.mx.NotFound(defaultResponse) @@ -144,6 +142,7 @@ func (s *apigw) Init(ctx context.Context, routes ...*route) { s.routes = routes + s.loadInfo() s.log.Debug("registering routes", zap.Int("count", len(s.routes))) defaultPostFilter, err := s.reg.Get("defaultJsonResponse") @@ -301,6 +300,31 @@ func (s *apigw) loadFilters(ctx context.Context, route uint64) (ff []*st.ApigwFi return } +func (s *apigw) loadInfo() { + s.log.Info("loading Integration Gateway", zap.Bool("debug", s.opts.Debug), zap.Bool("log", s.opts.LogEnabled)) + + if s.opts.ProfilerEnabled { + if s.opts.LogRequestBody { + s.log.Info("profiler and request body logging is enabled, profiler use is prefered", + zap.Bool("APIGW_PROFILER_ENABLED", s.opts.ProfilerEnabled), + zap.Bool("APIGW_LOG_REQUEST_BODY", s.opts.LogRequestBody)) + } else { + s.log.Info("request body logging is enabled, profiler use is prefered (APIGW_PROFILER_ENABLED)", + zap.Bool("APIGW_LOG_REQUEST_BODY", s.opts.LogRequestBody)) + } + + if !s.opts.ProfilerGlobal { + s.log.Warn("profiler enabled only for routes with a profiler prefilter, use global setting to enable for all (APIGW_PROFILER_GLOBAL)") + } + } else { + if s.opts.ProfilerGlobal { + s.log.Warn("profiler global is enabled, but profiler disabled, no routes will be profiled", + zap.Bool("APIGW_PROFILER_ENABLED", s.opts.ProfilerEnabled), + zap.Bool("APIGW_PROFILER_GLOBAL", s.opts.ProfilerGlobal)) + } + } +} + func (s *apigw) Profiler() *profiler.Profiler { return s.pr } diff --git a/pkg/apigw/service_test.go b/pkg/apigw/service_test.go index 6e5943db3..fae593384 100644 --- a/pkg/apigw/service_test.go +++ b/pkg/apigw/service_test.go @@ -160,7 +160,7 @@ func Test_serviceInit(t *testing.T) { ctx = context.Background() ) - reg := registry.NewRegistry(&options.ApigwOpt{}) + reg := registry.NewRegistry(options.ApigwOpt{}) for hn, h := range tc.reg { reg.Add(hn, h) diff --git a/pkg/apigw/types/handler.go b/pkg/apigw/types/handler.go index f9d56930c..78f7803ed 100644 --- a/pkg/apigw/types/handler.go +++ b/pkg/apigw/types/handler.go @@ -20,7 +20,7 @@ type ( HTTPHandler fmt.Stringer - New(*options.ApigwOpt) Handler + New(options.ApigwOpt) Handler Merge([]byte) (Handler, error) Meta() FilterMeta Enabled() bool diff --git a/pkg/apigw/types/test.go b/pkg/apigw/types/test.go index 3968b3cea..43012b63d 100644 --- a/pkg/apigw/types/test.go +++ b/pkg/apigw/types/test.go @@ -33,7 +33,7 @@ type ( MockRoundTripper func(*http.Request) (*http.Response, error) ) -func (h MockHandler) New(opts *options.ApigwOpt) Handler { +func (h MockHandler) New(opts options.ApigwOpt) Handler { return MockHandler{} } diff --git a/tests/apigw/main_test.go b/tests/apigw/main_test.go index c8cefef23..f6a675cc9 100644 --- a/tests/apigw/main_test.go +++ b/tests/apigw/main_test.go @@ -82,7 +82,7 @@ func InitTestApp() { r.Group(rest.MountRoutes()) // API gw routes - apigw.Setup(options.Apigw(), service.DefaultLogger, service.DefaultStore) + apigw.Setup(*options.Apigw(), service.DefaultLogger, service.DefaultStore) err := apigw.Service().Reload(ctx) if err != nil { panic(err)