-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow overriding base path for UI/API routes; rm --query.prefix #748
Changes from all commits
be11738
62ac673
01d09b6
44611cd
240d4ac
de4221a
7a019df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<meta charset="UTF-8"> | ||
<base href="/" data-inject-target="BASE_URL"/> | ||
<title>Test Page</title> | ||
<!-- JAEGER_CONFIG=DEFAULT_CONFIG; --> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
some asset |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(`<base href="/"`) | ||
basePathReplace = `<base href="%s/"` | ||
errBadBasePath = "Invalid base path '%s'. Must start but not end with a slash '/', e.g. '/jaeger/ui'" | ||
) | ||
|
||
// RegisterStaticHandler adds handler for static assets to the router. | ||
func RegisterStaticHandler(r *mux.Router, logger *zap.Logger, qOpts *QueryOptions) { | ||
staticHandler, err := NewStaticAssetsHandler(qOpts.StaticAssets, qOpts.UIConfig) | ||
staticHandler, err := NewStaticAssetsHandler(qOpts.StaticAssets, StaticAssetsHandlerOptions{ | ||
BasePath: qOpts.BasePath, | ||
UIConfigPath: qOpts.UIConfig, | ||
}) | ||
if err != nil { | ||
logger.Panic("Could not create static assets handler", zap.Error(err)) | ||
} | ||
|
@@ -48,12 +55,19 @@ func RegisterStaticHandler(r *mux.Router, logger *zap.Logger, qOpts *QueryOption | |
|
||
// StaticAssetsHandler handles static assets | ||
type StaticAssetsHandler struct { | ||
options StaticAssetsHandlerOptions | ||
staticAssetsRoot string | ||
indexHTML []byte | ||
} | ||
|
||
// StaticAssetsHandlerOptions defines options for NewStaticAssetsHandler | ||
type StaticAssetsHandlerOptions struct { | ||
BasePath string | ||
UIConfigPath string | ||
} | ||
|
||
// NewStaticAssetsHandler returns a StaticAssetsHandler | ||
func NewStaticAssetsHandler(staticAssetsRoot string, uiConfig string) (*StaticAssetsHandler, error) { | ||
func NewStaticAssetsHandler(staticAssetsRoot string, options StaticAssetsHandlerOptions) (*StaticAssetsHandler, error) { | ||
if staticAssetsRoot == "" { | ||
return nil, nil | ||
} | ||
|
@@ -65,7 +79,7 @@ func NewStaticAssetsHandler(staticAssetsRoot string, uiConfig string) (*StaticAs | |
return nil, errors.Wrap(err, "Cannot read UI static assets") | ||
} | ||
configString := "JAEGER_CONFIG = DEFAULT_CONFIG" | ||
if config, err := loadUIConfig(uiConfig); err != nil { | ||
if config, err := loadUIConfig(options.UIConfigPath); err != nil { | ||
return nil, err | ||
} else if config != nil { | ||
// TODO if we want to support other config formats like YAML, we need to normalize `config` to be | ||
|
@@ -74,9 +88,20 @@ func NewStaticAssetsHandler(staticAssetsRoot string, uiConfig string) (*StaticAs | |
bytes, _ := json.Marshal(config) | ||
configString = fmt.Sprintf("JAEGER_CONFIG = %v", string(bytes)) | ||
} | ||
indexBytes = configPattern.ReplaceAll(indexBytes, []byte(configString+";")) | ||
if options.BasePath == "" { | ||
options.BasePath = "/" | ||
} | ||
if options.BasePath != "/" { | ||
if !strings.HasPrefix(options.BasePath, "/") || strings.HasSuffix(options.BasePath, "/") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to add a log statement notifying that what people see in the HTML is not what will be served to clients? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you elaborate why you think this would be useful? We're modifying the internal file of the app, not something that users provide. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mainly because it's deviating from what is "expected": if I had a problem with the static resources (like the original problem this PR is solving) and were to check the HTML, I would wonder why it's not the same as what my browser is seeing. A log entry at There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. well, still not convinced why someone needs to know what happens internally, e.g. we also override the config, and in both cases we only do that when requested via cli arguments. Let's do it separately, because the constructor doesn't take a logger and the unit tests explicitly verify that nothing is logged, so it's not as trivial a change as it sounds for a feature that seems rather marginal. |
||
return nil, fmt.Errorf(errBadBasePath, options.BasePath) | ||
} | ||
indexBytes = basePathPattern.ReplaceAll(indexBytes, []byte(fmt.Sprintf(basePathReplace, options.BasePath))) | ||
} | ||
return &StaticAssetsHandler{ | ||
options: options, | ||
staticAssetsRoot: staticAssetsRoot, | ||
indexHTML: configPattern.ReplaceAll(indexBytes, []byte(configString+";")), | ||
indexHTML: indexBytes, | ||
}, nil | ||
} | ||
|
||
|
@@ -108,7 +133,7 @@ func loadUIConfig(uiConfig string) (map[string]interface{}, error) { | |
|
||
// RegisterRoutes registers routes for this handler on the given router | ||
func (sH *StaticAssetsHandler) RegisterRoutes(router *mux.Router) { | ||
router.PathPrefix("/static").Handler(http.FileServer(http.Dir(sH.staticAssetsRoot))) | ||
router.PathPrefix("/static").Handler(sH.fileHandler()) | ||
for _, file := range staticRootFiles { | ||
router.Path("/" + file).HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
http.ServeFile(w, r, sH.staticAssetsRoot+file) | ||
|
@@ -117,6 +142,22 @@ func (sH *StaticAssetsHandler) RegisterRoutes(router *mux.Router) { | |
router.NotFoundHandler = http.HandlerFunc(sH.notFound) | ||
} | ||
|
||
func (sH *StaticAssetsHandler) fileHandler() http.Handler { | ||
fs := http.FileServer(http.Dir(sH.staticAssetsRoot)) | ||
base := sH.options.BasePath | ||
if base == "/" { | ||
return fs | ||
} | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if strings.HasPrefix(r.URL.Path, base) { | ||
// gorilla Subroute() is a bit odd, it keeps the base path in the URL, | ||
// which prevents the FileServer from locating the files, so we strip the prefix. | ||
r.URL.Path = r.URL.Path[len(base):] | ||
} | ||
fs.ServeHTTP(w, r) | ||
}) | ||
} | ||
|
||
func (sH *StaticAssetsHandler) notFound(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "text/html; charset=utf-8") | ||
w.Write(sH.indexHTML) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tiffon this models the compiled
index.html
from UI assets.Q: is it possible that babel/webpack would reorder the
href
anddata-inject-target
attributes? Because if it does, the search&replace in the query service won't workThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yurishkuro It should not change the order of the attributes.