mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-06 04:15:46 +08:00
rpc mw
This commit is contained in:
parent
37d04c9fa0
commit
d80cf34f26
@ -5,10 +5,11 @@ import (
|
|||||||
"OpenIM/pkg/common/cmd"
|
"OpenIM/pkg/common/cmd"
|
||||||
"OpenIM/pkg/common/config"
|
"OpenIM/pkg/common/config"
|
||||||
"OpenIM/pkg/common/log"
|
"OpenIM/pkg/common/log"
|
||||||
|
"OpenIM/pkg/common/mw"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/OpenIMSDK/openKeeper"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"OpenIM/pkg/common/constant"
|
"OpenIM/pkg/common/constant"
|
||||||
@ -38,14 +39,19 @@ func run(configFolderPath string, port int) error {
|
|||||||
if port == 0 {
|
if port == 0 {
|
||||||
port = config.Config.Api.GinPort[0]
|
port = config.Config.Api.GinPort[0]
|
||||||
}
|
}
|
||||||
|
zk, err := openKeeper.NewClient(nil, "", 10, "", "")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
log.NewPrivateLog(constant.LogFileName)
|
log.NewPrivateLog(constant.LogFileName)
|
||||||
router := api.NewGinRouter()
|
zk.AddOption(mw.GrpcClient())
|
||||||
|
router := api.NewGinRouter(zk)
|
||||||
address := constant.LocalHost + ":" + strconv.Itoa(port)
|
address := constant.LocalHost + ":" + strconv.Itoa(port)
|
||||||
if config.Config.Api.ListenIP != "" {
|
if config.Config.Api.ListenIP != "" {
|
||||||
address = config.Config.Api.ListenIP + ":" + strconv.Itoa(port)
|
address = config.Config.Api.ListenIP + ":" + strconv.Itoa(port)
|
||||||
}
|
}
|
||||||
fmt.Println("start api server, address: ", address, ", OpenIM version: ", constant.CurrentVersion)
|
fmt.Println("start api server, address: ", address, ", OpenIM version: ", constant.CurrentVersion)
|
||||||
err := router.Run(address)
|
err = router.Run(address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("", "api run failed ", address, err.Error())
|
log.Error("", "api run failed ", address, err.Error())
|
||||||
return err
|
return err
|
||||||
|
2
go.mod
2
go.mod
@ -4,7 +4,7 @@ go 1.18
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
firebase.google.com/go v3.13.0+incompatible
|
firebase.google.com/go v3.13.0+incompatible
|
||||||
github.com/OpenIMSDK/openKeeper v0.0.2
|
github.com/OpenIMSDK/openKeeper v0.0.3
|
||||||
github.com/OpenIMSDK/open_utils v1.0.8
|
github.com/OpenIMSDK/open_utils v1.0.8
|
||||||
github.com/Shopify/sarama v1.32.0
|
github.com/Shopify/sarama v1.32.0
|
||||||
github.com/antonfisher/nested-logrus-formatter v1.3.1
|
github.com/antonfisher/nested-logrus-formatter v1.3.1
|
||||||
|
@ -20,7 +20,7 @@ type Auth struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (o *Auth) client() (auth.AuthClient, error) {
|
func (o *Auth) client() (auth.AuthClient, error) {
|
||||||
conn, err := o.zk.GetDefaultConn(config.Config.RpcRegisterName.OpenImAuthName)
|
conn, err := o.zk.GetConn(config.Config.RpcRegisterName.OpenImAuthName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ type Conversation struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (o *Conversation) client() (conversation.ConversationClient, error) {
|
func (o *Conversation) client() (conversation.ConversationClient, error) {
|
||||||
conn, err := o.zk.GetDefaultConn(config.Config.RpcRegisterName.OpenImConversationName)
|
conn, err := o.zk.GetConn(config.Config.RpcRegisterName.OpenImConversationName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ type Friend struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (o *Friend) client() (friend.FriendClient, error) {
|
func (o *Friend) client() (friend.FriendClient, error) {
|
||||||
conn, err := o.zk.GetDefaultConn(config.Config.RpcRegisterName.OpenImFriendName)
|
conn, err := o.zk.GetConn(config.Config.RpcRegisterName.OpenImFriendName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ type Group struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (o *Group) client() (group.GroupClient, error) {
|
func (o *Group) client() (group.GroupClient, error) {
|
||||||
conn, err := o.zk.GetDefaultConn(config.Config.RpcRegisterName.OpenImGroupName)
|
conn, err := o.zk.GetConn(config.Config.RpcRegisterName.OpenImGroupName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -107,7 +107,7 @@ func newUserSendMsgReq(params *apistruct.ManagementSendMsgReq) *msg.SendMsgReq {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (o *Msg) client() (msg.MsgClient, error) {
|
func (o *Msg) client() (msg.MsgClient, error) {
|
||||||
conn, err := o.zk.GetDefaultConn(config.Config.RpcRegisterName.OpenImMsgName)
|
conn, err := o.zk.GetConn(config.Config.RpcRegisterName.OpenImMsgName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -7,13 +7,11 @@ import (
|
|||||||
"OpenIM/pkg/common/prome"
|
"OpenIM/pkg/common/prome"
|
||||||
"github.com/OpenIMSDK/openKeeper"
|
"github.com/OpenIMSDK/openKeeper"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"google.golang.org/grpc"
|
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewGinRouter() *gin.Engine {
|
func NewGinRouter(zk *openKeeper.ZkClient) *gin.Engine {
|
||||||
openKeeper.DefaultOptions = []grpc.DialOption{mw.GrpcClient()} // 默认RPC中间件
|
|
||||||
gin.SetMode(gin.ReleaseMode)
|
gin.SetMode(gin.ReleaseMode)
|
||||||
f, _ := os.Create("../logs/api.log")
|
f, _ := os.Create("../logs/api.log")
|
||||||
gin.DefaultWriter = io.MultiWriter(f)
|
gin.DefaultWriter = io.MultiWriter(f)
|
||||||
@ -28,8 +26,7 @@ func NewGinRouter() *gin.Engine {
|
|||||||
r.Use(prome.PrometheusMiddleware)
|
r.Use(prome.PrometheusMiddleware)
|
||||||
r.GET("/metrics", prome.PrometheusHandler())
|
r.GET("/metrics", prome.PrometheusHandler())
|
||||||
}
|
}
|
||||||
|
zk.AddOption(mw.GrpcClient()) // 默认RPC中间件
|
||||||
var zk *openKeeper.ZkClient
|
|
||||||
|
|
||||||
userRouterGroup := r.Group("/user")
|
userRouterGroup := r.Group("/user")
|
||||||
{
|
{
|
||||||
|
@ -20,7 +20,7 @@ type Third struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (o *Third) client() (third.ThirdClient, error) {
|
func (o *Third) client() (third.ThirdClient, error) {
|
||||||
conn, err := o.zk.GetDefaultConn(config.Config.RpcRegisterName.OpenImThirdName)
|
conn, err := o.zk.GetConn(config.Config.RpcRegisterName.OpenImThirdName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ type User struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (o *User) client() (user.UserClient, error) {
|
func (o *User) client() (user.UserClient, error) {
|
||||||
conn, err := o.zk.GetDefaultConn(config.Config.RpcRegisterName.OpenImUserName)
|
conn, err := o.zk.GetConn(config.Config.RpcRegisterName.OpenImUserName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -1,89 +0,0 @@
|
|||||||
package constant
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
type ErrInfo struct {
|
|
||||||
ErrCode int32
|
|
||||||
ErrMsg string
|
|
||||||
DetailErrMsg string
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewErrInfo(code int32, msg, detail string) *ErrInfo {
|
|
||||||
return &ErrInfo{
|
|
||||||
ErrCode: code,
|
|
||||||
ErrMsg: msg,
|
|
||||||
DetailErrMsg: detail,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *ErrInfo) Error() string {
|
|
||||||
return "errMsg: " + e.ErrMsg + " detail errMsg: " + e.DetailErrMsg
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *ErrInfo) Code() int32 {
|
|
||||||
return e.ErrCode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *ErrInfo) Msg() string {
|
|
||||||
return e.ErrMsg
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *ErrInfo) Detail() string {
|
|
||||||
return e.DetailErrMsg
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *ErrInfo) Wrap(msg ...string) error {
|
|
||||||
return errors.Wrap(e, strings.Join(msg, "--"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewErrNetwork(err error) error {
|
|
||||||
return toDetail(err, ErrNetwork)
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewErrData(err error) error {
|
|
||||||
return toDetail(err, ErrData)
|
|
||||||
}
|
|
||||||
|
|
||||||
func toDetail(err error, info *ErrInfo) *ErrInfo {
|
|
||||||
errInfo := *info
|
|
||||||
errInfo.DetailErrMsg = err.Error()
|
|
||||||
return &errInfo
|
|
||||||
}
|
|
||||||
|
|
||||||
func ToAPIErrWithErr(err error) *ErrInfo {
|
|
||||||
return &ErrInfo{}
|
|
||||||
//unwrap := utils.Unwrap(err)
|
|
||||||
//if unwrap == gorm.ErrRecordNotFound {
|
|
||||||
// return &ErrInfo{
|
|
||||||
// ErrCode: ErrRecordNotFound.Code(),
|
|
||||||
// ErrMsg: ErrRecordNotFound.Msg(),
|
|
||||||
// DetailErrMsg: fmt.Sprintf("%+v", err),
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
//if errInfo, ok := unwrap.(*ErrInfo); ok {
|
|
||||||
// return &ErrInfo{
|
|
||||||
// ErrCode: errInfo.Code(),
|
|
||||||
// ErrMsg: errInfo.Msg(),
|
|
||||||
// DetailErrMsg: fmt.Sprintf("%+v", err),
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//errComm := errors.New("")
|
|
||||||
//var marshalErr *json.MarshalerError
|
|
||||||
//errInfo := &ErrInfo{}
|
|
||||||
//switch {
|
|
||||||
//case errors.As(err, &errComm):
|
|
||||||
// if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
||||||
// return toDetail(err, ErrRecordNotFound)
|
|
||||||
// }
|
|
||||||
// return toDetail(err, ErrData)
|
|
||||||
//case errors.As(err, &marshalErr):
|
|
||||||
// return toDetail(err, ErrData)
|
|
||||||
//case errors.As(err, &errInfo):
|
|
||||||
// return toDetail(err, errInfo)
|
|
||||||
//}
|
|
||||||
//return toDetail(err, ErrDefaultOther)
|
|
||||||
}
|
|
@ -9,7 +9,6 @@ package http
|
|||||||
import (
|
import (
|
||||||
"OpenIM/pkg/callbackstruct"
|
"OpenIM/pkg/callbackstruct"
|
||||||
"OpenIM/pkg/common/config"
|
"OpenIM/pkg/common/config"
|
||||||
"OpenIM/pkg/common/constant"
|
|
||||||
"OpenIM/pkg/errs"
|
"OpenIM/pkg/errs"
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@ -79,13 +78,13 @@ func callBackPostReturn(url, command string, input interface{}, output callbacks
|
|||||||
if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue {
|
if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue {
|
||||||
return errs.ErrCallbackContinue
|
return errs.ErrCallbackContinue
|
||||||
}
|
}
|
||||||
return constant.NewErrNetwork(err)
|
return errs.ErrNetwork.Wrap(err.Error())
|
||||||
}
|
}
|
||||||
if err = json.Unmarshal(b, output); err != nil {
|
if err = json.Unmarshal(b, output); err != nil {
|
||||||
if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue {
|
if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue {
|
||||||
return errs.ErrCallbackContinue
|
return errs.ErrCallbackContinue
|
||||||
}
|
}
|
||||||
return constant.NewErrData(err)
|
return errs.ErrData.Wrap(err.Error())
|
||||||
}
|
}
|
||||||
return output.Parse()
|
return output.Parse()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user