1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-05 03:23:49 +08:00

feat: split out server register

we can add server with less dependency
This commit is contained in:
wdvxdr 2021-10-02 23:42:09 +08:00
parent 4b99f64b56
commit ce944539c1
No known key found for this signature in database
GPG Key ID: 703F8C071DE7A1B6
7 changed files with 109 additions and 61 deletions

47
main.go
View File

@ -19,6 +19,7 @@ import (
"golang.org/x/term" "golang.org/x/term"
_ "github.com/Mrs4s/go-cqhttp/modules/mime" // mime检查模块 _ "github.com/Mrs4s/go-cqhttp/modules/mime" // mime检查模块
"github.com/Mrs4s/go-cqhttp/modules/servers"
_ "github.com/Mrs4s/go-cqhttp/modules/silk" // silk编码模块 _ "github.com/Mrs4s/go-cqhttp/modules/silk" // silk编码模块
"github.com/Mrs4s/go-cqhttp/coolq" "github.com/Mrs4s/go-cqhttp/coolq"
@ -26,7 +27,6 @@ import (
"github.com/Mrs4s/go-cqhttp/global/terminal" "github.com/Mrs4s/go-cqhttp/global/terminal"
"github.com/Mrs4s/go-cqhttp/internal/base" "github.com/Mrs4s/go-cqhttp/internal/base"
"github.com/Mrs4s/go-cqhttp/internal/selfupdate" "github.com/Mrs4s/go-cqhttp/internal/selfupdate"
"github.com/Mrs4s/go-cqhttp/modules/config"
"github.com/Mrs4s/go-cqhttp/server" "github.com/Mrs4s/go-cqhttp/server"
) )
@ -325,49 +325,8 @@ func main() {
base.Account.Status = 0 base.Account.Status = 0
} }
cli.SetOnlineStatus(allowStatus[base.Account.Status]) cli.SetOnlineStatus(allowStatus[base.Account.Status])
bot := coolq.NewQQBot(cli)
for _, m := range base.Servers { servers.Run(coolq.NewQQBot(cli))
if h, ok := m["http"]; ok {
hc := new(config.HTTPServer)
if err := h.Decode(hc); err != nil {
log.Warn("读取http配置失败 :", err)
} else if !hc.Disabled {
go server.RunHTTPServerAndClients(bot, hc)
}
}
if s, ok := m["ws"]; ok {
sc := new(config.WebsocketServer)
if err := s.Decode(sc); err != nil {
log.Warn("读取正向Websocket配置失败 :", err)
} else if !sc.Disabled {
go server.RunWebSocketServer(bot, sc)
}
}
if c, ok := m["ws-reverse"]; ok {
rc := new(config.WebsocketReverse)
if err := c.Decode(rc); err != nil {
log.Warn("读取反向Websocket配置失败 :", err)
} else if !rc.Disabled {
go server.RunWebSocketClient(bot, rc)
}
}
if p, ok := m["pprof"]; ok {
pc := new(config.PprofServer)
if err := p.Decode(pc); err != nil {
log.Warn("读取pprof配置失败 :", err)
} else if !pc.Disabled {
go server.RunPprofServer(pc)
}
}
if p, ok := m["lambda"]; ok {
lc := new(config.LambdaServer)
if err := p.Decode(lc); err != nil {
log.Warn("读取pprof配置失败 :", err)
} else if !lc.Disabled {
go server.RunLambdaClient(bot, lc)
}
}
}
log.Info("资源初始化完成, 开始处理信息.") log.Info("资源初始化完成, 开始处理信息.")
log.Info("アトリは、高性能ですから!") log.Info("アトリは、高性能ですから!")

View File

@ -0,0 +1,30 @@
package servers
import (
"gopkg.in/yaml.v3"
"github.com/Mrs4s/go-cqhttp/coolq"
"github.com/Mrs4s/go-cqhttp/internal/base"
)
var svr = make(map[string]func(*coolq.CQBot, yaml.Node))
// Register 注册 Server
func Register(name string, proc func(*coolq.CQBot, yaml.Node)) {
_, ok := svr[name]
if ok {
panic(name + " server has existed")
}
svr[name] = proc
}
// Run 运行所有svr
func Run(bot *coolq.CQBot) {
for _, l := range base.Servers {
for name, conf := range l {
if fn, ok := svr[name]; ok {
go fn(bot, conf)
}
}
}
}

View File

@ -1,2 +1,13 @@
// Package server 包含HTTP,WebSocket,反向WebSocket请求处理的相关函数与结构体 // Package server 包含HTTP,WebSocket,反向WebSocket请求处理的相关函数与结构体
package server package server
import "github.com/Mrs4s/go-cqhttp/modules/servers"
// 注册
func init() {
servers.Register("http", runHTTP)
servers.Register("ws", runWSServer)
servers.Register("ws-reverse", runWSClient)
servers.Register("pprof", runPprof)
servers.Register("lambda", runLambda)
}

View File

@ -19,6 +19,7 @@ import (
"github.com/Mrs4s/MiraiGo/utils" "github.com/Mrs4s/MiraiGo/utils"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
"gopkg.in/yaml.v3"
"github.com/Mrs4s/go-cqhttp/coolq" "github.com/Mrs4s/go-cqhttp/coolq"
"github.com/Mrs4s/go-cqhttp/modules/config" "github.com/Mrs4s/go-cqhttp/modules/config"
@ -149,13 +150,19 @@ func checkAuth(req *http.Request, token string) int {
} }
} }
// RunHTTPServerAndClients 启动HTTP服务器与HTTP上报客户端 // runHTTP 启动HTTP服务器与HTTP上报客户端
func RunHTTPServerAndClients(bot *coolq.CQBot, conf *config.HTTPServer) { func runHTTP(bot *coolq.CQBot, node yaml.Node) {
var ( var conf config.HTTPServer
s = new(httpServer) switch err := node.Decode(&conf); {
addr string case err != nil:
) log.Warn("读取http配置失败 :", err)
s.accessToken = conf.AccessToken fallthrough
case conf.Disabled:
return
}
var addr string
s := &httpServer{accessToken: conf.AccessToken}
if conf.Host == "" || conf.Port == 0 { if conf.Host == "" || conf.Port == 0 {
goto client goto client
} }

View File

@ -8,12 +8,23 @@ import (
"time" "time"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
"github.com/Mrs4s/go-cqhttp/coolq"
"github.com/Mrs4s/go-cqhttp/modules/config" "github.com/Mrs4s/go-cqhttp/modules/config"
) )
// RunPprofServer 启动 pprof 性能分析服务器 // runPprof 启动 pprof 性能分析服务器
func RunPprofServer(conf *config.PprofServer) { func runPprof(_ *coolq.CQBot, node yaml.Node) {
var conf config.PprofServer
switch err := node.Decode(&conf); {
case err != nil:
log.Warn("读取pprof配置失败 :", err)
fallthrough
case conf.Disabled:
return
}
addr := fmt.Sprintf("%s:%d", conf.Host, conf.Port) addr := fmt.Sprintf("%s:%d", conf.Host, conf.Port)
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/debug/pprof/", pprof.Index) mux.HandleFunc("/debug/pprof/", pprof.Index)

View File

@ -13,6 +13,7 @@ import (
"github.com/Mrs4s/MiraiGo/utils" "github.com/Mrs4s/MiraiGo/utils"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
"github.com/Mrs4s/go-cqhttp/coolq" "github.com/Mrs4s/go-cqhttp/coolq"
"github.com/Mrs4s/go-cqhttp/global" "github.com/Mrs4s/go-cqhttp/global"
@ -77,8 +78,17 @@ func (l *lambdaResponseWriter) WriteHeader(statusCode int) {
var cli *lambdaClient var cli *lambdaClient
// RunLambdaClient type: [scf,aws] // runLambda type: [scf,aws]
func RunLambdaClient(bot *coolq.CQBot, conf *config.LambdaServer) { func runLambda(bot *coolq.CQBot, node yaml.Node) {
var conf config.LambdaServer
switch err := node.Decode(&conf); {
case err != nil:
log.Warn("读取lambda配置失败 :", err)
fallthrough
case conf.Disabled:
return
}
cli = &lambdaClient{ cli = &lambdaClient{
lambdaType: conf.Type, lambdaType: conf.Type,
client: http.Client{Timeout: 0}, client: http.Client{Timeout: 0},

View File

@ -10,6 +10,8 @@ import (
"sync" "sync"
"time" "time"
"gopkg.in/yaml.v3"
"github.com/Mrs4s/go-cqhttp/coolq" "github.com/Mrs4s/go-cqhttp/coolq"
"github.com/Mrs4s/go-cqhttp/global" "github.com/Mrs4s/go-cqhttp/global"
"github.com/Mrs4s/go-cqhttp/modules/config" "github.com/Mrs4s/go-cqhttp/modules/config"
@ -54,11 +56,20 @@ var upgrader = websocket.Upgrader{
}, },
} }
// RunWebSocketServer 运行一个正向WS server // runWSServer 运行一个正向WS server
func RunWebSocketServer(b *coolq.CQBot, conf *config.WebsocketServer) { func runWSServer(b *coolq.CQBot, node yaml.Node) {
var conf config.WebsocketServer
switch err := node.Decode(&conf); {
case err != nil:
log.Warn("读取正向Websocket配置失败 :", err)
fallthrough
case conf.Disabled:
return
}
s := &webSocketServer{ s := &webSocketServer{
bot: b, bot: b,
conf: conf, conf: &conf,
token: conf.AccessToken, token: conf.AccessToken,
filter: conf.Filter, filter: conf.Filter,
} }
@ -77,11 +88,20 @@ func RunWebSocketServer(b *coolq.CQBot, conf *config.WebsocketServer) {
}() }()
} }
// RunWebSocketClient 运行一个正向WS client // runWSClient 运行一个反向向WS client
func RunWebSocketClient(b *coolq.CQBot, conf *config.WebsocketReverse) { func runWSClient(b *coolq.CQBot, node yaml.Node) {
var conf config.WebsocketReverse
switch err := node.Decode(&conf); {
case err != nil:
log.Warn("读取反向Websocket配置失败 :", err)
fallthrough
case conf.Disabled:
return
}
c := &websocketClient{ c := &websocketClient{
bot: b, bot: b,
conf: conf, conf: &conf,
token: conf.AccessToken, token: conf.AccessToken,
filter: conf.Filter, filter: conf.Filter,
} }