[change] start command
This commit is contained in:
parent
75ee709c23
commit
2bd41ed135
7 changed files with 69 additions and 224 deletions
2
Makefile
2
Makefile
|
|
@ -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' | grep -vE 'examples|tests*|config')
|
||||
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')
|
||||
TESTTAGS ?= ""
|
||||
|
||||
# TESTWIDGETS := $(shell $(GO) list ./widgets/...)
|
||||
|
|
|
|||
119
cmd/start.go
119
cmd/start.go
|
|
@ -12,18 +12,18 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
"github.com/yaoapp/gou/api"
|
||||
"github.com/yaoapp/gou/connector"
|
||||
"github.com/yaoapp/gou/fs"
|
||||
"github.com/yaoapp/gou/schedule"
|
||||
"github.com/yaoapp/gou/server/http"
|
||||
"github.com/yaoapp/gou/store"
|
||||
"github.com/yaoapp/gou/task"
|
||||
"github.com/yaoapp/gou/websocket"
|
||||
"github.com/yaoapp/kun/log"
|
||||
"github.com/yaoapp/yao/config"
|
||||
"github.com/yaoapp/yao/engine"
|
||||
"github.com/yaoapp/yao/fs"
|
||||
"github.com/yaoapp/yao/service"
|
||||
"github.com/yaoapp/yao/setup"
|
||||
"github.com/yaoapp/yao/share"
|
||||
"github.com/yaoapp/yao/studio"
|
||||
)
|
||||
|
||||
var startDebug = false
|
||||
|
|
@ -55,23 +55,33 @@ var startCmd = &cobra.Command{
|
|||
// defer service.Stop(func() { fmt.Println(L("Service stopped")) })
|
||||
Boot()
|
||||
|
||||
if startDebug { // 强制 debug 模式启动
|
||||
// force debug
|
||||
if startDebug {
|
||||
config.Development()
|
||||
}
|
||||
|
||||
mode := config.Conf.Mode
|
||||
err := engine.Load(config.Conf) // 加载脚本等
|
||||
// load the application engine
|
||||
err := engine.Load(config.Conf)
|
||||
if err != nil {
|
||||
fmt.Println(color.RedString(L("Fatal: %s"), err.Error()))
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
port := fmt.Sprintf(":%d", config.Conf.Port)
|
||||
if port == ":80" {
|
||||
port = ""
|
||||
}
|
||||
|
||||
// variables for the service
|
||||
fs, err := fs.Get("system")
|
||||
if err != nil {
|
||||
fmt.Println(color.RedString(L("Fatal: %s"), err.Error()))
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
mode := config.Conf.Mode
|
||||
host := config.Conf.Host
|
||||
dataRoot, _ := fs.Root(config.Conf)
|
||||
dataRoot := fs.Root()
|
||||
|
||||
fmt.Println(color.WhiteString("\n---------------------------------"))
|
||||
fmt.Println(color.WhiteString(strings.TrimPrefix(share.App.Name, "::")), color.WhiteString(share.App.Version), mode)
|
||||
|
|
@ -81,50 +91,34 @@ var startCmd = &cobra.Command{
|
|||
fmt.Println(color.WhiteString(L("Root")), color.GreenString(" %s", root))
|
||||
}
|
||||
|
||||
if share.App.XGen == "1.0" {
|
||||
|
||||
root, _ := adminRoot()
|
||||
urls := []string{fmt.Sprintf("http://%s:%s", host, port)}
|
||||
if host == "0.0.0.0" {
|
||||
urls, _ = setup.URLs(config.Conf)
|
||||
}
|
||||
|
||||
fmt.Println(color.WhiteString(L("Data")), color.GreenString(" %s", dataRoot))
|
||||
fmt.Println(color.WhiteString(L(" XGEN")), color.GreenString(" 1.0"))
|
||||
fmt.Println(color.WhiteString(L("Listening")), color.GreenString(" %s:%d", config.Conf.Host, config.Conf.Port))
|
||||
for _, url := range urls {
|
||||
fmt.Println(color.CyanString("\n%s", url))
|
||||
fmt.Println(color.WhiteString("--------------------------"))
|
||||
fmt.Println(color.WhiteString(L("Frontend")), color.GreenString(" %s", url))
|
||||
fmt.Println(color.WhiteString(L("Dashboard")), color.GreenString(" %s/%s/login/admin", url, strings.Trim(root, "/")))
|
||||
fmt.Println(color.WhiteString(L("API")), color.GreenString(" %s/api", url))
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
if host == "0.0.0.0" {
|
||||
host = "127.0.0.1"
|
||||
}
|
||||
|
||||
fmt.Println(color.WhiteString(L("Data")), color.GreenString(" %s", dataRoot))
|
||||
fmt.Println(color.WhiteString(L("Frontend")), color.GreenString(" http://%s%s/", host, port))
|
||||
fmt.Println(color.WhiteString(L("Dashboard")), color.GreenString(" http://%s%s/xiang/login/admin", host, port))
|
||||
fmt.Println(color.WhiteString(L("API")), color.GreenString(" http://%s%s/api", host, port))
|
||||
fmt.Println(color.WhiteString(L("Listening")), color.GreenString(" %s:%d", config.Conf.Host, config.Conf.Port))
|
||||
root, _ := adminRoot()
|
||||
urls := []string{fmt.Sprintf("http://%s:%s", host, port)}
|
||||
if host == "0.0.0.0" {
|
||||
urls, _ = setup.URLs(config.Conf)
|
||||
}
|
||||
|
||||
// development mode
|
||||
fmt.Println(color.WhiteString(L("Data")), color.GreenString(" %s", dataRoot))
|
||||
fmt.Println(color.WhiteString(L("Listening")), color.GreenString(" %s:%d", config.Conf.Host, config.Conf.Port))
|
||||
for _, url := range urls {
|
||||
fmt.Println(color.CyanString("\n%s", url))
|
||||
fmt.Println(color.WhiteString("--------------------------"))
|
||||
fmt.Println(color.WhiteString(L("Frontend")), color.GreenString(" %s", url))
|
||||
fmt.Println(color.WhiteString(L("Dashboard")), color.GreenString(" %s/%s/login/admin", url, strings.Trim(root, "/")))
|
||||
fmt.Println(color.WhiteString(L("API")), color.GreenString(" %s/api", url))
|
||||
}
|
||||
|
||||
// print the messages under the development mode
|
||||
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)
|
||||
|
|
@ -135,14 +129,16 @@ var startCmd = &cobra.Command{
|
|||
|
||||
}
|
||||
|
||||
// start watching
|
||||
if mode == "development" && !startDisableWatching {
|
||||
// Watching
|
||||
fmt.Println(color.WhiteString("\n---------------------------------"))
|
||||
fmt.Println(color.WhiteString(L("Watching")))
|
||||
fmt.Println(color.WhiteString("---------------------------------"))
|
||||
service.Watch(config.Conf)
|
||||
// service.Watch(config.Conf)
|
||||
}
|
||||
|
||||
// print the messages under the production mode
|
||||
if mode == "production" {
|
||||
printApis(true)
|
||||
printTasks(true)
|
||||
|
|
@ -152,25 +148,28 @@ var startCmd = &cobra.Command{
|
|||
}
|
||||
|
||||
srv, err := service.Start(config.Conf)
|
||||
|
||||
// Start server
|
||||
go func() {
|
||||
srv.Start()
|
||||
if err != nil {
|
||||
fmt.Println(color.RedString(L("Fatal: %s"), err.Error()))
|
||||
os.Exit(3)
|
||||
}
|
||||
defer func() {
|
||||
service.Stop(srv)
|
||||
fmt.Println(color.GreenString(L("✨EXITED✨")))
|
||||
}()
|
||||
defer srv.Stop()
|
||||
|
||||
fmt.Println(color.GreenString(L("✨LISTENING✨")))
|
||||
|
||||
for {
|
||||
select {
|
||||
case v := <-srv.Event():
|
||||
|
||||
switch v {
|
||||
case http.READY:
|
||||
fmt.Println(color.GreenString(L("✨LISTENING✨")))
|
||||
break
|
||||
|
||||
case http.CLOSED:
|
||||
fmt.Println(color.GreenString(L("✨EXITED✨")))
|
||||
return
|
||||
default:
|
||||
fmt.Println("hello", v)
|
||||
}
|
||||
|
||||
case <-interrupt:
|
||||
// ctx, canceled := context.WithTimeout(context.Background(), (5 * time.Second))
|
||||
// defer canceled()
|
||||
service.Stop(srv)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package config
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
|
@ -109,6 +110,11 @@ func ReloadLog() {
|
|||
// OpenLog 打开日志
|
||||
func OpenLog() {
|
||||
if Conf.Log != "" {
|
||||
|
||||
if !filepath.IsAbs(Conf.Log) {
|
||||
Conf.Log = filepath.Join(Conf.Root, Conf.Log)
|
||||
}
|
||||
|
||||
logfile, err := filepath.Abs(Conf.Log)
|
||||
if err != nil {
|
||||
log.With(log.F{"file": logfile}).Error(err.Error())
|
||||
|
|
@ -129,7 +135,7 @@ func OpenLog() {
|
|||
}
|
||||
|
||||
log.SetOutput(LogOutput)
|
||||
gin.DefaultWriter = LogOutput
|
||||
gin.DefaultWriter = io.MultiWriter(LogOutput)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
22
init_test.go
22
init_test.go
|
|
@ -1,22 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/yaoapp/gou/plugin"
|
||||
"github.com/yaoapp/yao/config"
|
||||
)
|
||||
|
||||
var cfg config.Config
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
|
||||
// Run test suites
|
||||
exitVal := m.Run()
|
||||
|
||||
// we can do clean up code here
|
||||
plugin.KillAll()
|
||||
|
||||
os.Exit(exitVal)
|
||||
}
|
||||
139
main_test.go
139
main_test.go
|
|
@ -1,139 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/yaoapp/kun/any"
|
||||
"github.com/yaoapp/kun/maps"
|
||||
"github.com/yaoapp/yao/config"
|
||||
)
|
||||
|
||||
func TestCommandVersion(t *testing.T) {
|
||||
oldArgs := os.Args
|
||||
defer func() { os.Args = oldArgs }()
|
||||
|
||||
os.Args = append(os.Args, "version")
|
||||
assert.NotPanics(t, func() {
|
||||
main()
|
||||
})
|
||||
}
|
||||
|
||||
func TestCommandMigrate(t *testing.T) {
|
||||
oldArgs := os.Args
|
||||
defer func() { os.Args = oldArgs }()
|
||||
|
||||
os.Args = append(os.Args, "migrate", "--reset", "--force")
|
||||
assert.NotPanics(t, func() {
|
||||
main()
|
||||
})
|
||||
}
|
||||
|
||||
func TestCommandStart(t *testing.T) {
|
||||
oldArgs := os.Args
|
||||
defer func() {
|
||||
os.Args = oldArgs
|
||||
// service.Stop(func() {})
|
||||
log.Println("服务已关闭")
|
||||
}()
|
||||
|
||||
go func() {
|
||||
os.Args = append(os.Args, "start")
|
||||
main()
|
||||
}()
|
||||
|
||||
// 发送请求
|
||||
request := func() (maps.MapStr, error) {
|
||||
time.Sleep(time.Microsecond * 2000)
|
||||
url := fmt.Sprintf("http://%s:%d/api/user/find/1?select=id,name", "127.0.0.1", config.Conf.Port)
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
res := maps.MakeMapStr()
|
||||
err = jsoniter.Unmarshal(body, &res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// 等待服务启动
|
||||
times := 0
|
||||
for times < 30 { // 2秒超时
|
||||
time.Sleep(time.Second * 2)
|
||||
times++
|
||||
fmt.Printf("Trying(%d)...", times)
|
||||
res, err := request()
|
||||
if err != nil {
|
||||
fmt.Printf(" %s\n", err.Error())
|
||||
continue
|
||||
}
|
||||
assert.Equal(t, 1, any.Of(res.Get("id")).CInt())
|
||||
assert.Equal(t, "管理员", res.Get("name"))
|
||||
return
|
||||
}
|
||||
|
||||
assert.True(t, false)
|
||||
}
|
||||
|
||||
func TestCommandStop(t *testing.T) {
|
||||
oldArgs := os.Args
|
||||
defer func() {
|
||||
os.Args = oldArgs
|
||||
}()
|
||||
go func() {
|
||||
os.Args = append(os.Args, "start")
|
||||
main()
|
||||
}()
|
||||
|
||||
// 发送请求
|
||||
request := func() (maps.MapStr, error) {
|
||||
time.Sleep(time.Microsecond * 2000)
|
||||
url := fmt.Sprintf("http://%s:%d/api/user/find/1?select=id,name", "127.0.0.1", config.Conf.Port)
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
res := maps.MakeMapStr()
|
||||
err = jsoniter.Unmarshal(body, &res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// 等待服务启动
|
||||
times := 0
|
||||
for times < 30 { // 2秒超时
|
||||
time.Sleep(time.Second * 2)
|
||||
times++
|
||||
res, err := request()
|
||||
if err != nil {
|
||||
fmt.Println("REQUEST ERROR:", err)
|
||||
continue
|
||||
}
|
||||
assert.Equal(t, 1, any.Of(res.Get("id")).CInt())
|
||||
assert.Equal(t, "管理员", res.Get("name"))
|
||||
|
||||
// 测试关闭
|
||||
// service.Stop(func() { log.Println("服务已关闭") })
|
||||
time.Sleep(time.Second * 5)
|
||||
_, err = request()
|
||||
assert.NotNil(t, err)
|
||||
return
|
||||
}
|
||||
|
||||
assert.True(t, false)
|
||||
}
|
||||
|
|
@ -23,6 +23,7 @@ func Start(cfg config.Config) (*http.Server, error) {
|
|||
}
|
||||
|
||||
router := gin.New()
|
||||
router.Use(gin.Logger())
|
||||
api.SetGuards(Guards)
|
||||
api.SetRoutes(router, "/api", cfg.AllowFrom...)
|
||||
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ func Check() bool {
|
|||
|
||||
root := appRoot()
|
||||
|
||||
appfile := filepath.Join(root, "app.json")
|
||||
appfile := filepath.Join(root, "app.yao")
|
||||
if _, err := os.Stat(appfile); err != nil && os.IsNotExist(err) {
|
||||
return true
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue