Skip to content

Commit

Permalink
[add] migrate studio
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Apr 1, 2023
1 parent 82dfe75 commit 5d2c434
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 351 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ COMMIT := $(shell git log | head -n 1 | awk '{print substr($$2, 0, 12)}')
NOW := $(shell date +"%FT%T%z")

# ROOT_DIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
TESTFOLDER := $(shell $(GO) list ./... | grep -E 'api|model|flow|script|fs|i18n|connector|query|plugin|cert|crypto|task|schedule|runtime|helper|utils|widget|importer|store|widgets|engine|service' | grep -vE 'examples|tests*|config')
TESTFOLDER := $(shell $(GO) list ./... | grep -vE 'examples|tests|share*')
TESTTAGS ?= ""

# TESTWIDGETS := $(shell $(GO) list ./widgets/...)
Expand Down
17 changes: 9 additions & 8 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/yaoapp/yao/service"
"github.com/yaoapp/yao/setup"
"github.com/yaoapp/yao/share"
"github.com/yaoapp/yao/studio"
)

var startDebug = false
Expand Down Expand Up @@ -115,14 +116,14 @@ var startCmd = &cobra.Command{
if mode == "development" {

// Start Studio Server
// go func() {
// err := studio.Start(config.Conf)
// if err != nil {
// fmt.Println(color.RedString(L("Fatal: %s"), err.Error()))
// os.Exit(2)
// }
// }()
// defer studio.Stop()
go func() {
err := studio.Start(config.Conf)
if err != nil {
fmt.Println(color.RedString(L("Fatal: %s"), err.Error()))
os.Exit(2)
}
}()
defer studio.Stop()

printApis(false)
printTasks(false)
Expand Down
13 changes: 12 additions & 1 deletion cmd/studio/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/engine"
"github.com/yaoapp/yao/share"
"github.com/yaoapp/yao/studio"
)

// RunCmd command
Expand All @@ -34,13 +35,23 @@ var RunCmd = &cobra.Command{
Boot()
cfg := config.Conf
cfg.Session.IsCLI = true
engine.Load(cfg)

if len(args) < 1 {
fmt.Println(color.RedString(L("Not enough arguments")))
fmt.Println(color.WhiteString(share.BUILDNAME + " help"))
return
}

err := engine.Load(cfg)
if err != nil {
fmt.Println(color.RedString(L("Engine: %s"), err.Error()))
}

err = studio.Load(cfg)
if err != nil {
fmt.Println(color.RedString(L("Studio: %s"), err.Error()))
}

name := strings.Split(args[0], ".")
service := strings.Join(name[0:len(name)-1], ".")
method := name[len(name)-1]
Expand Down
24 changes: 23 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func init() {
Conf = Load()
return
}

Conf = LoadFrom(filename)
if Conf.Mode == "production" {
Production()
Expand All @@ -56,14 +57,21 @@ func LoadFrom(envfile string) Config {
return Load()
}

// Load 加载配置
// Load the config
func Load() Config {
cfg := Config{}
if err := env.Parse(&cfg); err != nil {
exception.New("Can't read config %s", 500, err.Error()).Throw()
}

// Root path
cfg.Root, _ = filepath.Abs(cfg.Root)

// App Root
if cfg.AppSource == "" {
cfg.AppSource = cfg.Root
}

// Studio Secret
if cfg.Studio.Secret == nil {
v, err := crypto.Hash(crypto.HashTypes["SHA256"], uuid.New().String())
Expand All @@ -74,6 +82,20 @@ func Load() Config {
cfg.Studio.Auto = true
}

// DataRoot
if cfg.DataRoot == "" {
cfg.DataRoot = filepath.Join(cfg.Root, "data")
if !filepath.IsAbs(cfg.DataRoot) {
cfg.DataRoot, _ = filepath.Abs(cfg.DataRoot)
}

if _, err := os.Stat(cfg.DataRoot); errors.Is(err, os.ErrNotExist) {
if err := os.MkdirAll(cfg.DataRoot, os.ModePerm); err != nil {
exception.New("Can't create data root %s", 500, err.Error()).Throw()
}
}
}

return cfg
}

Expand Down
5 changes: 3 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"fmt"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -56,11 +57,11 @@ func TestLoadFrom(t *testing.T) {
assert.Equal(t, cfg.Root, root)
assert.Equal(t, cfg.Mode, os.Getenv("YAO_ENV"))
assert.Equal(t, cfg.Host, os.Getenv("YAO_HOST"))
assert.Equal(t, cfg.Port, os.Getenv("YAO_PORT"))
assert.Equal(t, fmt.Sprintf("%d", cfg.Port), os.Getenv("YAO_PORT"))
assert.Equal(t, cfg.JWTSecret, os.Getenv("YAO_JWT_SECRET"))
assert.Equal(t, cfg.Log, os.Getenv("YAO_LOG"))
assert.Equal(t, cfg.LogMode, os.Getenv("YAO_LOG_MODE"))
assert.Equal(t, cfg.DB.Driver, os.Getenv("YAO_DB_DRIVER"))
assert.Equal(t, cfg.DB.Primary[0], os.Getenv("YAO_DB_PRIMARY"))
assert.Equal(t, cfg.DB.Secondary[0], os.Getenv("YAO_DB_SECONDARY"))
// assert.Equal(t, cfg.DB.Secondary[0], os.Getenv("YAO_DB_SECONDARY"))
}
23 changes: 12 additions & 11 deletions config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ package config

// Config 象传应用引擎配置
type Config struct {
Mode string `json:"mode,omitempty" env:"YAO_ENV" envDefault:"production"` // 象传引擎启动模式 production/development
Root string `json:"root,omitempty" env:"YAO_ROOT" envDefault:"."` // 应用根目录
Mode string `json:"mode,omitempty" env:"YAO_ENV" envDefault:"production"` // The start mode production/development
AppSource string `json:"app,omitempty" env:"YAO_APP_SOURCE"` // The Application Source Root Path default same as Root
Root string `json:"root,omitempty" env:"YAO_ROOT" envDefault:"."` // The Application Root Path
Lang string `json:"lang,omitempty" env:"YAO_LANG" envDefault:"en-us"` // Default language setting
TimeZone string `json:"timezone,omitempty" env:"YAO_TIMEZONE"` // Default TimeZone
DataRoot string `json:"data_root,omitempty" env:"YAO_DATA_ROOT" envDefault:""` // DATA PATH
DataRoot string `json:"data_root,omitempty" env:"YAO_DATA_ROOT" envDefault:""` // The data root path
ExtensionRoot string `json:"extension_root,omitempty" env:"YAO_EXTENSION_ROOT" envDefault:""` // Plugin, Wasm root PATH, Default is <YAO_ROOT> (<YAO_ROOT>/plugins <YAO_ROOT>/wasms)
Host string `json:"host,omitempty" env:"YAO_HOST" envDefault:"0.0.0.0"` // 服务监听地址
Port int `json:"port,omitempty" env:"YAO_PORT" envDefault:"5099"` // 服务监听端口
Cert string `json:"cert,omitempty" env:"YAO_CERT"` // HTTPS 证书文件地址
Key string `json:"key,omitempty" env:"YAO_KEY"` // HTTPS 证书密钥地址
Log string `json:"log,omitempty" env:"YAO_LOG"` // 服务日志地址
LogMode string `json:"log_mode,omitempty" env:"YAO_LOG_MODE" envDefault:"TEXT"` // 服务日志模式 JSON|TEXT
JWTSecret string `json:"jwt_secret,omitempty" env:"YAO_JWT_SECRET"` // JWT 密钥
DB Database `json:"db,omitempty"` // 数据库配置
Host string `json:"host,omitempty" env:"YAO_HOST" envDefault:"0.0.0.0"` // The server host
Port int `json:"port,omitempty" env:"YAO_PORT" envDefault:"5099"` // The server port
Cert string `json:"cert,omitempty" env:"YAO_CERT"` // The HTTPS certificate path
Key string `json:"key,omitempty" env:"YAO_KEY"` // The HTTPS certificate key path
Log string `json:"log,omitempty" env:"YAO_LOG"` // The log file path
LogMode string `json:"log_mode,omitempty" env:"YAO_LOG_MODE" envDefault:"TEXT"` // The log mode TEXT|JSON
JWTSecret string `json:"jwt_secret,omitempty" env:"YAO_JWT_SECRET"` // The JWT Secret
DB Database `json:"db,omitempty"` // The database config
AllowFrom []string `json:"allowfrom,omitempty" envSeparator:"|" env:"YAO_ALLOW_FROM"` // Domain list the separator is |
Session Session `json:"session,omitempty"` // Session Config
Studio Studio `json:"studio,omitempty"` // Studio config
Expand Down
Loading

0 comments on commit 5d2c434

Please sign in to comment.