From 2c1ff8701a7aeafc86f87c286514768cdf121ccb Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 25 Jan 2025 22:36:47 +0800 Subject: Refactor context flash msg and global variables (#33375) 1. add `GetSiteCookieFlashMessage` to help to parse flash message 2. clarify `handleRepoHomeFeed` logic 3. remove unnecessary global variables, use `sync.OnceValue` instead 4. add some tests for `IsUsableUsername` and `IsUsableRepoName` --- services/context/context.go | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) (limited to 'services/context') diff --git a/services/context/context.go b/services/context/context.go index 6715c5663d..5b16f9be98 100644 --- a/services/context/context.go +++ b/services/context/context.go @@ -165,18 +165,10 @@ func Contexter() func(next http.Handler) http.Handler { ctx.Base.SetContextValue(WebContextKey, ctx) ctx.Csrf = NewCSRFProtector(csrfOpts) - // Get the last flash message from cookie - lastFlashCookie := middleware.GetSiteCookie(ctx.Req, CookieNameFlash) + // get the last flash message from cookie + lastFlashCookie, lastFlashMsg := middleware.GetSiteCookieFlashMessage(ctx, ctx.Req, CookieNameFlash) if vals, _ := url.ParseQuery(lastFlashCookie); len(vals) > 0 { - // store last Flash message into the template data, to render it - ctx.Data["Flash"] = &middleware.Flash{ - DataStore: ctx, - Values: vals, - ErrorMsg: vals.Get("error"), - SuccessMsg: vals.Get("success"), - InfoMsg: vals.Get("info"), - WarningMsg: vals.Get("warning"), - } + ctx.Data["Flash"] = lastFlashMsg // store last Flash message into the template data, to render it } // if there are new messages in the ctx.Flash, write them into cookie -- cgit v1.2.3