@@ -55,7 +55,7 @@ type Render interface {
5555type Context struct {
5656 Resp ResponseWriter
5757 Req * http.Request
58- Data map [ string ] interface {} // data used by MVC templates
58+ Data middleware. ContextData // data used by MVC templates
5959 PageData map [string ]interface {} // data used by JavaScript modules in one page, it's `window.config.pageData`
6060 Render Render
6161 translation.Locale
@@ -97,7 +97,7 @@ func (ctx *Context) TrHTMLEscapeArgs(msg string, args ...string) string {
9797}
9898
9999// GetData returns the data
100- func (ctx * Context ) GetData () map [ string ] interface {} {
100+ func (ctx * Context ) GetData () middleware. ContextData {
101101 return ctx .Data
102102}
103103
@@ -219,20 +219,27 @@ const tplStatus500 base.TplName = "status/500"
219219// HTML calls Context.HTML and renders the template to HTTP response
220220func (ctx * Context ) HTML (status int , name base.TplName ) {
221221 log .Debug ("Template: %s" , name )
222+
222223 tmplStartTime := time .Now ()
223224 if ! setting .IsProd {
224225 ctx .Data ["TemplateName" ] = name
225226 }
226227 ctx .Data ["TemplateLoadTimes" ] = func () string {
227228 return strconv .FormatInt (time .Since (tmplStartTime ).Nanoseconds ()/ 1e6 , 10 ) + "ms"
228229 }
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 {
234238 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
236243 }
237244}
238245
@@ -676,42 +683,38 @@ func getCsrfOpts() CsrfOptions {
676683}
677684
678685// 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 {
680687 rnd := templates .HTMLRenderer ()
681688 csrfOpts := getCsrfOpts ()
682689 if ! setting .IsProd {
683690 CsrfTokenRegenerationInterval = 5 * time .Second // in dev, re-generate the tokens more aggressively for debug purpose
684691 }
685692 return func (next http.Handler ) http.Handler {
686693 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-
691694 ctx := Context {
692695 Resp : NewResponse (resp ),
693696 Cache : mc .GetCache (),
694- Locale : locale ,
695- Link : link ,
697+ Locale : middleware . Locale ( resp , req ) ,
698+ Link : setting . AppSubURL + strings . TrimSuffix ( req . URL . EscapedPath (), "/" ) ,
696699 Render : rnd ,
697700 Session : session .GetSession (req ),
698701 Repo : & Repository {
699702 PullRequest : & PullRequest {},
700703 },
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 ()),
708706 }
709707 defer ctx .Close ()
710708
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+
711715 // 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 {}
713717 ctx .Data ["PageData" ] = ctx .PageData
714- ctx .Data ["Context" ] = & ctx
715718
716719 ctx .Req = WithContext (req , & ctx )
717720 ctx .Csrf = PrepareCSRFProtector (csrfOpts , & ctx )
@@ -755,16 +758,6 @@ func Contexter(ctx context.Context) func(next http.Handler) http.Handler {
755758 ctx .Data ["CsrfTokenHtml" ] = template .HTML (`<input type="hidden" name="_csrf" value="` + ctx .Data ["CsrfToken" ].(string ) + `">` )
756759
757760 // 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
768761 ctx .Data ["DisableMigrations" ] = setting .Repository .DisableMigrations
769762 ctx .Data ["DisableStars" ] = setting .Repository .DisableStars
770763 ctx .Data ["EnableActions" ] = setting .Actions .Enabled
@@ -777,21 +770,9 @@ func Contexter(ctx context.Context) func(next http.Handler) http.Handler {
777770 ctx .Data ["UnitProjectsGlobalDisabled" ] = unit .TypeProjects .UnitGlobalDisabled ()
778771 ctx .Data ["UnitActionsGlobalDisabled" ] = unit .TypeActions .UnitGlobalDisabled ()
779772
780- ctx .Data ["locale" ] = locale
781773 ctx .Data ["AllLangs" ] = translation .AllLangs ()
782774
783775 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- }
795776 })
796777 }
797778}
0 commit comments