Skip to content

Commit

Permalink
[add] template & page asset api
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Oct 1, 2023
1 parent 3b8f1f3 commit 9af9230
Show file tree
Hide file tree
Showing 10 changed files with 488 additions and 8 deletions.
22 changes: 22 additions & 0 deletions sui/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,28 @@ var dsl = []byte(`
"process": "sui.Editor.Source",
"in": ["$param.id", "$param.template_id", "$param.route", "$param.kind"],
"out": { "status": 200, "type": "application/json" }
},
{
"path": "/:id/assets/:template_id/@assets/*path",
"method": "GET",
"process": "sui.Template.Asset",
"in": ["$param.id", "$param.template_id", "$param.path"],
"out": {
"status": 200,
"body": "?:content",
"headers": { "Content-Type": "?:type"}
}
},{
"path": "/:id/assets/:template_id/@pages/*path",
"method": "GET",
"process": "sui.Page.Asset",
"in": ["$param.id", "$param.template_id", "$param.path"],
"out": {
"status": 200,
"body": "?:content",
"headers": { "Content-Type": "?:type"}
}
}
],
}
Expand Down
80 changes: 74 additions & 6 deletions sui/api/process.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"path/filepath"
"strings"

"github.com/yaoapp/gou/process"
Expand All @@ -10,8 +11,9 @@ import (

func init() {
process.RegisterGroup("sui", map[string]process.Handler{
"template.get": TemplateGet,
"template.find": TemplateFind,
"template.get": TemplateGet,
"template.find": TemplateFind,
"template.asset": TemplateAsset,

"locale.get": LocaleGet,
"theme.get": ThemeGet,
Expand All @@ -22,8 +24,9 @@ func init() {
"component.get": ComponentGet,
"component.find": ComponentFind,

"page.tree": PageTree,
"page.get": PageGet,
"page.tree": PageTree,
"page.get": PageGet,
"page.asset": PageAsset,

"editor.render": EditorRender,
"editor.source": EditorSource,
Expand All @@ -48,12 +51,32 @@ func TemplateFind(process *process.Process) interface{} {
process.ValidateArgNums(2)

sui := get(process)
template, err := sui.GetTemplate(process.ArgsString(1))
tmpl, err := sui.GetTemplate(process.ArgsString(1))
if err != nil {
exception.New(err.Error(), 500).Throw()
}

return tmpl
}

// TemplateAsset handle the find Template request
func TemplateAsset(process *process.Process) interface{} {
process.ValidateArgNums(3)
sui := get(process)
tmpl, err := sui.GetTemplate(process.ArgsString(1))
if err != nil {
exception.New(err.Error(), 500).Throw()
}

return template
asset, err := tmpl.Asset(process.ArgsString(2))
if err != nil {
exception.New(err.Error(), 404).Throw()
}

return map[string]interface{}{
"content": asset.Content,
"type": asset.Type,
}
}

// LocaleGet handle the find Template request
Expand Down Expand Up @@ -200,6 +223,51 @@ func PageGet(process *process.Process) interface{} {
return tree
}

// PageAsset handle the find Template request
func PageAsset(process *process.Process) interface{} {
process.ValidateArgNums(3)

sui := get(process)
templateID := process.ArgsString(1)

tmpl, err := sui.GetTemplate(templateID)
if err != nil {
exception.New(err.Error(), 500).Throw()
}

file := process.ArgsString(2)
page, err := tmpl.GetPageFromAsset(file)
if err != nil {
exception.New(err.Error(), 500).Throw()
}

var asset *core.Asset

switch filepath.Ext(file) {
case ".css":
asset, err = page.AssetStyle()
if err != nil {
exception.New(err.Error(), 400).Throw()
}
break

case ".js", ".ts":
asset, err = page.AssetScript()
if err != nil {
exception.New(err.Error(), 400).Throw()
}
break

default:
exception.New("does not support the %s file", 400, filepath.Ext(file)).Throw()
}

return map[string]interface{}{
"content": asset.Content,
"type": asset.Type,
}
}

// EditorRender handle the render page request
func EditorRender(process *process.Process) interface{} {
process.ValidateArgNums(3)
Expand Down
82 changes: 80 additions & 2 deletions sui/api/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ func TestTemplateFind(t *testing.T) {
assert.Equal(t, "tech-blue", res.(*local.Template).ID)
}

func TestTemplateAsset(t *testing.T) {
load(t)
defer clean()

// test demo
p, err := process.Of("sui.template.asset", "demo", "tech-blue", "/css/tailwind.css")
if err != nil {
t.Fatal(err)
}

res, err := p.Exec()
if err != nil {
t.Fatal(err)
}

assert.NotEmpty(t, res)
assert.Equal(t, "text/css; charset=utf-8", res.(map[string]interface{})["type"])
assert.NotEmpty(t, res.(map[string]interface{})["content"])
}

func TestTemplateLocaleGet(t *testing.T) {
load(t)
defer clean()
Expand Down Expand Up @@ -174,7 +194,7 @@ func TestTemplateComponentFind(t *testing.T) {
assert.Contains(t, res.(string), "window.component__Box=")
}

func TestTemplatePageTree(t *testing.T) {
func TestPageTree(t *testing.T) {
load(t)
defer clean()

Expand All @@ -195,7 +215,7 @@ func TestTemplatePageTree(t *testing.T) {
assert.Equal(t, "index", res.([]*core.PageTreeNode)[1].Name)
}

func TestTemplatePageGet(t *testing.T) {
func TestPageGet(t *testing.T) {
load(t)
defer clean()

Expand All @@ -218,6 +238,64 @@ func TestTemplatePageGet(t *testing.T) {
}
}

func TestPageAssetJS(t *testing.T) {

load(t)
defer clean()

// test demo
p, err := process.Of("sui.page.asset", "demo", "tech-blue", "/page/404/404.js")
if err != nil {
t.Fatal(err)
}

res, err := p.Exec()
if err != nil {
t.Fatal(err)
}
assert.IsType(t, map[string]interface{}{}, res)
assert.Equal(t, "text/javascript; charset=utf-8", res.(map[string]interface{})["type"])
assert.NotEmpty(t, res.(map[string]interface{})["content"])
}

func TestPageAssetTS(t *testing.T) {
load(t)
defer clean()

// test demo
p, err := process.Of("sui.page.asset", "demo", "tech-blue", "/page/404/404.ts")
if err != nil {
t.Fatal(err)
}

res, err := p.Exec()
if err != nil {
t.Fatal(err)
}
assert.IsType(t, map[string]interface{}{}, res)
assert.Equal(t, "text/javascript; charset=utf-8", res.(map[string]interface{})["type"])
assert.NotEmpty(t, res.(map[string]interface{})["content"])
}

func TestPageAssetCSS(t *testing.T) {
load(t)
defer clean()

// test demo
p, err := process.Of("sui.page.asset", "demo", "tech-blue", "/page/[id]/[id].css")
if err != nil {
t.Fatal(err)
}

res, err := p.Exec()
if err != nil {
t.Fatal(err)
}
assert.IsType(t, map[string]interface{}{}, res)
assert.Equal(t, "text/css; charset=utf-8", res.(map[string]interface{})["type"])
assert.NotEmpty(t, res.(map[string]interface{})["content"])
}

func TestEditorRender(t *testing.T) {
load(t)
defer clean()
Expand Down
44 changes: 44 additions & 0 deletions sui/core/compile.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,48 @@
package core

import (
"regexp"

"github.com/evanw/esbuild/pkg/api"
"github.com/yaoapp/gou/runtime/transform"
)

// Compile the page
func (page *Page) Compile() {}

// CompileJS compile the javascript
func (page *Page) CompileJS(source []byte, minify bool) ([]byte, error) {
jsCode := regexp.MustCompile(`import\s+.*;`).ReplaceAllString(string(source), "")
if minify {
minified, err := transform.MinifyJS(jsCode)
return []byte(minified), err
}
return []byte(jsCode), nil
}

// CompileTS compile the typescript
func (page *Page) CompileTS(source []byte, minify bool) ([]byte, error) {
tsCode := regexp.MustCompile(`import\s+.*;`).ReplaceAllString(string(source), "")
if minify {
jsCode, err := transform.TypeScript(string(tsCode), api.TransformOptions{
Target: api.ESNext,
MinifyWhitespace: true,
MinifyIdentifiers: true,
MinifySyntax: true,
})

return []byte(jsCode), err
}

jsCode, err := transform.TypeScript(string(tsCode), api.TransformOptions{Target: api.ESNext})
return []byte(jsCode), err
}

// CompileCSS compile the css
func (page *Page) CompileCSS(source []byte, minify bool) ([]byte, error) {
if minify {
cssCode, err := transform.MinifyCSS(string(source))
return []byte(cssCode), err
}
return source, nil
}
6 changes: 6 additions & 0 deletions sui/core/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type ITemplate interface {
Pages() ([]IPage, error)
PageTree(route string) ([]*PageTreeNode, error)
Page(route string) (IPage, error)
GetPageFromAsset(asset string) (IPage, error)

Blocks() ([]IBlock, error)
Block(name string) (IBlock, error)
Expand All @@ -25,6 +26,8 @@ type ITemplate interface {
Assets() []string
Locales() []SelectOption
Themes() []SelectOption

Asset(file string) (*Asset, error)
}

// IPage is the interface for the page
Expand All @@ -39,6 +42,9 @@ type IPage interface {
EditorStyleSource() ResponseSource
EditorDataSource() ResponseSource

AssetScript() (*Asset, error)
AssetStyle() (*Asset, error)

// Render()

// Html()
Expand Down
7 changes: 7 additions & 0 deletions sui/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ type SelectOption struct {
Value string `json:"value"`
}

// Asset is the struct for the asset
type Asset struct {
file string
Type string `json:"type"`
Content []byte `json:"content"`
}

// Request is the struct for the request
type Request struct {
Method string `json:"method"`
Expand Down
Loading

0 comments on commit 9af9230

Please sign in to comment.