From 598cdf69a812950d244d803516bfc45b0d54cd12 Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 23 Oct 2023 13:00:17 +0800 Subject: [PATCH] [add] sui api guard and support session vars --- sui/api/api.go | 11 ++++++++-- sui/api/process.go | 1 + sui/core/interfaces.go | 1 + sui/core/sui.go | 44 +++++++++++++++++++++++++++++++++++++ sui/core/types.go | 12 +++++----- sui/storages/local/build.go | 14 ++++++++++-- 6 files changed, 74 insertions(+), 9 deletions(-) diff --git a/sui/api/api.go b/sui/api/api.go index 771b82ab..db15d98a 100644 --- a/sui/api/api.go +++ b/sui/api/api.go @@ -7,11 +7,12 @@ var dsl = []byte(` "name": "SUI API", "description": "The API for SUI", "version": "1.0.0", - "guard": "-", + "guard": "bearer-jwt", "group": "__yao/sui/v1", "paths": [ { "path": "/:id/setting", + "guard": "-", "method": "GET", "process": "sui.Setting", "in": ["$param.id"], @@ -60,12 +61,14 @@ var dsl = []byte(` "out": { "status": 200, "type": "application/json" } },{ "path": "/:id/block/:template_id/:block_id", + "guard": "query-jwt", "method": "GET", "process": "sui.Block.Find", "in": ["$param.id", "$param.template_id", "$param.block_id"], "out": { "status": 200, "type": "text/javascript" } },{ "path": "/:id/block/:template_id/:block_id/media", + "guard": "query-jwt", "method": "GET", "process": "sui.Block.Media", "in": ["$param.id", "$param.template_id", "$param.block_id"], @@ -84,6 +87,7 @@ var dsl = []byte(` "out": { "status": 200, "type": "application/json" } },{ "path": "/:id/component/:template_id/:component_id", + "guard": "query-jwt", "method": "GET", "process": "sui.Component.Find", "in": ["$param.id", "$param.template_id", "$param.component_id"], @@ -163,6 +167,7 @@ var dsl = []byte(` { "path": "/:id/asset/:template_id/@assets/*path", "method": "GET", + "guard": "-", "process": "sui.Template.Asset", "in": ["$param.id", "$param.template_id", "$param.path"], "out": { @@ -172,7 +177,8 @@ var dsl = []byte(` } },{ "path": "/:id/asset/:template_id/@pages/*path", - "method": "GET", + "guard": "query-jwt", + "method": "-", "process": "sui.Page.Asset", "in": ["$param.id", "$param.template_id", "$param.path"], "out": { @@ -184,6 +190,7 @@ var dsl = []byte(` { "path": "/:id/preview/:template_id/*route", + "guard": "query-jwt", "method": "GET", "process": "sui.Preview.Render", "in": ["$param.id", "$param.template_id", "$param.route", "$header.Referer", "$query.r", "$query.t"], diff --git a/sui/api/process.go b/sui/api/process.go index f3f74766..691ff657 100644 --- a/sui/api/process.go +++ b/sui/api/process.go @@ -708,6 +708,7 @@ func get(process *process.Process) core.SUI { if !has { exception.New("the sui %s does not exist", 404, process.ID).Throw() } + sui.WithSid(process.Sid) return sui } diff --git a/sui/core/interfaces.go b/sui/core/interfaces.go index ecaaa432..bb464ef8 100644 --- a/sui/core/interfaces.go +++ b/sui/core/interfaces.go @@ -9,6 +9,7 @@ type SUI interface { GetTemplates() ([]ITemplate, error) GetTemplate(name string) (ITemplate, error) UploadTemplate(src string, dst string) (ITemplate, error) + WithSid(sid string) } // ITemplate is the interface for the ITemplate diff --git a/sui/core/sui.go b/sui/core/sui.go index bc62ad83..982818e2 100644 --- a/sui/core/sui.go +++ b/sui/core/sui.go @@ -1,5 +1,14 @@ package core +import ( + "fmt" + "regexp" + "strings" + + "github.com/yaoapp/gou/session" + "github.com/yaoapp/kun/maps" +) + // Setting the struct for the DSL func (sui *DSL) Setting() (*Setting, error) { return &Setting{ @@ -10,3 +19,38 @@ func (sui *DSL) Setting() (*Setting, error) { }, }, 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 +} diff --git a/sui/core/types.go b/sui/core/types.go index 7621d056..d02f12b5 100644 --- a/sui/core/types.go +++ b/sui/core/types.go @@ -2,11 +2,13 @@ package core // DSL the struct for the DSL type DSL struct { - ID string `json:"-"` - Name string `json:"name,omitempty"` - Guard string `json:"guard,omitempty"` - Storage *Storage `json:"storage,omitempty"` - Public *Public `json:"public,omitempty"` + ID string `json:"-"` + Name string `json:"name,omitempty"` + Guard string `json:"guard,omitempty"` + Storage *Storage `json:"storage,omitempty"` + Public *Public `json:"public,omitempty"` + Sid string `json:"-"` + publicRoot string `json:"-"` } // Setting is the struct for the setting diff --git a/sui/storages/local/build.go b/sui/storages/local/build.go index c38ec8e3..23fc716b 100644 --- a/sui/storages/local/build.go +++ b/sui/storages/local/build.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/go-multierror" "github.com/yaoapp/gou/application" + "github.com/yaoapp/kun/log" "github.com/yaoapp/yao/sui/core" ) @@ -55,8 +56,13 @@ func (tmpl *Template) SyncAssets(option *core.BuildOption) error { } //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") + if exist, _ := os.Stat(targetRoot); exist == nil { os.MkdirAll(targetRoot, os.ModePerm) } @@ -93,7 +99,11 @@ func (page *Page) Build(option *core.BuildOption) error { } 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) }