Merge pull request #206 from trheyi/main

[add] app setting api
This commit is contained in:
Max 2022-10-16 23:27:52 +08:00 committed by GitHub
commit eddc0793b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 103 additions and 9 deletions

View file

@ -17,6 +17,7 @@ import (
"github.com/yaoapp/yao/connector"
"github.com/yaoapp/yao/flow"
"github.com/yaoapp/yao/fs"
"github.com/yaoapp/yao/i18n"
"github.com/yaoapp/yao/importer"
"github.com/yaoapp/yao/model"
"github.com/yaoapp/yao/page"
@ -65,10 +66,18 @@ func Load(cfg config.Config) (err error) {
printErr(cfg.Mode, "FileSystem", err)
}
// Load Studio
err = studio.Load(cfg)
// Load i18n
err = i18n.Load(cfg)
if err != nil {
printErr(cfg.Mode, "Studio", err)
printErr(cfg.Mode, "i18n", err)
}
// Load Studio development mode only
if cfg.Mode == "development" {
err = studio.Load(cfg)
if err != nil {
printErr(cfg.Mode, "Studio", err)
}
}
// 第二步: 建立数据库 & 会话连接

View file

@ -6,9 +6,11 @@ import (
"os"
"path/filepath"
"strings"
"time"
jsoniter "github.com/json-iterator/go"
"github.com/yaoapp/gou"
"github.com/yaoapp/gou/session"
"github.com/yaoapp/kun/exception"
"github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/data"
@ -43,10 +45,6 @@ func LoadAndExport(cfg config.Config) error {
// Load the app DSL
func Load(cfg config.Config) error {
if os.Getenv("YAO_LANG") == "" {
os.Setenv("YAO_LANG", "en-us")
}
file := filepath.Join(cfg.Root, "app.json")
file, err := filepath.Abs(file)
if err != nil {
@ -58,7 +56,7 @@ func Load(cfg config.Config) error {
return err
}
dsl := &DSL{Optional: OptionalDSL{}}
dsl := &DSL{Optional: OptionalDSL{}, Lang: cfg.Lang}
err = jsoniter.Unmarshal(data, dsl)
if err != nil {
return err
@ -110,6 +108,19 @@ func exportAPI() error {
}
http.Paths = append(http.Paths, path)
// POST
path = gou.Path{
Label: "App Setting",
Description: "App Setting",
Guard: "-",
Path: "/setting",
Method: "POST",
Process: process,
In: []string{":payload"},
Out: gou.Out{Status: 200, Type: "application/json"},
}
http.Paths = append(http.Paths, path)
process = "yao.app.Menu"
args := []string{}
if Setting.Menu.Process != "" {
@ -219,11 +230,33 @@ func processSetting(process *gou.Process) interface{} {
return nil
}
sid := process.Sid
if sid == "" {
sid = session.ID()
}
// Set User ENV
if process.NumOfArgs() > 0 {
payload := process.ArgsMap(0, map[string]interface{}{
"now": time.Now().Unix(),
"lang": "en-us",
"sid": "",
})
if payload["sid"] != "" {
sid = payload["sid"].(string)
}
lang := strings.ToLower(fmt.Sprintf("%v", payload["lang"]))
session.Global().ID(sid).Set("__yao_lang", lang)
}
setting, err := i18n.Trans(process.Lang(), "app", "app", Setting)
if err != nil {
exception.New(err.Error(), 500).Throw()
}
setting.(*DSL).Sid = sid
return *setting.(*DSL)
}
@ -233,6 +266,27 @@ func processXgen(process *gou.Process) interface{} {
exception.New("the app does not init", 500).Throw()
}
sid := process.Sid
if sid == "" {
sid = session.ID()
}
// Set User ENV
if process.NumOfArgs() > 0 {
payload := process.ArgsMap(0, map[string]interface{}{
"now": time.Now().Unix(),
"lang": "en-us",
"sid": "",
})
if payload["sid"] != "" {
sid = payload["sid"].(string)
}
lang := strings.ToLower(fmt.Sprintf("%v", payload["lang"]))
session.Global().ID(sid).Set("__yao_lang", lang)
}
mode := os.Getenv("YAO_ENV")
if mode == "" {
mode = "production"
@ -304,6 +358,7 @@ func processXgen(process *gou.Process) interface{} {
"name": Setting.Name,
"description": Setting.Description,
"theme": Setting.Theme,
"lang": Setting.Lang,
"mode": mode,
"apiPrefix": "__yao",
"token": "localStorage",
@ -324,6 +379,7 @@ func processXgen(process *gou.Process) interface{} {
exception.New(err.Error(), 500).Throw()
}
setting.(map[string]interface{})["sid"] = sid
return setting.(map[string]interface{})
}

View file

@ -118,7 +118,7 @@ func TestExport(t *testing.T) {
api, has := gou.APIs["widgets.app"]
assert.True(t, has)
assert.Equal(t, 3, len(api.HTTP.Paths))
assert.Equal(t, 4, len(api.HTTP.Paths))
_, has = gou.ThirdHandlers["yao.app.setting"]
assert.True(t, has)
@ -146,6 +146,19 @@ func TestProcessSetting(t *testing.T) {
assert.Equal(t, "flows.app.menu", setting.Menu.Process)
assert.Equal(t, true, setting.Optional.HideNotification)
assert.Equal(t, false, setting.Optional.HideSetting)
assert.Equal(t, true, setting.Sid != "")
// Set
res, err = gou.NewProcess("yao.app.Setting", map[string]interface{}{"lang": "zh-hk", "sid": setting.Sid}).Exec()
if err != nil {
t.Fatal(err)
}
setting2, ok := res.(DSL)
assert.Equal(t, setting.Sid, setting2.Sid)
lang := gou.NewProcess("yao.app.Setting").WithSID(setting.Sid).Lang()
assert.Equal(t, "zh-hk", lang)
}
func TestProcessXgen(t *testing.T) {
@ -171,6 +184,20 @@ func TestProcessXgen(t *testing.T) {
assert.Equal(t, "Demo Application", xgen.Get("name"))
assert.Equal(t, true, xgen.Get("optional.hideNotification"))
assert.Equal(t, "localStorage", xgen.Get("token"))
assert.Equal(t, true, xgen.Get("sid").(string) != "")
// Set
res, err = gou.NewProcess("yao.app.Xgen", map[string]interface{}{"lang": "zh-hk", "sid": xgen.Get("sid")}).Exec()
if err != nil {
t.Fatal(err)
}
xgen2 := any.Of(res).MapStr().Dot()
assert.Equal(t, xgen.Get("sid"), xgen2.Get("sid"))
lang := gou.NewProcess("yao.app.Setting").WithSID(xgen2.Get("sid").(string)).Lang()
assert.Equal(t, "zh-hk", lang)
}
func TestProcessMenu(t *testing.T) {

View file

@ -7,6 +7,8 @@ type DSL struct {
Version string `json:"version,omitempty"`
Description string `json:"description,omitempty"`
Theme string `json:"theme,omitempty"`
Lang string `json:"lang,omitempty"`
Sid string `json:"sid,omitempty"`
Logo string `json:"logo,omitempty"`
Favicon string `json:"favicon,omitempty"`
Menu MenuDSL `json:"menu,omitempty"`