Merge pull request #584 from trheyi/main

Refactor build process and add data parameter to BuildOption
This commit is contained in:
Max 2024-02-28 21:29:27 +08:00 committed by GitHub
commit 6e4578825f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 42 additions and 21 deletions

View file

@ -914,12 +914,17 @@ func BuildAll(process *process.Process) interface{} {
assetRoot = v assetRoot = v
} }
data := map[string]interface{}{}
if v, ok := option["data"].(map[string]interface{}); ok {
data = v
}
tmpl, err := sui.GetTemplate(templateID) tmpl, err := sui.GetTemplate(templateID)
if err != nil { if err != nil {
exception.New(err.Error(), 500).Throw() exception.New(err.Error(), 500).Throw()
} }
err = tmpl.Build(&core.BuildOption{SSR: ssr, AssetRoot: assetRoot}) err = tmpl.Build(&core.BuildOption{SSR: ssr, AssetRoot: assetRoot, Data: data})
if err != nil { if err != nil {
exception.New(err.Error(), 500).Throw() exception.New(err.Error(), 500).Throw()
} }
@ -959,7 +964,8 @@ func BuildPage(process *process.Process) interface{} {
exception.New(err.Error(), 500).Throw() exception.New(err.Error(), 500).Throw()
} }
err = page.Build(&core.BuildOption{SSR: ssr, AssetRoot: assetRoot}) data := process.ArgsMap(5, map[string]interface{}{})
err = page.Build(&core.BuildOption{SSR: ssr, AssetRoot: assetRoot, Data: data})
if err != nil { if err != nil {
exception.New(err.Error(), 500).Throw() exception.New(err.Error(), 500).Throw()
} }

View file

@ -73,18 +73,32 @@ func (sui *DSL) PublicRootWithSid(sid string) (string, error) {
} }
// PublicRoot returns the public root path // PublicRoot returns the public root path
func (sui *DSL) PublicRoot() (string, error) { func (sui *DSL) PublicRoot(data map[string]interface{}) (string, error) {
// Cache the public root // Cache the public root
if sui.publicRoot != "" { if sui.publicRoot != "" {
return sui.publicRoot, nil return sui.publicRoot, nil
} }
if data == nil {
data = map[string]interface{}{}
}
ss := session.Global().ID(sui.Sid) ss := session.Global().ID(sui.Sid)
data, err := ss.Dump() sessionData, err := ss.Dump()
if err != nil { if err != nil {
return "", err return "", err
} }
// Merge the session data
if sessionData == nil {
sessionData = map[string]interface{}{}
}
// Merge the data
for k, v := range sessionData {
data[k] = v
}
vars := map[string]interface{}{"$session": data} vars := map[string]interface{}{"$session": data}
var root = sui.Public.Root var root = sui.Public.Root
dot := maps.Of(vars).Dot() dot := maps.Of(vars).Dot()

View file

@ -136,15 +136,16 @@ type MediaSearchResult struct {
// BuildOption is the struct for the option option // BuildOption is the struct for the option option
type BuildOption struct { type BuildOption struct {
SSR bool `json:"ssr"` SSR bool `json:"ssr"`
CDN bool `json:"cdn"` CDN bool `json:"cdn"`
UpdateAll bool `json:"update_all"` UpdateAll bool `json:"update_all"`
AssetRoot string `json:"asset_root,omitempty"` AssetRoot string `json:"asset_root,omitempty"`
IgnoreAssetRoot bool `json:"ignore_asset_root,omitempty"` IgnoreAssetRoot bool `json:"ignore_asset_root,omitempty"`
IgnoreDocument bool `json:"ignore_document,omitempty"` IgnoreDocument bool `json:"ignore_document,omitempty"`
WithWrapper bool `json:"with_wrapper,omitempty"` WithWrapper bool `json:"with_wrapper,omitempty"`
KeepPageTag bool `json:"keep_page_tag,omitempty"` KeepPageTag bool `json:"keep_page_tag,omitempty"`
Namespace string `json:"namespace,omitempty"` Namespace string `json:"namespace,omitempty"`
Data map[string]interface{} `json:"data,omitempty"`
} }
// Request is the struct for the request // Request is the struct for the request

View file

@ -15,7 +15,7 @@ import (
func (tmpl *Template) Build(option *core.BuildOption) error { func (tmpl *Template) Build(option *core.BuildOption) error {
var err error var err error
root, err := tmpl.local.DSL.PublicRoot() root, err := tmpl.local.DSL.PublicRoot(option.Data)
if err != nil { if err != nil {
log.Error("SyncAssets: Get the public root error: %s. use %s", err.Error(), tmpl.local.DSL.Public.Root) log.Error("SyncAssets: Get the public root error: %s. use %s", err.Error(), tmpl.local.DSL.Public.Root)
root = tmpl.local.DSL.Public.Root root = tmpl.local.DSL.Public.Root
@ -62,7 +62,7 @@ func (tmpl *Template) SyncAssets(option *core.BuildOption) error {
} }
//get target abs path //get target abs path
root, err := tmpl.local.DSL.PublicRoot() root, err := tmpl.local.DSL.PublicRoot(option.Data)
if err != nil { if err != nil {
log.Error("SyncAssets: Get the public root error: %s. use %s", err.Error(), tmpl.local.DSL.Public.Root) log.Error("SyncAssets: Get the public root error: %s. use %s", err.Error(), tmpl.local.DSL.Public.Root)
root = tmpl.local.DSL.Public.Root root = tmpl.local.DSL.Public.Root
@ -81,7 +81,7 @@ func (tmpl *Template) SyncAssets(option *core.BuildOption) error {
func (page *Page) Build(option *core.BuildOption) error { func (page *Page) Build(option *core.BuildOption) error {
if option.AssetRoot == "" { if option.AssetRoot == "" {
root, err := page.tmpl.local.DSL.PublicRoot() root, err := page.tmpl.local.DSL.PublicRoot(option.Data)
if err != nil { if err != nil {
log.Error("SyncAssets: Get the public root error: %s. use %s", err.Error(), page.tmpl.local.DSL.Public.Root) log.Error("SyncAssets: Get the public root error: %s. use %s", err.Error(), page.tmpl.local.DSL.Public.Root)
root = page.tmpl.local.DSL.Public.Root root = page.tmpl.local.DSL.Public.Root
@ -96,11 +96,11 @@ func (page *Page) Build(option *core.BuildOption) error {
} }
// Save the html // Save the html
return page.writeHTML([]byte(html)) return page.writeHTML([]byte(html), option.Data)
} }
func (page *Page) publicFile() string { func (page *Page) publicFile(data map[string]interface{}) string {
root, err := page.tmpl.local.DSL.PublicRoot() root, err := page.tmpl.local.DSL.PublicRoot(data)
if err != nil { if err != nil {
log.Error("publicFile: Get the public root error: %s. use %s", err.Error(), page.tmpl.local.DSL.Public.Root) log.Error("publicFile: Get the public root error: %s. use %s", err.Error(), page.tmpl.local.DSL.Public.Root)
root = page.tmpl.local.DSL.Public.Root root = page.tmpl.local.DSL.Public.Root
@ -109,8 +109,8 @@ func (page *Page) publicFile() string {
} }
// writeHTMLTo write the html to file // writeHTMLTo write the html to file
func (page *Page) writeHTML(html []byte) error { func (page *Page) writeHTML(html []byte, data map[string]interface{}) error {
htmlFile := fmt.Sprintf("%s.sui", page.publicFile()) htmlFile := fmt.Sprintf("%s.sui", page.publicFile(data))
htmlFileAbs := filepath.Join(application.App.Root(), htmlFile) htmlFileAbs := filepath.Join(application.App.Root(), htmlFile)
dir := filepath.Dir(htmlFileAbs) dir := filepath.Dir(htmlFileAbs)
if exist, _ := os.Stat(dir); exist == nil { if exist, _ := os.Stat(dir); exist == nil {