From 0d9b63635eb1c4608d5af8b3013625d1bc17de14 Mon Sep 17 00:00:00 2001 From: Vivek Patel Date: Mon, 11 Jul 2022 14:58:18 +0530 Subject: [PATCH] Add env config for webapp sentry DSN Also, updates sentry DSN entry in config.js for webapp --- .env.example | 6 +++++ app/options/sentry.cue | 3 +++ app/servers.go | 2 +- pkg/options/options.gen.go | 1 + pkg/webapp/serve.go | 49 ++++++++++++++++++++++++++++++-------- 5 files changed, 50 insertions(+), 11 deletions(-) diff --git a/.env.example b/.env.example index 45a051666..2eed21ad6 100644 --- a/.env.example +++ b/.env.example @@ -1101,6 +1101,12 @@ # Default: # SENTRY_ENVIRONMENT= +############################################################################### +# Set to enable Sentry client for webapp. +# Type: string +# Default: +# SENTRY_WEBAPP_DSN= + ############################################################################### ############################################################################### # Rendering engine diff --git a/app/options/sentry.cue b/app/options/sentry.cue index 32df50e24..b89d4e22d 100644 --- a/app/options/sentry.cue +++ b/app/options/sentry.cue @@ -58,5 +58,8 @@ sentry: schema.#optionsGroup & { environment: { description: "Set reported environment." } + webapp_DSN: { + description: "Set to enable Sentry client for webapp." + } } } diff --git a/app/servers.go b/app/servers.go index 5ec5e38f1..524b7a4a3 100644 --- a/app/servers.go +++ b/app/servers.go @@ -57,7 +57,7 @@ func (app *CortezaApp) mountHttpRoutes(r chi.Router) { return } - r.Route(options.CleanBase(ho.WebappBaseUrl), webapp.MakeWebappServer(app.Log, ho, app.Opt.Auth, app.Opt.Discovery)) + r.Route(options.CleanBase(ho.WebappBaseUrl), webapp.MakeWebappServer(app.Log, ho, app.Opt.Auth, app.Opt.Discovery, app.Opt.Sentry)) app.Log.Info( "client web applications enabled", diff --git a/pkg/options/options.gen.go b/pkg/options/options.gen.go index 9b45965c5..c4e331b59 100644 --- a/pkg/options/options.gen.go +++ b/pkg/options/options.gen.go @@ -223,6 +223,7 @@ type ( Release string `env:"SENTRY_RELEASE"` Dist string `env:"SENTRY_DIST"` Environment string `env:"SENTRY_ENVIRONMENT"` + WebappDSN string `env:"SENTRY_WEBAPP_DSN"` } TemplateOpt struct { diff --git a/pkg/webapp/serve.go b/pkg/webapp/serve.go index 24fbf46c0..64056c486 100644 --- a/pkg/webapp/serve.go +++ b/pkg/webapp/serve.go @@ -16,13 +16,25 @@ import ( "go.uber.org/zap" ) +type ( + webappConfig struct { + appUrl string + apiBaseUrl string + authBaseUrl string + webappBaseUrl string + discoveryApiBaseUrl string + sentryUrl string + } +) + var ( baseHrefMatcher = regexp.MustCompile(``) ) -func MakeWebappServer(log *zap.Logger, httpSrvOpt options.HttpServerOpt, authOpt options.AuthOpt, discoveryOpt options.DiscoveryOpt) func(r chi.Router) { +func MakeWebappServer(log *zap.Logger, httpSrvOpt options.HttpServerOpt, authOpt options.AuthOpt, discoveryOpt options.DiscoveryOpt, sentryOpt options.SentryOpt) func(r chi.Router) { var ( apiBaseUrl = options.CleanBase(httpSrvOpt.BaseUrl, httpSrvOpt.ApiBaseUrl) + webappSentryUrl = sentryOpt.WebappDSN discoveryApiBaseUrl = discoveryOpt.BaseUrl apps = strings.Split(httpSrvOpt.WebappList, ",") @@ -49,12 +61,26 @@ 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, discoveryApiBaseUrl) + serveConfig(r, webappConfig{ + appUrl: webBaseUrl, + apiBaseUrl: apiBaseUrl, + authBaseUrl: authOpt.BaseURL, + webappBaseUrl: httpSrvOpt.BaseUrl, + discoveryApiBaseUrl: discoveryApiBaseUrl, + sentryUrl: webappSentryUrl, + }) r.Get(webBaseUrl+"*", serveIndex(httpSrvOpt, appIndexHTMLs[app], fs)) } webBaseUrl = options.CleanBase(httpSrvOpt.WebappBaseUrl) - serveConfig(r, webBaseUrl, apiBaseUrl, authOpt.BaseURL, httpSrvOpt.BaseUrl, discoveryApiBaseUrl) + serveConfig(r, webappConfig{ + appUrl: webBaseUrl, + apiBaseUrl: apiBaseUrl, + authBaseUrl: authOpt.BaseURL, + webappBaseUrl: httpSrvOpt.BaseUrl, + discoveryApiBaseUrl: discoveryApiBaseUrl, + sentryUrl: webappSentryUrl, + }) r.Get(webBaseUrl+"*", serveIndex(httpSrvOpt, appIndexHTMLs[""], fs)) } } @@ -95,14 +121,17 @@ func serveIndex(opt options.HttpServerOpt, indexHTML []byte, serve http.Handler) } } -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) { +func serveConfig(r chi.Router, config webappConfig) { + r.Get(options.CleanBase(config.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) - if len(discoveryApiBaseUrl) > 0 { - _, _ = fmt.Fprintf(w, line, "CortezaDiscoveryAPI", discoveryApiBaseUrl) + _, _ = fmt.Fprintf(w, line, "CortezaAPI", config.apiBaseUrl) + _, _ = fmt.Fprintf(w, line, "CortezaAuth", config.authBaseUrl) + _, _ = fmt.Fprintf(w, line, "CortezaWebapp", config.webappBaseUrl) + if len(config.discoveryApiBaseUrl) > 0 { + _, _ = fmt.Fprintf(w, line, "CortezaDiscoveryAPI", config.discoveryApiBaseUrl) + } + if len(config.sentryUrl) > 0 { + _, _ = fmt.Fprintf(w, line, "SentryDSN", config.sentryUrl) } }) }