refactor: Update Request struct to use NewData method for setting data

This commit is contained in:
Max 2024-06-26 18:19:51 +08:00
parent 0a5105b9d0
commit a52f0ed1a4
5 changed files with 49 additions and 29 deletions

View file

@ -164,9 +164,9 @@ func (r *Request) Render() (string, int, error) {
return "", code, err
}
data := core.Data{}
data := r.Request.NewData()
if c.Data != "" {
data, err = r.Request.ExecString(c.Data)
err = r.Request.ExecStringMerge(data, c.Data)
if err != nil {
return "", 500, fmt.Errorf("data error, please re-complie the page %s", err.Error())
}
@ -181,10 +181,9 @@ func (r *Request) Render() (string, int, error) {
}
// Set the page request data
r.Request.WithData(data)
option := core.ParserOption{
Theme: data["$theme"].(string),
Lang: data["$lang"].(string),
Theme: r.Request.Theme,
Locale: r.Request.Locale,
Debug: r.Request.DebugMode(),
Request: true,
}

View file

@ -17,7 +17,7 @@ func TestPageExec(t *testing.T) {
request := &Request{
URL: ReqeustURL{Path: "/test/path"},
Query: map[string][]string{"show": {"yes"}},
Locale: "zh-CN",
Locale: "zh-cn",
Theme: "dark",
}

View file

@ -29,12 +29,12 @@ type Mapping struct {
// ParserOption parser option
type ParserOption struct {
Editor bool `json:"editor,omitempty"`
Preview bool `json:"preview,omitempty"`
Debug bool `json:"debug,omitempty"`
Request bool `json:"request,omitempty"`
Theme string `json:"theme,omitempty"`
Lang string `json:"lang,omitempty"`
Editor bool `json:"editor,omitempty"`
Preview bool `json:"preview,omitempty"`
Debug bool `json:"debug,omitempty"`
Request bool `json:"request,omitempty"`
Theme any `json:"theme,omitempty"`
Locale any `json:"locale,omitempty"`
}
var keepWords = map[string]bool{

View file

@ -72,33 +72,54 @@ func (r *Request) DisableCache() bool {
return disable
}
// WithData set the data
func (r *Request) WithData(data Data) *Request {
// NewData create the new data
func (r *Request) NewData() Data {
cookies := r.Cookies()
theme := GetTheme(cookies)
locale := GetLocale(cookies)
r.Theme = theme
r.Locale = locale
data := Data{}
data["$payload"] = r.Payload
data["$query"] = r.Query
data["$param"] = r.Params
data["$cookie"] = cookies
data["$url"] = r.URL
data["$theme"] = GetTheme(cookies)
data["$lang"] = GetLang(cookies)
return r
data["$theme"] = r.Theme
data["$locale"] = r.Locale
return data
}
// GetLang get the lang
func GetLang(cookies map[string]string) string {
if lang, has := cookies["lang"]; has {
// GetLocale get the locale
func GetLocale(cookies map[string]string) interface{} {
if lang, has := cookies["locale"]; has {
return lang
}
return ""
return nil
}
// GetTheme get the theme
func GetTheme(cookies map[string]string) string {
func GetTheme(cookies map[string]string) interface{} {
if theme, has := cookies["color-theme"]; has {
return theme
}
return ""
return nil
}
// ExecStringMerge exec the string and merge the data
func (r *Request) ExecStringMerge(data Data, raw string) error {
res, err := r.ExecString(raw)
if err != nil {
return err
}
// Merge the data
for key, value := range res {
data[key] = value
}
return nil
}
// ExecString get the data

View file

@ -160,8 +160,8 @@ type Request struct {
Body interface{} `json:"body,omitempty"`
URL ReqeustURL `json:"url,omitempty"`
Sid string `json:"sid,omitempty"`
Theme string `json:"theme,omitempty"`
Locale string `json:"locale,omitempty"`
Theme any `json:"theme,omitempty"`
Locale any `json:"locale,omitempty"`
}
// RequestSource is the struct for the request
@ -307,21 +307,21 @@ type Matcher struct {
// DocumentDefault is the default document
var DocumentDefault = []byte(`
<!DOCTYPE html>
<html lang="{{ $REQ.locale || 'en' }}">
<html locale="{{ $locale ?? 'en-us' }}" class="{{ $theme }}" >
<head>
<meta charset="UTF-8" />
<title>{{ $DATA.head.title || '' }}</title>
<title>{{ $global.title ?? 'Untitled' }}</title>
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<meta
name="description"
content="{{ $DATA.head.description || '' }}"
content="{{ $global.description ?? '' }}"
/>
<meta
name="keywords"
content="{{ $DATA.head.keywords || '' }}"
content="{{ $global.keywords ?? '' }}"
/>
<meta name="author" content="Yao" />
<meta name="website" content="https://yaoapps.com" />