Merge pull request #627 from trheyi/main
refactor: Update Request struct to use NewData method for setting data
This commit is contained in:
commit
3a7bc271ec
5 changed files with 49 additions and 29 deletions
|
|
@ -164,9 +164,9 @@ func (r *Request) Render() (string, int, error) {
|
||||||
return "", code, err
|
return "", code, err
|
||||||
}
|
}
|
||||||
|
|
||||||
data := core.Data{}
|
data := r.Request.NewData()
|
||||||
if c.Data != "" {
|
if c.Data != "" {
|
||||||
data, err = r.Request.ExecString(c.Data)
|
err = r.Request.ExecStringMerge(data, c.Data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", 500, fmt.Errorf("data error, please re-complie the page %s", err.Error())
|
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
|
// Set the page request data
|
||||||
r.Request.WithData(data)
|
|
||||||
option := core.ParserOption{
|
option := core.ParserOption{
|
||||||
Theme: data["$theme"].(string),
|
Theme: r.Request.Theme,
|
||||||
Lang: data["$lang"].(string),
|
Locale: r.Request.Locale,
|
||||||
Debug: r.Request.DebugMode(),
|
Debug: r.Request.DebugMode(),
|
||||||
Request: true,
|
Request: true,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ func TestPageExec(t *testing.T) {
|
||||||
request := &Request{
|
request := &Request{
|
||||||
URL: ReqeustURL{Path: "/test/path"},
|
URL: ReqeustURL{Path: "/test/path"},
|
||||||
Query: map[string][]string{"show": {"yes"}},
|
Query: map[string][]string{"show": {"yes"}},
|
||||||
Locale: "zh-CN",
|
Locale: "zh-cn",
|
||||||
Theme: "dark",
|
Theme: "dark",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -33,8 +33,8 @@ type ParserOption struct {
|
||||||
Preview bool `json:"preview,omitempty"`
|
Preview bool `json:"preview,omitempty"`
|
||||||
Debug bool `json:"debug,omitempty"`
|
Debug bool `json:"debug,omitempty"`
|
||||||
Request bool `json:"request,omitempty"`
|
Request bool `json:"request,omitempty"`
|
||||||
Theme string `json:"theme,omitempty"`
|
Theme any `json:"theme,omitempty"`
|
||||||
Lang string `json:"lang,omitempty"`
|
Locale any `json:"locale,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var keepWords = map[string]bool{
|
var keepWords = map[string]bool{
|
||||||
|
|
|
||||||
|
|
@ -72,33 +72,54 @@ func (r *Request) DisableCache() bool {
|
||||||
return disable
|
return disable
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithData set the data
|
// NewData create the new data
|
||||||
func (r *Request) WithData(data Data) *Request {
|
func (r *Request) NewData() Data {
|
||||||
cookies := r.Cookies()
|
cookies := r.Cookies()
|
||||||
|
theme := GetTheme(cookies)
|
||||||
|
locale := GetLocale(cookies)
|
||||||
|
r.Theme = theme
|
||||||
|
r.Locale = locale
|
||||||
|
|
||||||
|
data := Data{}
|
||||||
data["$payload"] = r.Payload
|
data["$payload"] = r.Payload
|
||||||
data["$query"] = r.Query
|
data["$query"] = r.Query
|
||||||
data["$param"] = r.Params
|
data["$param"] = r.Params
|
||||||
data["$cookie"] = cookies
|
data["$cookie"] = cookies
|
||||||
data["$url"] = r.URL
|
data["$url"] = r.URL
|
||||||
data["$theme"] = GetTheme(cookies)
|
data["$theme"] = r.Theme
|
||||||
data["$lang"] = GetLang(cookies)
|
data["$locale"] = r.Locale
|
||||||
return r
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetLang get the lang
|
// GetLocale get the locale
|
||||||
func GetLang(cookies map[string]string) string {
|
func GetLocale(cookies map[string]string) interface{} {
|
||||||
if lang, has := cookies["lang"]; has {
|
if lang, has := cookies["locale"]; has {
|
||||||
return lang
|
return lang
|
||||||
}
|
}
|
||||||
return ""
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetTheme get the theme
|
// 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 {
|
if theme, has := cookies["color-theme"]; has {
|
||||||
return theme
|
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
|
// ExecString get the data
|
||||||
|
|
|
||||||
|
|
@ -160,8 +160,8 @@ type Request struct {
|
||||||
Body interface{} `json:"body,omitempty"`
|
Body interface{} `json:"body,omitempty"`
|
||||||
URL ReqeustURL `json:"url,omitempty"`
|
URL ReqeustURL `json:"url,omitempty"`
|
||||||
Sid string `json:"sid,omitempty"`
|
Sid string `json:"sid,omitempty"`
|
||||||
Theme string `json:"theme,omitempty"`
|
Theme any `json:"theme,omitempty"`
|
||||||
Locale string `json:"locale,omitempty"`
|
Locale any `json:"locale,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// RequestSource is the struct for the request
|
// RequestSource is the struct for the request
|
||||||
|
|
@ -307,21 +307,21 @@ type Matcher struct {
|
||||||
// DocumentDefault is the default document
|
// DocumentDefault is the default document
|
||||||
var DocumentDefault = []byte(`
|
var DocumentDefault = []byte(`
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="{{ $REQ.locale || 'en' }}">
|
<html locale="{{ $locale ?? 'en-us' }}" class="{{ $theme }}" >
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8" />
|
||||||
<title>{{ $DATA.head.title || '' }}</title>
|
<title>{{ $global.title ?? 'Untitled' }}</title>
|
||||||
<meta
|
<meta
|
||||||
name="viewport"
|
name="viewport"
|
||||||
content="width=device-width, initial-scale=1, shrink-to-fit=no"
|
content="width=device-width, initial-scale=1, shrink-to-fit=no"
|
||||||
/>
|
/>
|
||||||
<meta
|
<meta
|
||||||
name="description"
|
name="description"
|
||||||
content="{{ $DATA.head.description || '' }}"
|
content="{{ $global.description ?? '' }}"
|
||||||
/>
|
/>
|
||||||
<meta
|
<meta
|
||||||
name="keywords"
|
name="keywords"
|
||||||
content="{{ $DATA.head.keywords || '' }}"
|
content="{{ $global.keywords ?? '' }}"
|
||||||
/>
|
/>
|
||||||
<meta name="author" content="Yao" />
|
<meta name="author" content="Yao" />
|
||||||
<meta name="website" content="https://yaoapps.com" />
|
<meta name="website" content="https://yaoapps.com" />
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue