-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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 embedding custom UI config in index.html #490
Changes from all commits
9b8b78f
37c39a9
76dc275
b3765bb
0b554f3
dff7116
daedfd3
ef15909
8eefa3c
b557609
72c09d1
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 |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
<html lang="en"> | ||
<meta charset="UTF-8"> | ||
<title>Test Page</title> | ||
<!-- JAEGER_CONFIG=DEFAULT_CONFIG; --> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"x" == "y"} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"menu": [ | ||
{ | ||
"label": "GitHub", | ||
"url": "https://github.com/jaegertracing/jaeger" | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"x": "y" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
x: abcd | ||
z: | ||
- a | ||
- b |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,10 +15,16 @@ | |
package app | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
const ( | ||
|
@@ -27,22 +33,67 @@ const ( | |
|
||
var ( | ||
staticRootFiles = []string{"favicon.ico"} | ||
configPattern = regexp.MustCompile("JAEGER_CONFIG *= *DEFAULT_CONFIG;") | ||
) | ||
|
||
// StaticAssetsHandler handles static assets | ||
type StaticAssetsHandler struct { | ||
staticAssetsRoot string | ||
indexHTML []byte | ||
} | ||
|
||
// NewStaticAssetsHandler returns a StaticAssetsHandler | ||
func NewStaticAssetsHandler(staticAssetsRoot string) *StaticAssetsHandler { | ||
func NewStaticAssetsHandler(staticAssetsRoot string, uiConfig string) (*StaticAssetsHandler, error) { | ||
if staticAssetsRoot == "" { | ||
staticAssetsRoot = defaultStaticAssetsRoot | ||
} | ||
if !strings.HasSuffix(staticAssetsRoot, "/") { | ||
staticAssetsRoot = staticAssetsRoot + "/" | ||
} | ||
return &StaticAssetsHandler{staticAssetsRoot: staticAssetsRoot} | ||
indexBytes, err := ioutil.ReadFile(staticAssetsRoot + "index.html") | ||
if err != nil { | ||
return nil, errors.Wrap(err, "Cannot read UI static assets") | ||
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. Now it fails without UI. Before it was working. Workaround is to create empty #493 uses query as a docker container, when xdock is executed it builds all images, Now we have to build UI which takes significant time. 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. let's discuss on #497 |
||
} | ||
configString := "JAEGER_CONFIG = DEFAULT_CONFIG" | ||
if config, err := loadUIConfig(uiConfig); 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 | ||
// suitable for json.Marshal(). For example, YAML parser may return a map that has keys of type | ||
// interface{}, and json.Marshal() is unable to serialize it. | ||
bytes, _ := json.Marshal(config) | ||
configString = fmt.Sprintf("JAEGER_CONFIG = %v", string(bytes)) | ||
} | ||
return &StaticAssetsHandler{ | ||
staticAssetsRoot: staticAssetsRoot, | ||
indexHTML: configPattern.ReplaceAll(indexBytes, []byte(configString+";")), | ||
}, nil | ||
} | ||
|
||
func loadUIConfig(uiConfig string) (map[string]interface{}, error) { | ||
if uiConfig == "" { | ||
return nil, nil | ||
} | ||
ext := filepath.Ext(uiConfig) | ||
bytes, err := ioutil.ReadFile(uiConfig) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "Cannot read UI config file %v", uiConfig) | ||
} | ||
|
||
var c map[string]interface{} | ||
var unmarshal func([]byte, interface{}) error | ||
|
||
switch strings.ToLower(ext) { | ||
case ".json": | ||
unmarshal = json.Unmarshal | ||
default: | ||
return nil, fmt.Errorf("Unrecognized UI config file format %v", uiConfig) | ||
} | ||
|
||
if err := unmarshal(bytes, &c); err != nil { | ||
return nil, errors.Wrapf(err, "Cannot parse UI config file %v", uiConfig) | ||
} | ||
return c, nil | ||
} | ||
|
||
// RegisterRoutes registers routes for this handler on the given router | ||
|
@@ -57,8 +108,6 @@ func (sH *StaticAssetsHandler) RegisterRoutes(router *mux.Router) { | |
} | ||
|
||
func (sH *StaticAssetsHandler) notFound(w http.ResponseWriter, r *http.Request) { | ||
// don't allow returning "304 Not Modified" for index.html because | ||
// the cached versions might have the wrong filenames for javascript assets | ||
delete(r.Header, "If-Modified-Since") | ||
http.ServeFile(w, r, sH.staticAssetsRoot+"index.html") | ||
w.Header().Set("Content-Type", "text/html; charset=utf-8") | ||
w.Write(sH.indexHTML) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ EXPOSE 8080 | |
|
||
COPY .build/scripts/* /scripts/ | ||
COPY .build/cmd/* /cmd/ | ||
COPY .build/ui/* /ui/ |
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.
change this to: "JAEGER_CONFIG = DEFAULT_CONFIG;" to be a more accurate fixture?
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.
how is it more accurate? The regexp ignores the whitespace.