yao/sui/api/process.go
2023-09-28 10:47:18 +08:00

133 lines
2.7 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,
"editor.source": EditorSource,
})
}
// 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 := route(process, 2)
query := process.ArgsMap(3, map[string]interface{}{"method": "GET"})
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
}
// EditorSource handle the render page request
func EditorSource(process *process.Process) interface{} {
process.ValidateArgNums(3)
sui := get(process)
templateID := process.ArgsString(1)
route := route(process, 2)
kind := process.ArgsString(3)
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()
}
switch kind {
case "page":
return page.EditorPageSource()
case "style":
return page.EditorStyleSource()
case "script":
return page.EditorScriptSource()
case "data":
return page.EditorDataSource()
default:
exception.New("the %s source does not exist", 404, kind).Throw()
return nil
}
}
// 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
}
func route(process *process.Process, i int) string {
route := process.ArgsString(i)
if route == "" {
route = "/index"
}
if route[0] != '/' {
route = "/" + route
}
return route
}