[add] page build api
This commit is contained in:
parent
9a088ce461
commit
dd4e72a557
4 changed files with 144 additions and 16 deletions
|
|
@ -162,8 +162,22 @@ var dsl = []byte(`
|
|||
"path": "/:id/preview/:template_id/*route",
|
||||
"method": "GET",
|
||||
"process": "sui.Preview.Render",
|
||||
"in": ["$param.id", "$param.template_id", "$param.path", "$header.Referer", "$query.r", "$query.t"],
|
||||
"in": ["$param.id", "$param.template_id", "$param.route", "$header.Referer", "$query.r", "$query.t"],
|
||||
"out": {"status": 200, "type": "text/html; charset=utf-8"}
|
||||
},
|
||||
|
||||
{
|
||||
"path": "/:id/build/:template_id",
|
||||
"method": "POST",
|
||||
"process": "sui.Build.All",
|
||||
"in": ["$param.id", "$param.template_id", ":payload"],
|
||||
"out": {"status": 200}
|
||||
},{
|
||||
"path": "/:id/build/:template_id/*route",
|
||||
"method": "POST",
|
||||
"process": "sui.Build.Page",
|
||||
"in": ["$param.id", "$param.template_id", "$param.route", ":payload"],
|
||||
"out": {"status": 200}
|
||||
}
|
||||
],
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,6 +43,9 @@ func init() {
|
|||
"editor.sourceaftersavetemp": EditorSourceAfterSaveTemp,
|
||||
|
||||
"preview.render": PreviewRender,
|
||||
|
||||
"build.all": BuildAll,
|
||||
"build.page": BuildPage,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -571,6 +574,77 @@ func PreviewRender(process *process.Process) interface{} {
|
|||
return html
|
||||
}
|
||||
|
||||
// BuildAll handle the render page request
|
||||
func BuildAll(process *process.Process) interface{} {
|
||||
|
||||
process.ValidateArgNums(3)
|
||||
sui := get(process)
|
||||
templateID := process.ArgsString(1)
|
||||
|
||||
option := process.ArgsMap(2, map[string]interface{}{})
|
||||
ssr := true
|
||||
if v, ok := option["ssr"].(bool); ok {
|
||||
ssr = v
|
||||
}
|
||||
|
||||
assetRoot := ""
|
||||
if v, ok := option["asset_root"].(string); ok {
|
||||
assetRoot = v
|
||||
}
|
||||
|
||||
tmpl, err := sui.GetTemplate(templateID)
|
||||
if err != nil {
|
||||
exception.New(err.Error(), 500).Throw()
|
||||
}
|
||||
|
||||
err = tmpl.Build(&core.BuildOption{SSR: ssr, AssetRoot: assetRoot})
|
||||
if err != nil {
|
||||
exception.New(err.Error(), 500).Throw()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// BuildPage handle the render page request
|
||||
func BuildPage(process *process.Process) interface{} {
|
||||
process.ValidateArgNums(4)
|
||||
sui := get(process)
|
||||
templateID := process.ArgsString(1)
|
||||
route := route(process, 2)
|
||||
option := process.ArgsMap(3, map[string]interface{}{})
|
||||
ssr := true
|
||||
if v, ok := option["ssr"].(bool); ok {
|
||||
ssr = v
|
||||
}
|
||||
|
||||
assetRoot := ""
|
||||
if v, ok := option["asset_root"].(string); ok {
|
||||
assetRoot = v
|
||||
}
|
||||
|
||||
tmpl, err := sui.GetTemplate(templateID)
|
||||
if err != nil {
|
||||
exception.New(err.Error(), 500).Throw()
|
||||
}
|
||||
|
||||
page, err := tmpl.Page(route)
|
||||
if err != nil {
|
||||
exception.New(err.Error(), 500).Throw()
|
||||
}
|
||||
|
||||
err = page.Load()
|
||||
if err != nil {
|
||||
exception.New(err.Error(), 500).Throw()
|
||||
}
|
||||
|
||||
err = page.Build(&core.BuildOption{SSR: ssr, AssetRoot: assetRoot})
|
||||
if err != nil {
|
||||
exception.New(err.Error(), 500).Throw()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// get the sui
|
||||
func get(process *process.Process) core.SUI {
|
||||
sui, has := core.SUIs[process.ArgsString(0)]
|
||||
|
|
|
|||
|
|
@ -547,6 +547,27 @@ func TestEditorRender(t *testing.T) {
|
|||
assert.NotEmpty(t, res.(*core.ResponseEditorRender).Config)
|
||||
}
|
||||
|
||||
func TestEditorPageSource(t *testing.T) {
|
||||
load(t)
|
||||
defer clean()
|
||||
|
||||
sources := []string{"page", "script", "style", "data"}
|
||||
for _, source := range sources {
|
||||
p, err := process.Of("sui.editor.source", "demo", "tech-blue", "/index", source)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
res, err := p.Exec()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.IsType(t, core.SourceData{}, res)
|
||||
assert.NotEmpty(t, res.(core.SourceData).Source)
|
||||
assert.NotEmpty(t, res.(core.SourceData).Language)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEditorRenderWithQuery(t *testing.T) {
|
||||
load(t)
|
||||
defer clean()
|
||||
|
|
@ -587,25 +608,40 @@ func TestPreviewRender(t *testing.T) {
|
|||
assert.NotEmpty(t, res)
|
||||
}
|
||||
|
||||
func TestEditorPageSource(t *testing.T) {
|
||||
func TestBuildAll(t *testing.T) {
|
||||
load(t)
|
||||
defer clean()
|
||||
|
||||
sources := []string{"page", "script", "style", "data"}
|
||||
for _, source := range sources {
|
||||
p, err := process.Of("sui.editor.source", "demo", "tech-blue", "/index", source)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
res, err := p.Exec()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.IsType(t, core.SourceData{}, res)
|
||||
assert.NotEmpty(t, res.(core.SourceData).Source)
|
||||
assert.NotEmpty(t, res.(core.SourceData).Language)
|
||||
// test demo
|
||||
p, err := process.Of("sui.build.all", "demo", "tech-blue", map[string]interface{}{"ssr": true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
res, err := p.Exec()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.Nil(t, res)
|
||||
}
|
||||
|
||||
func TestBuildPage(t *testing.T) {
|
||||
load(t)
|
||||
defer clean()
|
||||
|
||||
// test demo
|
||||
p, err := process.Of("sui.build.page", "demo", "tech-blue", "/index", map[string]interface{}{"ssr": true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
res, err := p.Exec()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.Nil(t, res)
|
||||
}
|
||||
|
||||
func load(t *testing.T) {
|
||||
|
|
|
|||
|
|
@ -68,6 +68,10 @@ func (tmpl *Template) SyncAssets(option *core.BuildOption) error {
|
|||
// Build is the struct for the public
|
||||
func (page *Page) Build(option *core.BuildOption) error {
|
||||
|
||||
if option.AssetRoot == "" {
|
||||
option.AssetRoot = filepath.Join(page.tmpl.local.DSL.Public.Root, "assets")
|
||||
}
|
||||
|
||||
doc, _, err := page.Page.Build(option)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue