| 61 | } |
| 62 | |
| 63 | func wrapRouter(controller *controller.Controller, runOptions *options.ControllerOptions) func(ctx *fasthttp.RequestCtx) { |
| 64 | router := routing.New() |
| 65 | router.Get("/healthz", controller.HealthzHandler) |
| 66 | |
| 67 | router.Post("/v1/functions/<functionName>/invocations", controller.InvokeHandler) |
| 68 | router.Get("/v1/runtimes", controller.ListRuntimesHandler) |
| 69 | router.Get("/v1/resource", controller.GetResourceHandler) |
| 70 | router.Post("/v1/runtimes/<runtimeID>/invalidate", controller.InvalidateRuntime) |
| 71 | |
| 72 | if runOptions.HTTPEnhanced { |
| 73 | logs.V(9).Info("equipped with http trigger feature") |
| 74 | router.Any(`/<userID:\w+>/<functionName>`, httptrigger.ProxyHandler) |
| 75 | router.Any(`/<userID:\w+>/<functionName>/<version>`, httptrigger.ProxyHandler) |
| 76 | } |
| 77 | |
| 78 | var enhanceRouting bool |
| 79 | if runOptions.RecommendedOptions.Features.EnableMetrics || runOptions.RecommendedOptions.Features.EnableProfiling { |
| 80 | enhanceRouting = true |
| 81 | } |
| 82 | logs.Infof("enhanceRouting flag %v", enhanceRouting) |
| 83 | if !enhanceRouting { |
| 84 | return router.HandleRequest |
| 85 | } |
| 86 | |
| 87 | enhanceHandler := func(ctx *fasthttp.RequestCtx) { |
| 88 | urlPath := string(ctx.Path()) |
| 89 | if runOptions.RecommendedOptions.Features.EnableProfiling && strings.HasPrefix(urlPath, "/debug/pprof/") { |
| 90 | logs.V(9).Info("enable profiling") |
| 91 | switch urlPath { |
| 92 | case "/debug/pprof/cmdline": |
| 93 | fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Cmdline)(ctx) |
| 94 | case "/debug/pprof/profile": |
| 95 | fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Profile)(ctx) |
| 96 | case "/debug/pprof/symbol": |
| 97 | fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Symbol)(ctx) |
| 98 | case "/debug/pprof/trace": |
| 99 | fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Trace)(ctx) |
| 100 | default: |
| 101 | fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Index)(ctx) |
| 102 | } |
| 103 | return |
| 104 | } |
| 105 | |
| 106 | if runOptions.RecommendedOptions.Features.EnableMetrics && strings.HasPrefix(urlPath, "/metrics") { |
| 107 | logs.V(9).Info("enable metrics") |
| 108 | fasthttpadaptor.NewFastHTTPHandler(promhttp.Handler())(ctx) |
| 109 | return |
| 110 | } |
| 111 | |
| 112 | router.HandleRequest(ctx) |
| 113 | return |
| 114 | } |
| 115 | return enhanceHandler |
| 116 | } |