-
Notifications
You must be signed in to change notification settings - Fork 1
feat: persist user frontend theme preference #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package common | ||
|
|
||
| import ( | ||
| "strings" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| const FrontendThemeCookieName = "frontend_theme" | ||
| const FrontendThemeCookieMaxAge = 60 * 60 * 24 * 365 | ||
|
|
||
| func NormalizeFrontendTheme(theme string) string { | ||
| theme = strings.ToLower(strings.TrimSpace(theme)) | ||
| if theme == "default" || theme == "classic" { | ||
| return theme | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| func SetFrontendThemeCookie(c *gin.Context, theme string) { | ||
| theme = NormalizeFrontendTheme(theme) | ||
| if theme == "" { | ||
| return | ||
| } | ||
| c.SetCookie(FrontendThemeCookieName, theme, FrontendThemeCookieMaxAge, "/", "", false, false) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,12 +3,15 @@ package router | |
| import ( | ||
| "embed" | ||
| "net/http" | ||
| "net/url" | ||
| "strings" | ||
|
|
||
| "github.com/QuantumNous/new-api/common" | ||
| "github.com/QuantumNous/new-api/controller" | ||
| "github.com/QuantumNous/new-api/middleware" | ||
| "github.com/QuantumNous/new-api/model" | ||
| "github.com/gin-contrib/gzip" | ||
| "github.com/gin-contrib/sessions" | ||
| "github.com/gin-contrib/static" | ||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
@@ -24,23 +27,103 @@ type ThemeAssets struct { | |
| func SetWebRouter(router *gin.Engine, assets ThemeAssets) { | ||
| defaultFS := common.EmbedFolder(assets.DefaultBuildFS, "web/default/dist") | ||
| classicFS := common.EmbedFolder(assets.ClassicBuildFS, "web/classic/dist") | ||
| themeFS := common.NewThemeAwareFS(defaultFS, classicFS) | ||
|
|
||
| router.Use(gzip.Gzip(gzip.DefaultCompression)) | ||
| router.Use(middleware.GlobalWebRateLimit()) | ||
| router.Use(middleware.Cache()) | ||
| router.Use(static.Serve("/", themeFS)) | ||
| router.NoRoute(func(c *gin.Context) { | ||
| c.Set(middleware.RouteTagKey, "web") | ||
| if strings.HasPrefix(c.Request.RequestURI, "/v1") || strings.HasPrefix(c.Request.RequestURI, "/api") || strings.HasPrefix(c.Request.RequestURI, "/assets") { | ||
| if strings.HasPrefix(c.Request.RequestURI, "/v1") || strings.HasPrefix(c.Request.RequestURI, "/api") { | ||
| controller.RelayNotFound(c) | ||
| return | ||
| } | ||
|
|
||
| theme := resolveFrontendTheme(c) | ||
| if redirectPath := mapFrontendPath(theme, c.Request.URL.Path); redirectPath != "" && redirectPath != c.Request.URL.Path { | ||
| if c.Request.URL.RawQuery != "" { | ||
| redirectPath = redirectPath + "?" + c.Request.URL.RawQuery | ||
| } | ||
| c.Redirect(http.StatusFound, redirectPath) | ||
| return | ||
| } | ||
| themeFS := selectFrontendFS(theme, defaultFS, classicFS) | ||
| requestPath := strings.TrimPrefix(c.Request.URL.Path, "/") | ||
| if requestPath != "" && themeFS.Exists("/", requestPath) { | ||
| http.FileServer(themeFS).ServeHTTP(c.Writer, c.Request) | ||
| return | ||
| } | ||
|
|
||
| c.Header("Cache-Control", "no-cache") | ||
| if common.GetTheme() == "classic" { | ||
| if theme == "classic" { | ||
| c.Data(http.StatusOK, "text/html; charset=utf-8", assets.ClassicIndexPage) | ||
| } else { | ||
| c.Data(http.StatusOK, "text/html; charset=utf-8", assets.DefaultIndexPage) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| func resolveFrontendTheme(c *gin.Context) string { | ||
| session := sessions.Default(c) | ||
| if sessionID := session.Get("id"); sessionID != nil { | ||
| if userID, ok := sessionID.(int); ok && userID > 0 { | ||
| user, err := model.GetUserById(userID, false) | ||
| if err == nil { | ||
| theme := common.NormalizeFrontendTheme(user.GetSetting().FrontendTheme) | ||
| if theme != "" { | ||
| common.SetFrontendThemeCookie(c, theme) | ||
| return theme | ||
| } | ||
| } | ||
| } | ||
| } | ||
| themeCookie, err := c.Cookie(common.FrontendThemeCookieName) | ||
| if err == nil { | ||
| theme := common.NormalizeFrontendTheme(themeCookie) | ||
| if theme != "" { | ||
| return theme | ||
| } | ||
| } | ||
| return common.GetTheme() | ||
| } | ||
|
Comment on lines
+65
to
+87
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation of It is highly recommended to check the func resolveFrontendTheme(c *gin.Context) string {
if themeCookie, err := c.Cookie(common.FrontendThemeCookieName); err == nil {
if theme := common.NormalizeFrontendTheme(themeCookie); theme != "" {
return theme
}
}
session := sessions.Default(c)
if sessionID := session.Get("id"); sessionID != nil {
if userID, ok := sessionID.(int); ok && userID > 0 {
user, err := model.GetUserById(userID, false)
if err == nil {
theme := common.NormalizeFrontendTheme(user.GetSetting().FrontendTheme)
if theme != "" {
common.SetFrontendThemeCookie(c, theme)
return theme
}
}
}
}
return common.GetTheme()
} |
||
|
|
||
| func mapFrontendPath(theme string, path string) string { | ||
| normalizedPath := path | ||
| if normalizedPath == "" { | ||
| normalizedPath = "/" | ||
| } | ||
| unescapedPath, err := url.PathUnescape(normalizedPath) | ||
| if err == nil && unescapedPath != "" { | ||
| normalizedPath = unescapedPath | ||
| } | ||
| normalizedPath = strings.TrimSuffix(normalizedPath, "/") | ||
| if normalizedPath == "" { | ||
| normalizedPath = "/" | ||
| } | ||
|
|
||
| if theme == "classic" { | ||
| switch normalizedPath { | ||
| case "/dashboard": | ||
| return "/console" | ||
| case "/profile": | ||
| return "/console/personal" | ||
| } | ||
| } | ||
|
|
||
| if theme == "default" { | ||
| switch normalizedPath { | ||
| case "/console": | ||
| return "/dashboard" | ||
| case "/console/personal": | ||
| return "/profile" | ||
| } | ||
| } | ||
|
|
||
| return "" | ||
| } | ||
|
|
||
| func selectFrontendFS(theme string, defaultFS, classicFS static.ServeFileSystem) static.ServeFileSystem { | ||
| if theme == "classic" { | ||
| return classicFS | ||
| } | ||
| return defaultFS | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a
DB.Firstcall inside the generalUpdatemethod introduces an extra database query for every user update operation across the entire application. This is inefficient and usually unnecessary as GORM'sUpdatesalready modifies the fields in the local struct instance. If a specific caller needs to ensure the object is fully synced with database-side changes (like triggers or default values), they should perform the reload explicitly.