Add config.js option for discovery base url
This commit is contained in:
+1
-1
@@ -69,7 +69,7 @@ func (app *CortezaApp) mountHttpRoutes(r chi.Router) {
|
||||
return
|
||||
}
|
||||
|
||||
r.Route(options.CleanBase(ho.WebappBaseUrl), webapp.MakeWebappServer(app.Log, ho, app.Opt.Auth))
|
||||
r.Route(options.CleanBase(ho.WebappBaseUrl), webapp.MakeWebappServer(app.Log, ho, app.Opt.Auth, app.Opt.Discovery))
|
||||
|
||||
app.Log.Info(
|
||||
"client web applications enabled",
|
||||
|
||||
Generated
+2
@@ -13,6 +13,7 @@ type (
|
||||
Enabled bool `env:"DISCOVERY_ENABLED"`
|
||||
Debug bool `env:"DISCOVERY_DEBUG"`
|
||||
CortezaDomain string `env:"DISCOVERY_CORTEZA_DOMAIN"`
|
||||
BaseUrl string `env:"DISCOVERY_BASE_URL"`
|
||||
}
|
||||
)
|
||||
|
||||
@@ -22,6 +23,7 @@ func Discovery() (o *DiscoveryOpt) {
|
||||
Enabled: false,
|
||||
Debug: false,
|
||||
CortezaDomain: "",
|
||||
BaseUrl: "",
|
||||
}
|
||||
|
||||
fill(o)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
props:
|
||||
- name: Enabled
|
||||
- name: enabled
|
||||
type: bool
|
||||
default: false
|
||||
description: Enable discovery endpoints
|
||||
@@ -10,8 +10,14 @@ props:
|
||||
docs:
|
||||
description: Enable debug activity logging.
|
||||
|
||||
- name: CortezaDomain
|
||||
- name: cortezaDomain
|
||||
type: string
|
||||
default: ""
|
||||
docs:
|
||||
description: Provides host of corteza compose webapp.
|
||||
|
||||
- name: baseUrl
|
||||
type: string
|
||||
default: ""
|
||||
docs:
|
||||
description: Provides host of corteza server discovery.
|
||||
|
||||
+8
-6
@@ -20,10 +20,11 @@ var (
|
||||
baseHrefMatcher = regexp.MustCompile(`<base\s+href="?.+?"?\s*\/?>`)
|
||||
)
|
||||
|
||||
func MakeWebappServer(log *zap.Logger, httpSrvOpt options.HTTPServerOpt, authOpt options.AuthOpt) func(r chi.Router) {
|
||||
func MakeWebappServer(log *zap.Logger, httpSrvOpt options.HTTPServerOpt, authOpt options.AuthOpt, discoveryOpt options.DiscoveryOpt) func(r chi.Router) {
|
||||
var (
|
||||
apiBaseUrl = options.CleanBase(httpSrvOpt.BaseUrl, httpSrvOpt.ApiBaseUrl)
|
||||
apps = strings.Split(httpSrvOpt.WebappList, ",")
|
||||
apiBaseUrl = options.CleanBase(httpSrvOpt.BaseUrl, httpSrvOpt.ApiBaseUrl)
|
||||
discoveryApiBaseUrl = options.CleanBase(discoveryOpt.BaseUrl)
|
||||
apps = strings.Split(httpSrvOpt.WebappList, ",")
|
||||
|
||||
appIndexHTMLs = make(map[string][]byte)
|
||||
|
||||
@@ -48,12 +49,12 @@ func MakeWebappServer(log *zap.Logger, httpSrvOpt options.HTTPServerOpt, authOpt
|
||||
|
||||
for _, app := range apps {
|
||||
webBaseUrl = options.CleanBase(httpSrvOpt.WebappBaseUrl, app)
|
||||
serveConfig(r, webBaseUrl, apiBaseUrl, authOpt.BaseURL, httpSrvOpt.BaseUrl)
|
||||
serveConfig(r, webBaseUrl, apiBaseUrl, authOpt.BaseURL, httpSrvOpt.BaseUrl, discoveryApiBaseUrl)
|
||||
r.Get(webBaseUrl+"*", serveIndex(httpSrvOpt, appIndexHTMLs[app], fs))
|
||||
}
|
||||
|
||||
webBaseUrl = options.CleanBase(httpSrvOpt.WebappBaseUrl)
|
||||
serveConfig(r, webBaseUrl, apiBaseUrl, authOpt.BaseURL, httpSrvOpt.BaseUrl)
|
||||
serveConfig(r, webBaseUrl, apiBaseUrl, authOpt.BaseURL, httpSrvOpt.BaseUrl, discoveryApiBaseUrl)
|
||||
r.Get(webBaseUrl+"*", serveIndex(httpSrvOpt, appIndexHTMLs[""], fs))
|
||||
}
|
||||
}
|
||||
@@ -94,12 +95,13 @@ func serveIndex(opt options.HTTPServerOpt, indexHTML []byte, serve http.Handler)
|
||||
}
|
||||
}
|
||||
|
||||
func serveConfig(r chi.Router, appUrl, apiBaseUrl, authBaseUrl, webappBaseUrl string) {
|
||||
func serveConfig(r chi.Router, appUrl, apiBaseUrl, authBaseUrl, webappBaseUrl, discoveryApiBaseUrl string) {
|
||||
r.Get(options.CleanBase(appUrl, "config.js"), func(w http.ResponseWriter, r *http.Request) {
|
||||
const line = "window.%s = '%s';\n"
|
||||
_, _ = fmt.Fprintf(w, line, "CortezaAPI", apiBaseUrl)
|
||||
_, _ = fmt.Fprintf(w, line, "CortezaAuth", authBaseUrl)
|
||||
_, _ = fmt.Fprintf(w, line, "CortezaWebapp", webappBaseUrl)
|
||||
_, _ = fmt.Fprintf(w, line, "CortezaDiscoveryAPI", discoveryApiBaseUrl)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user