diff options
| author | Aleksandr Nogikh <nogikh@google.com> | 2023-10-09 16:30:20 +0200 |
|---|---|---|
| committer | Aleksandr Nogikh <nogikh@google.com> | 2023-10-12 11:18:22 +0000 |
| commit | d753e779cd5f71b91eaca2e18c383c6598e77c9f (patch) | |
| tree | 65aee07af8b18ce7d5736e8420a966bd7abf3c45 /dashboard/app/handler.go | |
| parent | 3cefb1441cc82b6846ee4b8e43c1661d417c88e9 (diff) | |
dashboard: access config through context
We used to have a single global `config` variable and access it
throughout the whole dashboard application.
However, this approach has been more and more complicated test writing
-- sometimes we want the config to be only slightly different, so that
it's not worth it adding new namespaces, sometimes we have to test how
dashboard handles config changes over time.
This has already led to a number of hacky contextWithXXX methods that
mocked various parts of the global variable. The rest of the code had to
sometimes still use `config` directly and sometimes invoke getXXX(c)
methods. This is very inconsistent and prone to errors.
With more and more situations where we need to patch the config
appearing (see #4118), let's refactor the application to always access
config via the getConfig(c) method. This allows us to uniformly patch
the config and be sure that the non-patched copy is not accessible from
anywhere else.
Diffstat (limited to 'dashboard/app/handler.go')
| -rw-r--r-- | dashboard/app/handler.go | 12 |
1 files changed, 6 insertions, 6 deletions
diff --git a/dashboard/app/handler.go b/dashboard/app/handler.go index 6c059c0f4..c8929290d 100644 --- a/dashboard/app/handler.go +++ b/dashboard/app/handler.go @@ -105,7 +105,7 @@ func (ce *ErrClient) HTTPStatus() int { func handleAuth(fn contextHandler) contextHandler { return func(c context.Context, w http.ResponseWriter, r *http.Request) error { - if err := checkAccessLevel(c, r, config.AccessLevel); err != nil { + if err := checkAccessLevel(c, r, getConfig(c).AccessLevel); err != nil { return err } return fn(c, w, r) @@ -148,8 +148,8 @@ func commonHeaderRaw(c context.Context, r *http.Request) *uiHeader { h := &uiHeader{ Admin: accessLevel(c, r) == AccessAdmin, URLPath: r.URL.Path, - AnalyticsTrackingID: config.AnalyticsTrackingID, - ContactEmail: config.ContactEmail, + AnalyticsTrackingID: getConfig(c).AnalyticsTrackingID, + ContactEmail: getConfig(c).ContactEmail, } if user.Current(c) == nil { h.LoginLink, _ = user.LoginURL(c, r.URL.String()) @@ -172,7 +172,7 @@ func commonHeader(c context.Context, r *http.Request, w http.ResponseWriter, ns const adminPage = "admin" isAdminPage := r.URL.Path == "/"+adminPage found := false - for ns1, cfg := range config.Namespaces { + for ns1, cfg := range getConfig(c).Namespaces { if accessLevel < cfg.AccessLevel { if ns1 == ns { return nil, ErrAccess @@ -195,8 +195,8 @@ func commonHeader(c context.Context, r *http.Request, w http.ResponseWriter, ns }) cookie := decodeCookie(r) if !found { - ns = config.DefaultNamespace - if cfg := config.Namespaces[cookie.Namespace]; cfg != nil && cfg.AccessLevel <= accessLevel { + ns = getConfig(c).DefaultNamespace + if cfg := getConfig(c).Namespaces[cookie.Namespace]; cfg != nil && cfg.AccessLevel <= accessLevel { ns = cookie.Namespace } if accessLevel == AccessAdmin { |
