Skip to content

Commit

Permalink
Merge pull request #485 from trheyi/main
Browse files Browse the repository at this point in the history
[add] sui api guard and support session vars
  • Loading branch information
trheyi authored Oct 23, 2023
2 parents 87ae759 + 0927a12 commit 7753b41
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 10 deletions.
11 changes: 9 additions & 2 deletions sui/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down Expand Up @@ -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"],
Expand All @@ -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"],
Expand Down Expand Up @@ -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": {
Expand All @@ -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": {
Expand All @@ -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"],
Expand Down
1 change: 1 addition & 0 deletions sui/api/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions sui/core/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions sui/core/sui.go
Original file line number Diff line number Diff line change
@@ -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{
Expand All @@ -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
}
12 changes: 7 additions & 5 deletions sui/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 19 additions & 3 deletions sui/storages/local/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,22 @@ import (

"github.com/hashicorp/go-multierror"
"github.com/yaoapp/gou/application"
"github.com/yaoapp/kun/log"
"github.com/yaoapp/yao/sui/core"
)

// Build the template
func (tmpl *Template) Build(option *core.BuildOption) error {
var err error

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
}

if option.AssetRoot == "" {
option.AssetRoot = filepath.Join(tmpl.local.DSL.Public.Root, "assets")
option.AssetRoot = filepath.Join(root, "assets")
}

// Sync the assets
Expand Down Expand Up @@ -55,8 +62,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)
}
Expand Down Expand Up @@ -93,7 +105,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)
}

Expand Down

0 comments on commit 7753b41

Please sign in to comment.