summaryrefslogtreecommitdiffstats
path: root/modules/context
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2023-08-25 13:07:42 +0200
committerGitHub <noreply@github.com>2023-08-25 13:07:42 +0200
commit412e5c0946fc5b83456685bc2fb1aed682228f57 (patch)
tree1dc1a823618d554f740cff449e20c1538ad95ba3 /modules/context
parentRemove incorrect CSS helper classes (#26712) (diff)
downloadforgejo-412e5c0946fc5b83456685bc2fb1aed682228f57.tar.xz
forgejo-412e5c0946fc5b83456685bc2fb1aed682228f57.zip
Make web context initialize correctly for different cases (#26726)
The web context (modules/context.Context) is quite complex, it's difficult for the callers to initialize correctly. This PR introduces a `NewWebContext` function, to make sure the web context have the same behavior for different cases.
Diffstat (limited to 'modules/context')
-rw-r--r--modules/context/context.go41
-rw-r--r--modules/context/package.go6
2 files changed, 27 insertions, 20 deletions
diff --git a/modules/context/context.go b/modules/context/context.go
index 98c1a9bdb6..47ad310b09 100644
--- a/modules/context/context.go
+++ b/modules/context/context.go
@@ -107,6 +107,29 @@ func GetValidateContext(req *http.Request) (ctx *ValidateContext) {
return ctx
}
+func NewTemplateContextForWeb(ctx *Context) TemplateContext {
+ tmplCtx := NewTemplateContext(ctx)
+ tmplCtx["Locale"] = ctx.Base.Locale
+ tmplCtx["AvatarUtils"] = templates.NewAvatarUtils(ctx)
+ return tmplCtx
+}
+
+func NewWebContext(base *Base, render Render, session session.Store) *Context {
+ ctx := &Context{
+ Base: base,
+ Render: render,
+ Session: session,
+
+ Cache: mc.GetCache(),
+ Link: setting.AppSubURL + strings.TrimSuffix(base.Req.URL.EscapedPath(), "/"),
+ Repo: &Repository{PullRequest: &PullRequest{}},
+ Org: &Organization{},
+ }
+ ctx.TemplateContext = NewTemplateContextForWeb(ctx)
+ ctx.Flash = &middleware.Flash{DataStore: ctx, Values: url.Values{}}
+ return ctx
+}
+
// Contexter initializes a classic context for a request.
func Contexter() func(next http.Handler) http.Handler {
rnd := templates.HTMLRenderer()
@@ -127,21 +150,8 @@ func Contexter() func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
base, baseCleanUp := NewBaseContext(resp, req)
- ctx := &Context{
- Base: base,
- Cache: mc.GetCache(),
- Link: setting.AppSubURL + strings.TrimSuffix(req.URL.EscapedPath(), "/"),
- Render: rnd,
- Session: session.GetSession(req),
- Repo: &Repository{PullRequest: &PullRequest{}},
- Org: &Organization{},
- }
defer baseCleanUp()
-
- // TODO: "install.go" also shares the same logic, which should be refactored to a general function
- ctx.TemplateContext = NewTemplateContext(ctx)
- ctx.TemplateContext["Locale"] = ctx.Locale
- ctx.TemplateContext["AvatarUtils"] = templates.NewAvatarUtils(ctx)
+ ctx := NewWebContext(base, rnd, session.GetSession(req))
ctx.Data.MergeFrom(middleware.CommonTemplateContextData())
ctx.Data["Context"] = ctx // TODO: use "ctx" in template and remove this
@@ -172,8 +182,7 @@ func Contexter() func(next http.Handler) http.Handler {
}
}
- // prepare an empty Flash message for current request
- ctx.Flash = &middleware.Flash{DataStore: ctx, Values: url.Values{}}
+ // if there are new messages in the ctx.Flash, write them into cookie
ctx.Resp.Before(func(resp ResponseWriter) {
if val := ctx.Flash.Encode(); val != "" {
middleware.SetSiteCookie(ctx.Resp, CookieNameFlash, val, 0)
diff --git a/modules/context/package.go b/modules/context/package.go
index be50e0a991..c0813fb2da 100644
--- a/modules/context/package.go
+++ b/modules/context/package.go
@@ -154,12 +154,10 @@ func PackageContexter() func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
base, baseCleanUp := NewBaseContext(resp, req)
- ctx := &Context{
- Base: base,
- Render: renderer, // it is still needed when rendering 500 page in a package handler
- }
defer baseCleanUp()
+ // it is still needed when rendering 500 page in a package handler
+ ctx := NewWebContext(base, renderer, nil)
ctx.Base.AppendContextValue(WebContextKey, ctx)
next.ServeHTTP(ctx.Resp, ctx.Req)
})