@@ -107,6 +107,29 @@ func GetValidateContext(req *http.Request) (ctx *ValidateContext) {
107
107
return ctx
108
108
}
109
109
110
+ func NewTemplateContextForWeb (ctx * Context ) TemplateContext {
111
+ tmplCtx := NewTemplateContext (ctx )
112
+ tmplCtx ["Locale" ] = ctx .Base .Locale
113
+ tmplCtx ["AvatarUtils" ] = templates .NewAvatarUtils (ctx )
114
+ return tmplCtx
115
+ }
116
+
117
+ func NewWebContext (base * Base , render Render , session session.Store ) * Context {
118
+ ctx := & Context {
119
+ Base : base ,
120
+ Render : render ,
121
+ Session : session ,
122
+
123
+ Cache : mc .GetCache (),
124
+ Link : setting .AppSubURL + strings .TrimSuffix (base .Req .URL .EscapedPath (), "/" ),
125
+ Repo : & Repository {PullRequest : & PullRequest {}},
126
+ Org : & Organization {},
127
+ }
128
+ ctx .TemplateContext = NewTemplateContextForWeb (ctx )
129
+ ctx .Flash = & middleware.Flash {DataStore : ctx , Values : url.Values {}}
130
+ return ctx
131
+ }
132
+
110
133
// Contexter initializes a classic context for a request.
111
134
func Contexter () func (next http.Handler ) http.Handler {
112
135
rnd := templates .HTMLRenderer ()
@@ -127,21 +150,8 @@ func Contexter() func(next http.Handler) http.Handler {
127
150
return func (next http.Handler ) http.Handler {
128
151
return http .HandlerFunc (func (resp http.ResponseWriter , req * http.Request ) {
129
152
base , baseCleanUp := NewBaseContext (resp , req )
130
- ctx := & Context {
131
- Base : base ,
132
- Cache : mc .GetCache (),
133
- Link : setting .AppSubURL + strings .TrimSuffix (req .URL .EscapedPath (), "/" ),
134
- Render : rnd ,
135
- Session : session .GetSession (req ),
136
- Repo : & Repository {PullRequest : & PullRequest {}},
137
- Org : & Organization {},
138
- }
139
153
defer baseCleanUp ()
140
-
141
- // TODO: "install.go" also shares the same logic, which should be refactored to a general function
142
- ctx .TemplateContext = NewTemplateContext (ctx )
143
- ctx .TemplateContext ["Locale" ] = ctx .Locale
144
- ctx .TemplateContext ["AvatarUtils" ] = templates .NewAvatarUtils (ctx )
154
+ ctx := NewWebContext (base , rnd , session .GetSession (req ))
145
155
146
156
ctx .Data .MergeFrom (middleware .CommonTemplateContextData ())
147
157
ctx .Data ["Context" ] = ctx // TODO: use "ctx" in template and remove this
@@ -172,8 +182,7 @@ func Contexter() func(next http.Handler) http.Handler {
172
182
}
173
183
}
174
184
175
- // prepare an empty Flash message for current request
176
- ctx .Flash = & middleware.Flash {DataStore : ctx , Values : url.Values {}}
185
+ // if there are new messages in the ctx.Flash, write them into cookie
177
186
ctx .Resp .Before (func (resp ResponseWriter ) {
178
187
if val := ctx .Flash .Encode (); val != "" {
179
188
middleware .SetSiteCookie (ctx .Resp , CookieNameFlash , val , 0 )
0 commit comments