From 83d06da9a91bc8cc45c1576ae4408d5c55d35b33 Mon Sep 17 00:00:00 2001 From: Yuri Shkuro Date: Fri, 23 Mar 2018 15:32:48 -0400 Subject: [PATCH] Allow overriding base path for UI/API routes; rm --query.prefix (#748) Signed-off-by: Yuri Shkuro --- cmd/query/app/fixture/index.html | 1 + cmd/query/app/fixture/static/asset.txt | 1 + cmd/query/app/flags.go | 12 +-- cmd/query/app/flags_test.go | 4 +- cmd/query/app/handler.go | 11 +-- cmd/query/app/handler_options.go | 11 ++- cmd/query/app/handler_test.go | 4 +- cmd/query/app/static_handler.go | 53 +++++++++++-- cmd/query/app/static_handler_test.go | 100 ++++++++++++++----------- cmd/query/main.go | 4 +- cmd/standalone/main.go | 4 +- jaeger-ui | 2 +- 12 files changed, 137 insertions(+), 70 deletions(-) create mode 100644 cmd/query/app/fixture/static/asset.txt diff --git a/cmd/query/app/fixture/index.html b/cmd/query/app/fixture/index.html index 159ece5a20d..bc5b89f5a43 100644 --- a/cmd/query/app/fixture/index.html +++ b/cmd/query/app/fixture/index.html @@ -1,6 +1,7 @@ + Test Page diff --git a/cmd/query/app/fixture/static/asset.txt b/cmd/query/app/fixture/static/asset.txt new file mode 100644 index 00000000000..3e20b5edaef --- /dev/null +++ b/cmd/query/app/fixture/static/asset.txt @@ -0,0 +1 @@ +some asset diff --git a/cmd/query/app/flags.go b/cmd/query/app/flags.go index 824842a33f6..9cdfb757be7 100644 --- a/cmd/query/app/flags.go +++ b/cmd/query/app/flags.go @@ -22,7 +22,7 @@ import ( const ( queryPort = "query.port" - queryPrefix = "query.prefix" + queryBasePath = "query.base-path" queryStaticFiles = "query.static-files" queryUIConfig = "query.ui-config" queryHealthCheckHTTPPort = "query.health-check-http-port" @@ -32,8 +32,8 @@ const ( type QueryOptions struct { // Port is the port that the query service listens in on Port int - // Prefix is the prefix of the query service api - Prefix string + // BasePath is the prefix for all UI and API HTTP routes + BasePath string // StaticAssets is the path for the static assets for the UI (https://github.com/uber/jaeger-ui) StaticAssets string // UIConfig is the path to a configuration file for the UI @@ -45,8 +45,8 @@ type QueryOptions struct { // AddFlags adds flags for QueryOptions func AddFlags(flagSet *flag.FlagSet) { flagSet.Int(queryPort, 16686, "The port for the query service") - flagSet.String(queryPrefix, "api", "The prefix for the url of the query service") - flagSet.String(queryStaticFiles, "jaeger-ui-build/build/", "The path for the static assets for the UI") + flagSet.String(queryBasePath, "/", "The base path for all HTTP routes, e.g. /jaeger; useful when running behind a reverse proxy") + flagSet.String(queryStaticFiles, "jaeger-ui-build/build/", "The directory path for the static assets for the UI") flagSet.String(queryUIConfig, "", "The path to the UI configuration file in JSON format") flagSet.Int(queryHealthCheckHTTPPort, 16687, "The http port for the health check service") } @@ -54,7 +54,7 @@ func AddFlags(flagSet *flag.FlagSet) { // InitFromViper initializes QueryOptions with properties from viper func (qOpts *QueryOptions) InitFromViper(v *viper.Viper) *QueryOptions { qOpts.Port = v.GetInt(queryPort) - qOpts.Prefix = v.GetString(queryPrefix) + qOpts.BasePath = v.GetString(queryBasePath) qOpts.StaticAssets = v.GetString(queryStaticFiles) qOpts.UIConfig = v.GetString(queryUIConfig) qOpts.HealthCheckHTTPPort = v.GetInt(queryHealthCheckHTTPPort) diff --git a/cmd/query/app/flags_test.go b/cmd/query/app/flags_test.go index 555d3920290..dcdb843c23f 100644 --- a/cmd/query/app/flags_test.go +++ b/cmd/query/app/flags_test.go @@ -27,12 +27,12 @@ func TestQueryBuilderFlags(t *testing.T) { command.ParseFlags([]string{ "--query.static-files=/dev/null", "--query.ui-config=some.json", - "--query.prefix=api", + "--query.base-path=/jaeger", "--query.port=80", }) qOpts := new(QueryOptions).InitFromViper(v) assert.Equal(t, "/dev/null", qOpts.StaticAssets) assert.Equal(t, "some.json", qOpts.UIConfig) - assert.Equal(t, "api", qOpts.Prefix) + assert.Equal(t, "/jaeger", qOpts.BasePath) assert.Equal(t, 80, qOpts.Port) } diff --git a/cmd/query/app/handler.go b/cmd/query/app/handler.go index 2224266c389..b499bac1a2b 100644 --- a/cmd/query/app/handler.go +++ b/cmd/query/app/handler.go @@ -43,7 +43,7 @@ const ( defaultDependencyLookbackDuration = time.Hour * 24 defaultTraceQueryLookbackDuration = time.Hour * 24 * 2 - defaultHTTPPrefix = "api" + defaultAPIPrefix = "api" ) var ( @@ -78,7 +78,8 @@ type APIHandler struct { adjuster adjuster.Adjuster logger *zap.Logger queryParser queryParser - httpPrefix string + basePath string + apiPrefix string tracer opentracing.Tracer } @@ -96,8 +97,8 @@ func NewAPIHandler(spanReader spanstore.Reader, dependencyReader dependencystore for _, option := range options { option(aH) } - if aH.httpPrefix == "" { - aH.httpPrefix = defaultHTTPPrefix + if aH.apiPrefix == "" { + aH.apiPrefix = defaultAPIPrefix } if aH.adjuster == nil { aH.adjuster = adjuster.Sequence(StandardAdjusters...) @@ -141,7 +142,7 @@ func (aH *APIHandler) handleFunc( } func (aH *APIHandler) route(route string, args ...interface{}) string { - args = append([]interface{}{aH.httpPrefix}, args...) + args = append([]interface{}{aH.apiPrefix}, args...) return fmt.Sprintf("/%s"+route, args...) } diff --git a/cmd/query/app/handler_options.go b/cmd/query/app/handler_options.go index b494688d594..49cec6feec0 100644 --- a/cmd/query/app/handler_options.go +++ b/cmd/query/app/handler_options.go @@ -47,10 +47,17 @@ func (handlerOptions) Adjusters(adjusters ...adjuster.Adjuster) HandlerOption { } } -// Prefix creates a HandlerOption that initializes prefix HTTP prefix of the API +// BasePath creates a HandlerOption that initializes the base path for all HTTP routes +func (handlerOptions) BasePath(prefix string) HandlerOption { + return func(apiHandler *APIHandler) { + apiHandler.basePath = prefix + } +} + +// Prefix creates a HandlerOption that initializes the HTTP prefix for API routes func (handlerOptions) Prefix(prefix string) HandlerOption { return func(apiHandler *APIHandler) { - apiHandler.httpPrefix = prefix + apiHandler.apiPrefix = prefix } } diff --git a/cmd/query/app/handler_test.go b/cmd/query/app/handler_test.go index 7fb833c9fa4..2d88f21e43c 100644 --- a/cmd/query/app/handler_test.go +++ b/cmd/query/app/handler_test.go @@ -85,7 +85,9 @@ func initializeTestServerWithHandler(options ...HandlerOption) (*httptest.Server append( []HandlerOption{ HandlerOptions.Logger(zap.NewNop()), - HandlerOptions.Prefix(defaultHTTPPrefix), + // add options for test coverage + HandlerOptions.Prefix(defaultAPIPrefix), + HandlerOptions.BasePath("/"), HandlerOptions.QueryLookbackDuration(defaultTraceQueryLookbackDuration), }, options..., diff --git a/cmd/query/app/static_handler.go b/cmd/query/app/static_handler.go index 5924a306177..7af5d18207c 100644 --- a/cmd/query/app/static_handler.go +++ b/cmd/query/app/static_handler.go @@ -29,13 +29,20 @@ import ( ) var ( - staticRootFiles = []string{"favicon.ico"} + favoriteIcon = "favicon.ico" + staticRootFiles = []string{favoriteIcon} configPattern = regexp.MustCompile("JAEGER_CONFIG *= *DEFAULT_CONFIG;") + basePathPattern = regexp.MustCompile(`