+ FreePort Helper & Auto Session port

This commit is contained in:
Max 2022-02-10 00:06:07 +08:00
parent 54a53ca82f
commit 1056defb31
8 changed files with 54 additions and 4 deletions

View file

@ -91,7 +91,7 @@ jobs:
with:
src: ./dist/release/*
host: ${{ secrets.SSH_HOST }}
remote: /data/down/
remote: /data/release/ui/
port: ${{ secrets.SSH_PORT }}
user: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_KEY }}

View file

@ -45,6 +45,7 @@ var langs = map[string]string{
"NEXT:": "下一步:",
"Listening": " 监听",
"✨LISTENING✨": "✨服务正在运行✨",
"SessionPort": "会话服务端口",
}
// L 多语言切换

View file

@ -78,6 +78,7 @@ var startCmd = &cobra.Command{
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("SessionPort")), color.GreenString(" %d", share.SessionPort))
fmt.Println(color.WhiteString(L("Listening")), color.GreenString(" %s:%d", config.Conf.Host, config.Conf.Port))
fmt.Println("")

View file

@ -38,3 +38,25 @@ func IP() map[string]string {
func ProcessIP(process *gou.Process) interface{} {
return IP()
}
// FreePort xiang.network.FreePort 获取可用端口
func FreePort() int {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
exception.New("获取可用端口失败 %s", 500, err.Error()).Throw()
return 0
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
exception.New("获取可用端口失败 %s", 500, err.Error()).Throw()
return 0
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port
}
// ProcessFreePort xiang.network.FreePort 获取可用端口
func ProcessFreePort(process *gou.Process) interface{} {
return FreePort()
}

View file

@ -18,3 +18,15 @@ func TestProcessIP(t *testing.T) {
assert.True(t, ok)
assert.True(t, len(resp) > 0)
}
func TestFreePort(t *testing.T) {
port := FreePort()
assert.True(t, port > 0)
}
func TestProcessFreePort(t *testing.T) {
res := gou.NewProcess("xiang.network.FreePort").Run()
port, ok := res.(int)
assert.True(t, ok)
assert.True(t, port > 0)
}

View file

@ -5,6 +5,7 @@ import "github.com/yaoapp/gou"
func init() {
// 注册处理器
gou.RegisterProcessHandler("xiang.network.ip", ProcessIP)
gou.RegisterProcessHandler("xiang.network.FreePort", ProcessFreePort)
gou.RegisterProcessHandler("xiang.network.Get", ProcessGet)
gou.RegisterProcessHandler("xiang.network.Post", ProcessPost)
gou.RegisterProcessHandler("xiang.network.PostJSON", ProcessPostJSON)

View file

@ -1,5 +1,8 @@
package share
// Logs
// (session -> freeport)
// VERSION 版本号
const VERSION = "0.9.0"

View file

@ -15,15 +15,24 @@ import (
"github.com/yaoapp/gou/session"
"github.com/yaoapp/kun/exception"
"github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/network"
)
var sessServer *olric.Olric
// SessionPort Session 端口
var SessionPort int
func init() {
SessionPort = network.FreePort()
klog.Trace("session port: %d", SessionPort)
}
// SessionConnect 加载会话信息
func SessionConnect(conf config.SessionConfig) {
var clientConfig = &client.Config{
Servers: []string{fmt.Sprintf("%s:%d", conf.Host, conf.Port)},
Servers: []string{fmt.Sprintf("%s:%d", "127.0.0.1", SessionPort)},
Serializer: serializer.NewMsgpackSerializer(),
Client: config_olric.NewClient(),
}
@ -46,9 +55,10 @@ func SessionServerStop() {
// SessionServerStart 启动会话服务器
func SessionServerStart() {
c := &config_olric.Config{
BindAddr: config.Conf.Session.Host,
BindPort: config.Conf.Session.Port,
BindAddr: "127.0.0.1",
BindPort: SessionPort,
ReadRepair: false,
ReplicaCount: 1,
WriteQuorum: 1,