[add] sui api guard and support session vars
This commit is contained in:
parent
9e88507e83
commit
598cdf69a8
6 changed files with 74 additions and 9 deletions
|
|
@ -7,11 +7,12 @@ var dsl = []byte(`
|
||||||
"name": "SUI API",
|
"name": "SUI API",
|
||||||
"description": "The API for SUI",
|
"description": "The API for SUI",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"guard": "-",
|
"guard": "bearer-jwt",
|
||||||
"group": "__yao/sui/v1",
|
"group": "__yao/sui/v1",
|
||||||
"paths": [
|
"paths": [
|
||||||
{
|
{
|
||||||
"path": "/:id/setting",
|
"path": "/:id/setting",
|
||||||
|
"guard": "-",
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
"process": "sui.Setting",
|
"process": "sui.Setting",
|
||||||
"in": ["$param.id"],
|
"in": ["$param.id"],
|
||||||
|
|
@ -60,12 +61,14 @@ var dsl = []byte(`
|
||||||
"out": { "status": 200, "type": "application/json" }
|
"out": { "status": 200, "type": "application/json" }
|
||||||
},{
|
},{
|
||||||
"path": "/:id/block/:template_id/:block_id",
|
"path": "/:id/block/:template_id/:block_id",
|
||||||
|
"guard": "query-jwt",
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
"process": "sui.Block.Find",
|
"process": "sui.Block.Find",
|
||||||
"in": ["$param.id", "$param.template_id", "$param.block_id"],
|
"in": ["$param.id", "$param.template_id", "$param.block_id"],
|
||||||
"out": { "status": 200, "type": "text/javascript" }
|
"out": { "status": 200, "type": "text/javascript" }
|
||||||
},{
|
},{
|
||||||
"path": "/:id/block/:template_id/:block_id/media",
|
"path": "/:id/block/:template_id/:block_id/media",
|
||||||
|
"guard": "query-jwt",
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
"process": "sui.Block.Media",
|
"process": "sui.Block.Media",
|
||||||
"in": ["$param.id", "$param.template_id", "$param.block_id"],
|
"in": ["$param.id", "$param.template_id", "$param.block_id"],
|
||||||
|
|
@ -84,6 +87,7 @@ var dsl = []byte(`
|
||||||
"out": { "status": 200, "type": "application/json" }
|
"out": { "status": 200, "type": "application/json" }
|
||||||
},{
|
},{
|
||||||
"path": "/:id/component/:template_id/:component_id",
|
"path": "/:id/component/:template_id/:component_id",
|
||||||
|
"guard": "query-jwt",
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
"process": "sui.Component.Find",
|
"process": "sui.Component.Find",
|
||||||
"in": ["$param.id", "$param.template_id", "$param.component_id"],
|
"in": ["$param.id", "$param.template_id", "$param.component_id"],
|
||||||
|
|
@ -163,6 +167,7 @@ var dsl = []byte(`
|
||||||
{
|
{
|
||||||
"path": "/:id/asset/:template_id/@assets/*path",
|
"path": "/:id/asset/:template_id/@assets/*path",
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
|
"guard": "-",
|
||||||
"process": "sui.Template.Asset",
|
"process": "sui.Template.Asset",
|
||||||
"in": ["$param.id", "$param.template_id", "$param.path"],
|
"in": ["$param.id", "$param.template_id", "$param.path"],
|
||||||
"out": {
|
"out": {
|
||||||
|
|
@ -172,7 +177,8 @@ var dsl = []byte(`
|
||||||
}
|
}
|
||||||
},{
|
},{
|
||||||
"path": "/:id/asset/:template_id/@pages/*path",
|
"path": "/:id/asset/:template_id/@pages/*path",
|
||||||
"method": "GET",
|
"guard": "query-jwt",
|
||||||
|
"method": "-",
|
||||||
"process": "sui.Page.Asset",
|
"process": "sui.Page.Asset",
|
||||||
"in": ["$param.id", "$param.template_id", "$param.path"],
|
"in": ["$param.id", "$param.template_id", "$param.path"],
|
||||||
"out": {
|
"out": {
|
||||||
|
|
@ -184,6 +190,7 @@ var dsl = []byte(`
|
||||||
|
|
||||||
{
|
{
|
||||||
"path": "/:id/preview/:template_id/*route",
|
"path": "/:id/preview/:template_id/*route",
|
||||||
|
"guard": "query-jwt",
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
"process": "sui.Preview.Render",
|
"process": "sui.Preview.Render",
|
||||||
"in": ["$param.id", "$param.template_id", "$param.route", "$header.Referer", "$query.r", "$query.t"],
|
"in": ["$param.id", "$param.template_id", "$param.route", "$header.Referer", "$query.r", "$query.t"],
|
||||||
|
|
|
||||||
|
|
@ -708,6 +708,7 @@ func get(process *process.Process) core.SUI {
|
||||||
if !has {
|
if !has {
|
||||||
exception.New("the sui %s does not exist", 404, process.ID).Throw()
|
exception.New("the sui %s does not exist", 404, process.ID).Throw()
|
||||||
}
|
}
|
||||||
|
sui.WithSid(process.Sid)
|
||||||
return sui
|
return sui
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ type SUI interface {
|
||||||
GetTemplates() ([]ITemplate, error)
|
GetTemplates() ([]ITemplate, error)
|
||||||
GetTemplate(name string) (ITemplate, error)
|
GetTemplate(name string) (ITemplate, error)
|
||||||
UploadTemplate(src string, dst string) (ITemplate, error)
|
UploadTemplate(src string, dst string) (ITemplate, error)
|
||||||
|
WithSid(sid string)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ITemplate is the interface for the ITemplate
|
// ITemplate is the interface for the ITemplate
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,14 @@
|
||||||
package core
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/yaoapp/gou/session"
|
||||||
|
"github.com/yaoapp/kun/maps"
|
||||||
|
)
|
||||||
|
|
||||||
// Setting the struct for the DSL
|
// Setting the struct for the DSL
|
||||||
func (sui *DSL) Setting() (*Setting, error) {
|
func (sui *DSL) Setting() (*Setting, error) {
|
||||||
return &Setting{
|
return &Setting{
|
||||||
|
|
@ -10,3 +19,38 @@ func (sui *DSL) Setting() (*Setting, error) {
|
||||||
},
|
},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithSid set the sid
|
||||||
|
func (sui *DSL) WithSid(sid string) {
|
||||||
|
sui.Sid = sid
|
||||||
|
}
|
||||||
|
|
||||||
|
// PublicRoot returns the public root path
|
||||||
|
func (sui *DSL) PublicRoot() (string, error) {
|
||||||
|
// Cache the public root
|
||||||
|
if sui.publicRoot != "" {
|
||||||
|
return sui.publicRoot, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
ss := session.Global().ID(sui.Sid)
|
||||||
|
data, err := ss.Dump()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
vars := map[string]interface{}{"$session": data}
|
||||||
|
var root = sui.Public.Root
|
||||||
|
dot := maps.Of(vars).Dot()
|
||||||
|
|
||||||
|
re := regexp.MustCompile(`{{\s*([^{}]+)\s*}}`)
|
||||||
|
output := re.ReplaceAllStringFunc(root, func(matched string) string {
|
||||||
|
varName := strings.TrimSpace(matched[2 : len(matched)-2])
|
||||||
|
if value, ok := dot[varName]; ok {
|
||||||
|
return fmt.Sprint(value)
|
||||||
|
}
|
||||||
|
return matched
|
||||||
|
})
|
||||||
|
|
||||||
|
sui.publicRoot = output
|
||||||
|
return output, nil
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,11 +2,13 @@ package core
|
||||||
|
|
||||||
// DSL the struct for the DSL
|
// DSL the struct for the DSL
|
||||||
type DSL struct {
|
type DSL struct {
|
||||||
ID string `json:"-"`
|
ID string `json:"-"`
|
||||||
Name string `json:"name,omitempty"`
|
Name string `json:"name,omitempty"`
|
||||||
Guard string `json:"guard,omitempty"`
|
Guard string `json:"guard,omitempty"`
|
||||||
Storage *Storage `json:"storage,omitempty"`
|
Storage *Storage `json:"storage,omitempty"`
|
||||||
Public *Public `json:"public,omitempty"`
|
Public *Public `json:"public,omitempty"`
|
||||||
|
Sid string `json:"-"`
|
||||||
|
publicRoot string `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setting is the struct for the setting
|
// Setting is the struct for the setting
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import (
|
||||||
|
|
||||||
"github.com/hashicorp/go-multierror"
|
"github.com/hashicorp/go-multierror"
|
||||||
"github.com/yaoapp/gou/application"
|
"github.com/yaoapp/gou/application"
|
||||||
|
"github.com/yaoapp/kun/log"
|
||||||
"github.com/yaoapp/yao/sui/core"
|
"github.com/yaoapp/yao/sui/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -55,8 +56,13 @@ func (tmpl *Template) SyncAssets(option *core.BuildOption) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
//get target abs path
|
//get target abs path
|
||||||
root := tmpl.local.DSL.Public.Root
|
root, err := tmpl.local.DSL.PublicRoot()
|
||||||
|
if err != nil {
|
||||||
|
log.Error("SyncAssets: Get the public root error: %s. use %s", err.Error(), tmpl.local.DSL.Public.Root)
|
||||||
|
root = tmpl.local.DSL.Public.Root
|
||||||
|
}
|
||||||
targetRoot := filepath.Join(application.App.Root(), "public", root, "assets")
|
targetRoot := filepath.Join(application.App.Root(), "public", root, "assets")
|
||||||
|
|
||||||
if exist, _ := os.Stat(targetRoot); exist == nil {
|
if exist, _ := os.Stat(targetRoot); exist == nil {
|
||||||
os.MkdirAll(targetRoot, os.ModePerm)
|
os.MkdirAll(targetRoot, os.ModePerm)
|
||||||
}
|
}
|
||||||
|
|
@ -93,7 +99,11 @@ func (page *Page) Build(option *core.BuildOption) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (page *Page) publicFile() string {
|
func (page *Page) publicFile() string {
|
||||||
root := page.tmpl.local.DSL.Public.Root
|
root, err := page.tmpl.local.DSL.PublicRoot()
|
||||||
|
if err != nil {
|
||||||
|
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
|
||||||
|
}
|
||||||
return filepath.Join(application.App.Root(), "public", root, page.Route)
|
return filepath.Join(application.App.Root(), "public", root, page.Route)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue