1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
| package cutil
import ( "context" "slices" "strings"
"github.com/mileusna/useragent" "github.com/valyala/fasthttp"
"{{{ .Package }}}/app" "{{{ .Package }}}/app/controller/csession" "{{{ .Package }}}/app/lib/telemetry" "{{{ .Package }}}/app/lib/telemetry/httpmetrics" "{{{ .Package }}}/app/lib/user"{{{ if .DatabaseUISaveUser }}} usr "{{{ .Package }}}/app/user"{{{ end }}} "{{{ .Package }}}/app/util" )
var initialIcons = {{{ if .HasModule "search" }}}[]string{"searchbox"}{{{ else }}}[]string{}{{{ end }}}
func LoadPageState(as *app.State, rc *fasthttp.RequestCtx, key string, logger util.Logger) *PageState { parentCtx, logger := httpmetrics.ExtractHeaders(rc, logger) ctx, span, logger := telemetry.StartSpan(parentCtx, "http:"+key, logger) span.Attribute("path", string(rc.Request.URI().Path())) if !telemetry.SkipControllerMetrics { httpmetrics.InjectHTTP(rc, span) } session, flashes, prof{{{ if .HasAccount }}}, accts{{{ end }}} := loadSession(ctx, as, rc, logger) params := ParamSetFromRequest(rc) ua := useragent.Parse(string(rc.Request.Header.Peek("User-Agent"))) os := strings.ToLower(ua.OS) browser := strings.ToLower(ua.Name) platform := "unknown" switch { case ua.Desktop: platform = "desktop" case ua.Tablet: platform = "tablet" case ua.Mobile: platform = "mobile" case ua.Bot: platform = "bot" } span.Attribute("browser", browser) span.Attribute("os", os){{{ if .HasAccount }}}
isAuthed, _ := user.Check("/", accts) isAdmin, _ := user.Check("/admin", accts){{{ end }}}{{{ if .HasUser }}}
u, _ := as.User(ctx, prof.ID, logger){{{ end }}}
return &PageState{ Action: key, Method: string(rc.Method()), URI: rc.Request.URI(), Flashes: flashes, Session: session, OS: os, OSVersion: ua.OSVersion, Browser: browser, BrowserVersion: ua.Version, Platform: platform, {{{ if .HasUser }}}User: u, {{{ end }}}Profile: prof, {{{ if .HasAccount }}}Accounts: accts, Authed: isAuthed, Admin: isAdmin, {{{ end }}}Params: params, Icons: slices.Clone(initialIcons), Started: util.TimeCurrent(), Logger: logger, Context: ctx, Span: span, } }
func loadSession({{{ if .DatabaseUISaveUser }}}ctx{{{ else }}}_{{{ end }}} context.Context, {{{ if .DatabaseUISaveUser }}}as{{{ else }}}_{{{ end }}} *app.State, rc *fasthttp.RequestCtx, logger util.Logger) (util.ValueMap, []string, *user.Profile{{{ if .HasAccount }}}, user.Accounts{{{ end }}}) { sessionBytes := rc.Request.Header.Cookie(util.AppKey) session := util.ValueMap{} if len(sessionBytes) > 0 { dec, err := util.DecryptMessage(nil, string(sessionBytes), logger) if err != nil { logger.Warnf("error decrypting session: %+v", err) } err = util.FromJSON([]byte(dec), &session) if err != nil { session = util.ValueMap{} } }
flashes := util.StringSplitAndTrim(session.GetStringOpt(csession.WebFlashKey), ";") if len(flashes) > 0 { delete(session, csession.WebFlashKey) err := csession.SaveSession(rc, session, logger) if err != nil { logger.Warnf("can't save session: %+v", err) } }
prof, err := loadProfile(session) if err != nil { logger.Warnf("can't load profile: %+v", err) }{{{ if .HasAccount }}}
var accts user.Accounts authX, ok := session[csession.WebAuthKey] if ok { authS, ok := authX.(string) if ok { accts = user.AccountsFromString(authS) } }{{{ end }}}{{{ if .HasUser }}}
if prof.ID == util.UUIDDefault { prof.ID = util.UUID(){{{ if .DatabaseUISaveUser }}} u := &usr.User{ID: prof.ID, Name: prof.Name{{{ if .HasAccount }}}, Picture: accts.Image(){{{ end }}}, Created: util.TimeCurrent()} err = as.Services.User.Save(ctx, nil, logger, u) if err != nil { logger.Warnf("unable to save user [%s]", prof.ID.String()) return nil, nil, prof, nil }{{{ end }}} session["profile"] = prof err = csession.SaveSession(rc, session, logger) if err != nil { logger.Warnf("unable to save session for user [%s]", prof.ID.String()) return nil, nil, prof{{{ if .HasAccount }}}, nil{{{ end }}} } }{{{ end }}}
return session, flashes, prof{{{ if .HasAccount }}}, accts{{{ end }}} }
func loadProfile(session util.ValueMap) (*user.Profile, error) { x, ok := session["profile"] if !ok { return user.DefaultProfile.Clone(), nil } s, ok := x.(string) if !ok { m, ok := x.(map[string]any) if !ok { return user.DefaultProfile.Clone(), nil } s = util.ToJSON(m) } p := &user.Profile{} err := util.FromJSON([]byte(s), p) if err != nil { return nil, err } if p.Name == "" { p.Name = user.DefaultProfile.Name } return p, nil }
|