diff --git a/cmd/run.go b/cmd/run.go index 17ac45c39d..5b91a08bdb 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -18,7 +18,9 @@ var runCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { defer gou.KillPlugins() Boot() - engine.Load(config.Conf) + cfg := config.Conf + cfg.Session.IsCLI = true + engine.Load(cfg) if len(args) < 1 { fmt.Println(color.RedString("参数错误: 未指定处理名称")) fmt.Println(color.WhiteString("xiang run <处理器名称> [参数表...]")) diff --git a/config/config.go b/config/config.go index 694b764a12..1a0390805a 100644 --- a/config/config.go +++ b/config/config.go @@ -63,6 +63,7 @@ type ServiceConfig struct { type SessionConfig struct { Debug bool `json:"debug,omitempty" env:"XIANG_SESSION_DEBUG" envDefault:"false"` // DEBUG 开关 Hosting bool `json:"hosting,omitempty" env:"XIANG_SESSION_HOSTING" envDefault:"true"` // 会话服务器 + IsCLI bool `json:"iscli,omitempty" env:"XIANG_SESSION_ISCLI" envDefault:"false"` // 是否为客户端启动 Host string `json:"host,omitempty" env:"XIANG_SESSION_HOST" envDefault:"127.0.0.1"` // 会话服务器IP Port int `json:"port,omitempty" env:"XIANG_SESSION_PORT" envDefault:"3322"` // 会话服务器端口 } diff --git a/engine/load.go b/engine/load.go index c01221befc..8d947d3294 100644 --- a/engine/load.go +++ b/engine/load.go @@ -23,7 +23,8 @@ import ( // Load 根据配置加载 API, FLow, Model, Plugin func Load(cfg config.Config) { - share.DBConnect(cfg.Database) // 创建数据库连接 + share.DBConnect(cfg.Database) // 创建数据库连接 + share.SessionConnect(cfg.Session) // 创建会话服务器链接 app.Load(cfg) // 加载应用信息 LoadEngine(cfg.Path) diff --git a/service/service.go b/service/service.go index 256740f1a9..11cf8d8b8d 100644 --- a/service/service.go +++ b/service/service.go @@ -3,6 +3,7 @@ package service import ( "github.com/yaoapp/gou" "github.com/yaoapp/xiang/config" + "github.com/yaoapp/xiang/share" ) var shutdown = make(chan bool) @@ -10,6 +11,11 @@ var shutdownComplete = make(chan bool) // Start 启动服务 func Start() { + + if config.Conf.Session.Hosting && config.Conf.Session.IsCLI == false { + share.SessionServerStart() + } + gou.SetHTTPGuards(Guards) gou.ServeHTTP( gou.Server{ @@ -28,6 +34,7 @@ func Start() { func Stop(onComplete func()) { shutdown <- true <-shutdownComplete + share.SessionServerStop() gou.KillPlugins() onComplete() } diff --git a/share/const.go b/share/const.go index b952d861de..e662b95b19 100644 --- a/share/const.go +++ b/share/const.go @@ -1,7 +1,7 @@ package share // VERSION 版本号 -const VERSION = "0.9.10" +const VERSION = "0.9.11" // DOMAIN 许可域(废弃) const DOMAIN = "*.iqka.com" diff --git a/share/session.go b/share/session.go index a69ec23059..328fb4ce5c 100644 --- a/share/session.go +++ b/share/session.go @@ -15,15 +15,13 @@ import ( "github.com/yaoapp/xiang/config" ) +var sessServer *olric.Olric + // SessionConnect 加载会话信息 -func SessionConnect() { - if config.Conf.Session.Hosting { - SessionServer() - return - } +func SessionConnect(conf config.SessionConfig) { var clientConfig = &client.Config{ - Servers: []string{fmt.Sprintf("%s:%d", config.Conf.Session.Host, config.Conf.Session.Port)}, + Servers: []string{fmt.Sprintf("%s:%d", conf.Host, conf.Port)}, Serializer: serializer.NewMsgpackSerializer(), Client: config_olric.NewClient(), } @@ -37,8 +35,15 @@ func SessionConnect() { session.MemoryUse(session.ClientDMap{DMap: dm}) } -// SessionServer 启动会话服务器 -func SessionServer() { +// SessionServerStop 关闭会话服务器 +func SessionServerStop() { + if sessServer != nil { + sessServer.Shutdown(context.Background()) + } +} + +// SessionServerStart 启动会话服务器 +func SessionServerStart() { c := config_olric.New("local") c.BindAddr = config.Conf.Session.Host @@ -51,20 +56,21 @@ func SessionServer() { // log.Println("[INFO] Olric is ready to accept connections") } - db, err := olric.New(c) + var err error + sessServer, err = olric.New(c) if err != nil { log.Fatalf("Failed to create Olric instance: %v", err) } go func() { - err = db.Start() // Call Start at background. It's a blocker call. + err = sessServer.Start() // Call Start at background. It's a blocker call. if err != nil { log.Fatalf("olric.Start returned an error: %v", err) } }() <-ctx.Done() - dm, err := db.NewDMap("local-session") + dm, err := sessServer.NewDMap("local-session") if err != nil { log.Fatalf("olric.NewDMap returned an error: %v", err) }