From 1fc21beeee28e151c18af297db83702e56ee6660 Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 29 Nov 2021 00:27:42 +0800 Subject: [PATCH] =?UTF-8?q?v0.9.10=20=E4=BC=98=E5=8C=96=20process=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20=E4=BC=9A=E8=AF=9D=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.go | 9 ++++ data/bindata.go | 52 +++++++++--------- helper/process.go | 4 -- network/process.go | 4 ++ {helper => network}/request.go | 40 +++++++++----- {helper => network}/request.process.go | 20 ++++++- {helper => network}/request_test.go | 21 ++++++-- share/const.go | 2 +- share/session.go | 73 ++++++++++++++++++++++++++ 9 files changed, 177 insertions(+), 48 deletions(-) rename {helper => network}/request.go (74%) rename {helper => network}/request.process.go (76%) rename {helper => network}/request_test.go (67%) create mode 100644 share/session.go diff --git a/config/config.go b/config/config.go index 468063dba7..694b764a12 100644 --- a/config/config.go +++ b/config/config.go @@ -20,6 +20,7 @@ var Conf Config type Config struct { XiangConfig Service ServiceConfig `json:"service,omitempty"` + Session SessionConfig `json:"session,omitempty"` Database DatabaseConfig `json:"database,omitempty"` JWT JWTConfig `json:"jwt,omitempty"` Log LogConfig `json:"log,omitempty"` @@ -58,6 +59,14 @@ type ServiceConfig struct { Port int `json:"port,omitempty" env:"XIANG_SERVICE_PORT" envDefault:"5099"` // 服务监听端口 } +// SessionConfig 会话服务器 +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"` // 会话服务器 + 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"` // 会话服务器端口 +} + // DatabaseConfig 数据库配置 type DatabaseConfig struct { Debug bool `json:"debug,omitempty" env:"XIANG_DB_DEBUG" envDefault:"false"` // DEBUG 开关 diff --git a/data/bindata.go b/data/bindata.go index 9e4719bc1c..01c56ff3af 100644 --- a/data/bindata.go +++ b/data/bindata.go @@ -204,7 +204,7 @@ func uiIndexHtml() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "ui/index.html", size: 12, mode: os.FileMode(420), modTime: time.Unix(1638111968, 0)} + info := bindataFileInfo{name: "ui/index.html", size: 12, mode: os.FileMode(420), modTime: time.Unix(1638115948, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -224,7 +224,7 @@ func xiangDs_store() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/.DS_Store", size: 6148, mode: os.FileMode(420), modTime: time.Unix(1638111968, 0)} + info := bindataFileInfo{name: "xiang/.DS_Store", size: 6148, mode: os.FileMode(420), modTime: time.Unix(1638115948, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -244,7 +244,7 @@ func xiangApisReadmeMd() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/apis/README.md", size: 13, mode: os.FileMode(420), modTime: time.Unix(1638111968, 0)} + info := bindataFileInfo{name: "xiang/apis/README.md", size: 13, mode: os.FileMode(420), modTime: time.Unix(1638115948, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -264,7 +264,7 @@ func xiangApisChartHttpJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/apis/chart.http.json", size: 624, mode: os.FileMode(420), modTime: time.Unix(1638111968, 0)} + info := bindataFileInfo{name: "xiang/apis/chart.http.json", size: 624, mode: os.FileMode(420), modTime: time.Unix(1638115948, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -284,7 +284,7 @@ func xiangApisPageHttpJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/apis/page.http.json", size: 609, mode: os.FileMode(420), modTime: time.Unix(1638111968, 0)} + info := bindataFileInfo{name: "xiang/apis/page.http.json", size: 609, mode: os.FileMode(420), modTime: time.Unix(1638115948, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -304,7 +304,7 @@ func xiangApisStorageHttpJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/apis/storage.http.json", size: 1092, mode: os.FileMode(420), modTime: time.Unix(1638111968, 0)} + info := bindataFileInfo{name: "xiang/apis/storage.http.json", size: 1092, mode: os.FileMode(420), modTime: time.Unix(1638115948, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -324,7 +324,7 @@ func xiangApisTableHttpJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/apis/table.http.json", size: 2632, mode: os.FileMode(420), modTime: time.Unix(1638111968, 0)} + info := bindataFileInfo{name: "xiang/apis/table.http.json", size: 2632, mode: os.FileMode(420), modTime: time.Unix(1638115948, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -344,7 +344,7 @@ func xiangApisUserHttpJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/apis/user.http.json", size: 1453, mode: os.FileMode(420), modTime: time.Unix(1638111968, 0)} + info := bindataFileInfo{name: "xiang/apis/user.http.json", size: 1453, mode: os.FileMode(420), modTime: time.Unix(1638115948, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -364,7 +364,7 @@ func xiangApisXiangHttpJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/apis/xiang.http.json", size: 1239, mode: os.FileMode(420), modTime: time.Unix(1638111968, 0)} + info := bindataFileInfo{name: "xiang/apis/xiang.http.json", size: 1239, mode: os.FileMode(420), modTime: time.Unix(1638115948, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -384,7 +384,7 @@ func xiangDataAppJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/data/app.json", size: 245, mode: os.FileMode(420), modTime: time.Unix(1638111968, 0)} + info := bindataFileInfo{name: "xiang/data/app.json", size: 245, mode: os.FileMode(420), modTime: time.Unix(1638115948, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -404,7 +404,7 @@ func xiangDataIconsDs_store() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/data/icons/.DS_Store", size: 6148, mode: os.FileMode(420), modTime: time.Unix(1638111968, 0)} + info := bindataFileInfo{name: "xiang/data/icons/.DS_Store", size: 6148, mode: os.FileMode(420), modTime: time.Unix(1638115948, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -424,7 +424,7 @@ func xiangDataIcons404Png() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/data/icons/404.png", size: 9342, mode: os.FileMode(420), modTime: time.Unix(1638111968, 0)} + info := bindataFileInfo{name: "xiang/data/icons/404.png", size: 9342, mode: os.FileMode(420), modTime: time.Unix(1638115948, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -444,7 +444,7 @@ func xiangDataIconsIconIcns() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/data/icons/icon.icns", size: 75636, mode: os.FileMode(420), modTime: time.Unix(1638111968, 0)} + info := bindataFileInfo{name: "xiang/data/icons/icon.icns", size: 75636, mode: os.FileMode(420), modTime: time.Unix(1638115948, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -464,7 +464,7 @@ func xiangDataIconsIconIco() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/data/icons/icon.ico", size: 31310, mode: os.FileMode(420), modTime: time.Unix(1638111968, 0)} + info := bindataFileInfo{name: "xiang/data/icons/icon.ico", size: 31310, mode: os.FileMode(420), modTime: time.Unix(1638115948, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -484,7 +484,7 @@ func xiangDataIconsIconPng() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/data/icons/icon.png", size: 22420, mode: os.FileMode(420), modTime: time.Unix(1638111968, 0)} + info := bindataFileInfo{name: "xiang/data/icons/icon.png", size: 22420, mode: os.FileMode(420), modTime: time.Unix(1638115948, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -504,7 +504,7 @@ func xiangDataIndexHtml() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/data/index.html", size: 302, mode: os.FileMode(420), modTime: time.Unix(1638111968, 0)} + info := bindataFileInfo{name: "xiang/data/index.html", size: 302, mode: os.FileMode(420), modTime: time.Unix(1638115948, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -524,7 +524,7 @@ func xiangFlowsReadmeMd() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/flows/README.md", size: 12, mode: os.FileMode(420), modTime: time.Unix(1638111968, 0)} + info := bindataFileInfo{name: "xiang/flows/README.md", size: 12, mode: os.FileMode(420), modTime: time.Unix(1638115948, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -544,7 +544,7 @@ func xiangFlowsMenuFlowJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/flows/menu.flow.json", size: 1045, mode: os.FileMode(420), modTime: time.Unix(1638111968, 0)} + info := bindataFileInfo{name: "xiang/flows/menu.flow.json", size: 1045, mode: os.FileMode(420), modTime: time.Unix(1638115948, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -564,7 +564,7 @@ func xiangFlowsTableGetFlowJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/flows/table/get.flow.json", size: 541, mode: os.FileMode(420), modTime: time.Unix(1638111968, 0)} + info := bindataFileInfo{name: "xiang/flows/table/get.flow.json", size: 541, mode: os.FileMode(420), modTime: time.Unix(1638115948, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -584,7 +584,7 @@ func xiangModelsReadmeMd() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/models/README.md", size: 15, mode: os.FileMode(420), modTime: time.Unix(1638111968, 0)} + info := bindataFileInfo{name: "xiang/models/README.md", size: 15, mode: os.FileMode(420), modTime: time.Unix(1638115948, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -604,7 +604,7 @@ func xiangModelsMenuJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/models/menu.json", size: 2378, mode: os.FileMode(420), modTime: time.Unix(1638111968, 0)} + info := bindataFileInfo{name: "xiang/models/menu.json", size: 2378, mode: os.FileMode(420), modTime: time.Unix(1638115948, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -624,7 +624,7 @@ func xiangModelsUserJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/models/user.json", size: 8826, mode: os.FileMode(420), modTime: time.Unix(1638111968, 0)} + info := bindataFileInfo{name: "xiang/models/user.json", size: 8826, mode: os.FileMode(420), modTime: time.Unix(1638115948, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -644,7 +644,7 @@ func xiangModelsWorkflowModJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/models/workflow.mod.json", size: 3333, mode: os.FileMode(420), modTime: time.Unix(1638111968, 0)} + info := bindataFileInfo{name: "xiang/models/workflow.mod.json", size: 3333, mode: os.FileMode(420), modTime: time.Unix(1638115948, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -664,7 +664,7 @@ func xiangTablesReadmeMd() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/tables/README.md", size: 15, mode: os.FileMode(420), modTime: time.Unix(1638111968, 0)} + info := bindataFileInfo{name: "xiang/tables/README.md", size: 15, mode: os.FileMode(420), modTime: time.Unix(1638115948, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -684,7 +684,7 @@ func xiangTablesMenuJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/tables/menu.json", size: 10382, mode: os.FileMode(420), modTime: time.Unix(1638111968, 0)} + info := bindataFileInfo{name: "xiang/tables/menu.json", size: 10382, mode: os.FileMode(420), modTime: time.Unix(1638115948, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -704,7 +704,7 @@ func xiangTablesUserJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/tables/user.json", size: 1422, mode: os.FileMode(420), modTime: time.Unix(1638111968, 0)} + info := bindataFileInfo{name: "xiang/tables/user.json", size: 1422, mode: os.FileMode(420), modTime: time.Unix(1638115948, 0)} a := &asset{bytes: bytes, info: info} return a, nil } diff --git a/helper/process.go b/helper/process.go index 45e02d1057..10fee0ca43 100644 --- a/helper/process.go +++ b/helper/process.go @@ -33,10 +33,6 @@ func init() { gou.RegisterProcessHandler("xiang.helper.Print", ProcessPrint) - gou.RegisterProcessHandler("xiang.helper.Get", ProcessGet) - gou.RegisterProcessHandler("xiang.helper.Post", ProcessPost) - gou.RegisterProcessHandler("xiang.helper.Send", ProcessSend) - } // ProcessPrint xiang.helper.Print 打印语句 diff --git a/network/process.go b/network/process.go index b79cb3715a..416edd8da3 100644 --- a/network/process.go +++ b/network/process.go @@ -5,4 +5,8 @@ import "github.com/yaoapp/gou" func init() { // 注册处理器 gou.RegisterProcessHandler("xiang.network.ip", ProcessIP) + gou.RegisterProcessHandler("xiang.network.Get", ProcessGet) + gou.RegisterProcessHandler("xiang.network.Post", ProcessPost) + gou.RegisterProcessHandler("xiang.network.PostJSON", ProcessPostJSON) + gou.RegisterProcessHandler("xiang.network.Send", ProcessSend) } diff --git a/helper/request.go b/network/request.go similarity index 74% rename from helper/request.go rename to network/request.go index ebb647b73a..0a3f9e476e 100644 --- a/helper/request.go +++ b/network/request.go @@ -1,7 +1,8 @@ -package helper +package network import ( "bytes" + "fmt" "io/ioutil" "net/http" "strings" @@ -27,22 +28,35 @@ func RequestPost(url string, data interface{}, headers map[string]string) Respon return RequestSend("POST", url, map[string]interface{}{}, data, headers) } +// RequestPostJSON 发送POST请求 +func RequestPostJSON(url string, data interface{}, headers map[string]string) Response { + if headers == nil { + headers = map[string]string{} + } + headers["content-type"] = "application/json;charset=utf8" + return RequestSend("POST", url, map[string]interface{}{}, data, headers) +} + // RequestSend 发送Request请求 func RequestSend(method string, url string, params map[string]interface{}, data interface{}, headers map[string]string) Response { var body []byte var err error if data != nil { - body, err = jsoniter.Marshal(data) - if err != nil { - return Response{ - Status: 500, - Body: err.Error(), - Data: map[string]interface{}{"code": 500, "message": err.Error()}, - Headers: map[string]interface{}{ - "Content-Type": "application/json;charset=utf8", - }, + if strings.HasPrefix(strings.ToLower(headers["content-type"]), "application/json") { + body, err = jsoniter.Marshal(data) + if err != nil { + return Response{ + Status: 500, + Body: err.Error(), + Data: map[string]interface{}{"code": 500, "message": err.Error()}, + Headers: map[string]interface{}{ + "Content-Type": "application/json;charset=utf8", + }, + } } + } else { + body = []byte(fmt.Sprintf("%v", data)) } } @@ -59,8 +73,10 @@ func RequestSend(method string, url string, params map[string]interface{}, data } // Request Header - for name, header := range headers { - req.Header.Set(name, header) + if headers != nil { + for name, header := range headers { + req.Header.Set(name, header) + } } resp, err := (&http.Client{}).Do(req) diff --git a/helper/request.process.go b/network/request.process.go similarity index 76% rename from helper/request.process.go rename to network/request.process.go index 681fbdec76..8bdcfcfecc 100644 --- a/helper/request.process.go +++ b/network/request.process.go @@ -1,4 +1,4 @@ -package helper +package network import ( "fmt" @@ -25,6 +25,24 @@ func ProcessPost(process *gou.Process) interface{} { return RequestPost(url, data, headers) } +// ProcessPostJSON xiang.helper.PostJSON HTTP Post +func ProcessPostJSON(process *gou.Process) interface{} { + process.ValidateArgNums(1) + var data interface{} + headers := map[string]string{} + url := process.ArgsString(0) + if process.NumOfArgs() > 1 { + data = process.Args[1] + } + if process.NumOfArgs() > 2 { + inputHeaders := process.ArgsMap(2) + for name, value := range inputHeaders { + headers[name] = fmt.Sprintf("%v", value) + } + } + return RequestPostJSON(url, data, headers) +} + // ProcessSend xiang.helper.Send HTTP Send func ProcessSend(process *gou.Process) interface{} { process.ValidateArgNums(2) diff --git a/helper/request_test.go b/network/request_test.go similarity index 67% rename from helper/request_test.go rename to network/request_test.go index 2ca9149c0c..9e0d3ab6c4 100644 --- a/helper/request_test.go +++ b/network/request_test.go @@ -1,4 +1,4 @@ -package helper +package network import ( "testing" @@ -26,9 +26,14 @@ func TestRequestPost(t *testing.T) { assert.Equal(t, 404, resp.Status) } +func TestRequestPostJSON(t *testing.T) { + resp := RequestPostJSON("https://api.github.com/zen", map[string]interface{}{"foo": "bar"}, nil) + assert.Equal(t, 404, resp.Status) +} + func TestRequestProcessGet(t *testing.T) { args := []interface{}{"https://api.github.com/zen"} - res := gou.NewProcess("xiang.helper.Get", args...).Run() + res := gou.NewProcess("xiang.network.Get", args...).Run() resp, ok := res.(Response) assert.True(t, ok) assert.Equal(t, 200, resp.Status) @@ -37,7 +42,15 @@ func TestRequestProcessGet(t *testing.T) { func TestRequestProcessPost(t *testing.T) { args := []interface{}{"https://api.github.com/zen", map[string]interface{}{"foo": "bar"}, nil} - res := gou.NewProcess("xiang.helper.Post", args...).Run() + res := gou.NewProcess("xiang.network.Post", args...).Run() + resp, ok := res.(Response) + assert.True(t, ok) + assert.Equal(t, 404, resp.Status) +} + +func TestRequestProcessPostJSON(t *testing.T) { + args := []interface{}{"https://api.github.com/zen", map[string]interface{}{"foo": "bar"}, nil} + res := gou.NewProcess("xiang.network.PostJSON", args...).Run() resp, ok := res.(Response) assert.True(t, ok) assert.Equal(t, 404, resp.Status) @@ -45,7 +58,7 @@ func TestRequestProcessPost(t *testing.T) { func TestRequestProcessSend(t *testing.T) { args := []interface{}{"GET", "https://api.github.com/zen"} - res := gou.NewProcess("xiang.helper.Send", args...).Run() + res := gou.NewProcess("xiang.network.Send", args...).Run() resp, ok := res.(Response) assert.True(t, ok) assert.Equal(t, 200, resp.Status) diff --git a/share/const.go b/share/const.go index a3e19a1458..b952d861de 100644 --- a/share/const.go +++ b/share/const.go @@ -1,7 +1,7 @@ package share // VERSION 版本号 -const VERSION = "0.9.8" +const VERSION = "0.9.10" // DOMAIN 许可域(废弃) const DOMAIN = "*.iqka.com" diff --git a/share/session.go b/share/session.go new file mode 100644 index 0000000000..a69ec23059 --- /dev/null +++ b/share/session.go @@ -0,0 +1,73 @@ +package share + +import ( + "context" + "fmt" + "io/ioutil" + "log" + + "github.com/buraksezer/olric" + "github.com/buraksezer/olric/client" + config_olric "github.com/buraksezer/olric/config" + "github.com/buraksezer/olric/serializer" + "github.com/yaoapp/gou/session" + "github.com/yaoapp/kun/exception" + "github.com/yaoapp/xiang/config" +) + +// SessionConnect 加载会话信息 +func SessionConnect() { + if config.Conf.Session.Hosting { + SessionServer() + return + } + + var clientConfig = &client.Config{ + Servers: []string{fmt.Sprintf("%s:%d", config.Conf.Session.Host, config.Conf.Session.Port)}, + Serializer: serializer.NewMsgpackSerializer(), + Client: config_olric.NewClient(), + } + + c, err := client.New(clientConfig) + if err != nil { + exception.New("会话服务器连接失败 %s", 500, err.Error()).Throw() + } + + dm := c.NewDMap("local-session") + session.MemoryUse(session.ClientDMap{DMap: dm}) +} + +// SessionServer 启动会话服务器 +func SessionServer() { + + c := config_olric.New("local") + c.BindAddr = config.Conf.Session.Host + c.BindPort = config.Conf.Session.Port + + c.Logger.SetOutput(ioutil.Discard) // 暂时关闭日志 + ctx, cancel := context.WithCancel(context.Background()) + c.Started = func() { + defer cancel() + // log.Println("[INFO] Olric is ready to accept connections") + } + + db, 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. + if err != nil { + log.Fatalf("olric.Start returned an error: %v", err) + } + }() + + <-ctx.Done() + dm, err := db.NewDMap("local-session") + if err != nil { + log.Fatalf("olric.NewDMap returned an error: %v", err) + } + + session.MemoryUse(session.ServerDMap{DMap: dm}) +}