Skip to content

Commit

Permalink
v0.9.10 优化 process 增加 会话服务器
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Nov 28, 2021
1 parent 622c9a9 commit 1fc21be
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 48 deletions.
9 changes: 9 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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 开关
Expand Down
52 changes: 26 additions & 26 deletions data/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions helper/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 打印语句
Expand Down
4 changes: 4 additions & 0 deletions network/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
40 changes: 28 additions & 12 deletions helper/request.go → network/request.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package helper
package network

import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"strings"
Expand All @@ -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))
}
}

Expand All @@ -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)
Expand Down
20 changes: 19 additions & 1 deletion helper/request.process.go → network/request.process.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package helper
package network

import (
"fmt"
Expand All @@ -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)
Expand Down
Loading

0 comments on commit 1fc21be

Please sign in to comment.