89 lines
1.8 KiB
Go
89 lines
1.8 KiB
Go
package api
|
|
|
|
import (
|
|
"github.com/yaoapp/gou/process"
|
|
"github.com/yaoapp/kun/exception"
|
|
"github.com/yaoapp/yao/sui/core"
|
|
)
|
|
|
|
func init() {
|
|
process.RegisterGroup("sui", map[string]process.Handler{
|
|
"template.get": TemplateGet,
|
|
"template.find": TemplateFind,
|
|
|
|
"editor.render": EditorRender,
|
|
})
|
|
}
|
|
|
|
// TemplateGet handle the get Template request
|
|
// Process sui.<ID>.templates
|
|
func TemplateGet(process *process.Process) interface{} {
|
|
process.ValidateArgNums(1)
|
|
|
|
sui := get(process)
|
|
templates, err := sui.GetTemplates()
|
|
if err != nil {
|
|
exception.New(err.Error(), 500).Throw()
|
|
}
|
|
return templates
|
|
}
|
|
|
|
// TemplateFind handle the find Template request
|
|
func TemplateFind(process *process.Process) interface{} {
|
|
process.ValidateArgNums(2)
|
|
|
|
sui := get(process)
|
|
template, err := sui.GetTemplate(process.ArgsString(1))
|
|
if err != nil {
|
|
exception.New(err.Error(), 500).Throw()
|
|
}
|
|
|
|
return template
|
|
}
|
|
|
|
// EditorRender handle the render page request
|
|
func EditorRender(process *process.Process) interface{} {
|
|
process.ValidateArgNums(3)
|
|
|
|
sui := get(process)
|
|
templateID := process.ArgsString(1)
|
|
route := process.ArgsString(2)
|
|
query := process.ArgsMap(3, map[string]interface{}{"method": "GET"})
|
|
|
|
if route == "" {
|
|
route = "/index"
|
|
}
|
|
|
|
if route[0] != '/' {
|
|
route = "/" + route
|
|
}
|
|
|
|
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()
|
|
}
|
|
|
|
// Request data
|
|
req := &core.Request{Method: query["method"].(string)}
|
|
|
|
res, err := page.EditorRender(req)
|
|
if err != nil {
|
|
exception.New(err.Error(), 500).Throw()
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
// get the sui
|
|
func get(process *process.Process) core.SUI {
|
|
sui, has := core.SUIs[process.ArgsString(0)]
|
|
if !has {
|
|
exception.New("the sui %s does not exist", 404, process.ID).Throw()
|
|
}
|
|
return sui
|
|
}
|