@@ -55,7 +55,7 @@ type Render interface {
55
55
type Context struct {
56
56
Resp ResponseWriter
57
57
Req * http.Request
58
- Data map [ string ] interface {} // data used by MVC templates
58
+ Data middleware. ContextData // data used by MVC templates
59
59
PageData map [string ]interface {} // data used by JavaScript modules in one page, it's `window.config.pageData`
60
60
Render Render
61
61
translation.Locale
@@ -97,7 +97,7 @@ func (ctx *Context) TrHTMLEscapeArgs(msg string, args ...string) string {
97
97
}
98
98
99
99
// GetData returns the data
100
- func (ctx * Context ) GetData () map [ string ] interface {} {
100
+ func (ctx * Context ) GetData () middleware. ContextData {
101
101
return ctx .Data
102
102
}
103
103
@@ -219,20 +219,27 @@ const tplStatus500 base.TplName = "status/500"
219
219
// HTML calls Context.HTML and renders the template to HTTP response
220
220
func (ctx * Context ) HTML (status int , name base.TplName ) {
221
221
log .Debug ("Template: %s" , name )
222
+
222
223
tmplStartTime := time .Now ()
223
224
if ! setting .IsProd {
224
225
ctx .Data ["TemplateName" ] = name
225
226
}
226
227
ctx .Data ["TemplateLoadTimes" ] = func () string {
227
228
return strconv .FormatInt (time .Since (tmplStartTime ).Nanoseconds ()/ 1e6 , 10 ) + "ms"
228
229
}
229
- if err := ctx .Render .HTML (ctx .Resp , status , string (name ), templates .BaseVars ().Merge (ctx .Data )); err != nil {
230
- if status == http .StatusInternalServerError && name == tplStatus500 {
231
- ctx .PlainText (http .StatusInternalServerError , "Unable to find HTML templates, the template system is not initialized, or Gitea can't find your template files." )
232
- return
233
- }
230
+
231
+ err := ctx .Render .HTML (ctx .Resp , status , string (name ), ctx .Data )
232
+ if err == nil {
233
+ return
234
+ }
235
+
236
+ // if rendering fails, show error page
237
+ if name != tplStatus500 {
234
238
err = fmt .Errorf ("failed to render template: %s, error: %s" , name , templates .HandleTemplateRenderingError (err ))
235
- ctx .ServerError ("Render failed" , err )
239
+ ctx .ServerError ("Render failed" , err ) // show the 500 error page
240
+ } else {
241
+ ctx .PlainText (http .StatusInternalServerError , "Unable to render status/500 page, the template system is broken, or Gitea can't find your template files." )
242
+ return
236
243
}
237
244
}
238
245
@@ -676,42 +683,38 @@ func getCsrfOpts() CsrfOptions {
676
683
}
677
684
678
685
// Contexter initializes a classic context for a request.
679
- func Contexter (ctx context. Context ) func (next http.Handler ) http.Handler {
686
+ func Contexter () func (next http.Handler ) http.Handler {
680
687
rnd := templates .HTMLRenderer ()
681
688
csrfOpts := getCsrfOpts ()
682
689
if ! setting .IsProd {
683
690
CsrfTokenRegenerationInterval = 5 * time .Second // in dev, re-generate the tokens more aggressively for debug purpose
684
691
}
685
692
return func (next http.Handler ) http.Handler {
686
693
return http .HandlerFunc (func (resp http.ResponseWriter , req * http.Request ) {
687
- locale := middleware .Locale (resp , req )
688
- startTime := time .Now ()
689
- link := setting .AppSubURL + strings .TrimSuffix (req .URL .EscapedPath (), "/" )
690
-
691
694
ctx := Context {
692
695
Resp : NewResponse (resp ),
693
696
Cache : mc .GetCache (),
694
- Locale : locale ,
695
- Link : link ,
697
+ Locale : middleware . Locale ( resp , req ) ,
698
+ Link : setting . AppSubURL + strings . TrimSuffix ( req . URL . EscapedPath (), "/" ) ,
696
699
Render : rnd ,
697
700
Session : session .GetSession (req ),
698
701
Repo : & Repository {
699
702
PullRequest : & PullRequest {},
700
703
},
701
- Org : & Organization {},
702
- Data : map [string ]interface {}{
703
- "CurrentURL" : setting .AppSubURL + req .URL .RequestURI (),
704
- "PageStartTime" : startTime ,
705
- "Link" : link ,
706
- "RunModeIsProd" : setting .IsProd ,
707
- },
704
+ Org : & Organization {},
705
+ Data : middleware .GetContextData (req .Context ()),
708
706
}
709
707
defer ctx .Close ()
710
708
709
+ ctx .Data .MergeFrom (middleware .CommonTemplateContextData ())
710
+ ctx .Data ["Context" ] = & ctx
711
+ ctx .Data ["CurrentURL" ] = setting .AppSubURL + req .URL .RequestURI ()
712
+ ctx .Data ["Link" ] = ctx .Link
713
+ ctx .Data ["locale" ] = ctx .Locale
714
+
711
715
// PageData is passed by reference, and it will be rendered to `window.config.pageData` in `head.tmpl` for JavaScript modules
712
- ctx .PageData = map [string ]interface {} {}
716
+ ctx .PageData = map [string ]any {}
713
717
ctx .Data ["PageData" ] = ctx .PageData
714
- ctx .Data ["Context" ] = & ctx
715
718
716
719
ctx .Req = WithContext (req , & ctx )
717
720
ctx .Csrf = PrepareCSRFProtector (csrfOpts , & ctx )
@@ -755,16 +758,6 @@ func Contexter(ctx context.Context) func(next http.Handler) http.Handler {
755
758
ctx .Data ["CsrfTokenHtml" ] = template .HTML (`<input type="hidden" name="_csrf" value="` + ctx .Data ["CsrfToken" ].(string ) + `">` )
756
759
757
760
// FIXME: do we really always need these setting? There should be someway to have to avoid having to always set these
758
- ctx .Data ["IsLandingPageHome" ] = setting .LandingPageURL == setting .LandingPageHome
759
- ctx .Data ["IsLandingPageExplore" ] = setting .LandingPageURL == setting .LandingPageExplore
760
- ctx .Data ["IsLandingPageOrganizations" ] = setting .LandingPageURL == setting .LandingPageOrganizations
761
-
762
- ctx .Data ["ShowRegistrationButton" ] = setting .Service .ShowRegistrationButton
763
- ctx .Data ["ShowMilestonesDashboardPage" ] = setting .Service .ShowMilestonesDashboardPage
764
- ctx .Data ["ShowFooterVersion" ] = setting .Other .ShowFooterVersion
765
-
766
- ctx .Data ["EnableSwagger" ] = setting .API .EnableSwagger
767
- ctx .Data ["EnableOpenIDSignIn" ] = setting .Service .EnableOpenIDSignIn
768
761
ctx .Data ["DisableMigrations" ] = setting .Repository .DisableMigrations
769
762
ctx .Data ["DisableStars" ] = setting .Repository .DisableStars
770
763
ctx .Data ["EnableActions" ] = setting .Actions .Enabled
@@ -777,21 +770,9 @@ func Contexter(ctx context.Context) func(next http.Handler) http.Handler {
777
770
ctx .Data ["UnitProjectsGlobalDisabled" ] = unit .TypeProjects .UnitGlobalDisabled ()
778
771
ctx .Data ["UnitActionsGlobalDisabled" ] = unit .TypeActions .UnitGlobalDisabled ()
779
772
780
- ctx .Data ["locale" ] = locale
781
773
ctx .Data ["AllLangs" ] = translation .AllLangs ()
782
774
783
775
next .ServeHTTP (ctx .Resp , ctx .Req )
784
-
785
- // Handle adding signedUserName to the context for the AccessLogger
786
- usernameInterface := ctx .Data ["SignedUserName" ]
787
- identityPtrInterface := ctx .Req .Context ().Value (signedUserNameStringPointerKey )
788
- if usernameInterface != nil && identityPtrInterface != nil {
789
- username := usernameInterface .(string )
790
- identityPtr := identityPtrInterface .(* string )
791
- if identityPtr != nil && username != "" {
792
- * identityPtr = username
793
- }
794
- }
795
776
})
796
777
}
797
778
}
0 commit comments