mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-12-03 02:42:19 +08:00
feat: optimize tim white prom code return err
This commit is contained in:
parent
8a7d27eae8
commit
683d3d2202
@ -67,6 +67,7 @@ type LocationElem struct {
|
||||
Longitude float64 `mapstructure:"longitude" validate:"required"`
|
||||
Latitude float64 `mapstructure:"latitude" validate:"required"`
|
||||
}
|
||||
|
||||
type CustomElem struct {
|
||||
Data string `mapstructure:"data" validate:"required"`
|
||||
Description string `mapstructure:"description"`
|
||||
|
||||
@ -44,7 +44,7 @@ func CheckAccessV3(ctx context.Context, ownerUserID string) (err error) {
|
||||
if opUserID == ownerUserID {
|
||||
return nil
|
||||
}
|
||||
return errs.ErrNoPermission.Wrap(utils.GetSelfFuncName())
|
||||
return errs.Wrap(errs.ErrNoPermission, "CheckAccessV3: no permission for user "+opUserID)
|
||||
}
|
||||
|
||||
func IsAppManagerUid(ctx context.Context) bool {
|
||||
@ -61,6 +61,7 @@ func CheckAdmin(ctx context.Context) error {
|
||||
}
|
||||
return errs.ErrNoPermission.Wrap(fmt.Sprintf("user %s is not admin userID", mcontext.GetOpUserID(ctx)))
|
||||
}
|
||||
|
||||
func CheckIMAdmin(ctx context.Context) error {
|
||||
if utils.IsContain(mcontext.GetOpUserID(ctx), config.Config.IMAdmin.UserID) {
|
||||
return nil
|
||||
|
||||
@ -38,11 +38,11 @@ func (a *ApiCmd) AddApi(f func(port int, promPort int) error) {
|
||||
}
|
||||
}
|
||||
|
||||
func (a *ApiCmd) GetPortFromConfig(portType string) int {
|
||||
func (a *ApiCmd) GetPortFromConfig(portType string) (int, error) {
|
||||
if portType == constant.FlagPort {
|
||||
return config2.Config.Api.OpenImApiPort[0]
|
||||
return config2.Config.Api.OpenImApiPort[0], nil
|
||||
} else if portType == constant.FlagPrometheusPort {
|
||||
return config2.Config.Prometheus.ApiPrometheusPort[0]
|
||||
return config2.Config.Prometheus.ApiPrometheusPort[0], nil
|
||||
}
|
||||
return 0
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
@ -15,14 +15,12 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/openimsdk/open-im-server/v3/internal/msggateway"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/OpenIMSDK/protocol/constant"
|
||||
|
||||
"github.com/openimsdk/open-im-server/v3/internal/msggateway"
|
||||
v3config "github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||||
"github.com/OpenIMSDK/tools/errs"
|
||||
)
|
||||
|
||||
type MsgGatewayCmd struct {
|
||||
@ -39,40 +37,31 @@ func (m *MsgGatewayCmd) AddWsPortFlag() {
|
||||
m.Command.Flags().IntP(constant.FlagWsPort, "w", 0, "ws server listen port")
|
||||
}
|
||||
|
||||
func (m *MsgGatewayCmd) getWsPortFlag(cmd *cobra.Command) int {
|
||||
func (m *MsgGatewayCmd) getWsPortFlag(cmd *cobra.Command) (int, error) {
|
||||
port, err := cmd.Flags().GetInt(constant.FlagWsPort)
|
||||
if err != nil {
|
||||
log.Println("Error getting ws port flag:", err)
|
||||
return 0, errs.Wrap(err, "error getting ws port flag")
|
||||
}
|
||||
if port == 0 {
|
||||
port = m.PortFromConfig(constant.FlagWsPort)
|
||||
port, _ = m.PortFromConfig(constant.FlagWsPort)
|
||||
}
|
||||
return port
|
||||
return port, nil
|
||||
}
|
||||
|
||||
func (m *MsgGatewayCmd) addRunE() {
|
||||
m.Command.RunE = func(cmd *cobra.Command, args []string) error {
|
||||
return msggateway.RunWsAndServer(m.getPortFlag(cmd), m.getWsPortFlag(cmd), m.getPrometheusPortFlag(cmd))
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MsgGatewayCmd) Exec() error {
|
||||
m.addRunE()
|
||||
return m.Execute()
|
||||
}
|
||||
|
||||
func (m *MsgGatewayCmd) GetPortFromConfig(portType string) int {
|
||||
switch portType {
|
||||
case constant.FlagWsPort:
|
||||
return v3config.Config.LongConnSvr.OpenImWsPort[0]
|
||||
|
||||
case constant.FlagPort:
|
||||
return v3config.Config.LongConnSvr.OpenImMessageGatewayPort[0]
|
||||
|
||||
case constant.FlagPrometheusPort:
|
||||
return v3config.Config.Prometheus.MessageGatewayPrometheusPort[0]
|
||||
|
||||
default:
|
||||
return 0
|
||||
wsPort, err := m.getWsPortFlag(cmd)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to get WS port flag")
|
||||
}
|
||||
port, err := m.getPortFlag(cmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
prometheusPort, err := m.getPrometheusPortFlag(cmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return msggateway.RunWsAndServer(port, wsPort, prometheusPort)
|
||||
}
|
||||
}
|
||||
|
||||
@ -18,17 +18,19 @@ import (
|
||||
"fmt"
|
||||
|
||||
config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/OpenIMSDK/protocol/constant"
|
||||
"github.com/OpenIMSDK/tools/errs"
|
||||
"github.com/OpenIMSDK/tools/log"
|
||||
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||||
)
|
||||
|
||||
type RootCmdPt interface {
|
||||
GetPortFromConfig(portType string) int
|
||||
GetPortFromConfig(portType string) (int, error)
|
||||
}
|
||||
|
||||
type RootCmd struct {
|
||||
@ -131,32 +133,42 @@ func (r *RootCmd) AddPortFlag() {
|
||||
r.Command.Flags().IntP(constant.FlagPort, "p", 0, "server listen port")
|
||||
}
|
||||
|
||||
func (r *RootCmd) getPortFlag(cmd *cobra.Command) int {
|
||||
func (r *RootCmd) getPortFlag(cmd *cobra.Command) (int, error) {
|
||||
port, err := cmd.Flags().GetInt(constant.FlagPort)
|
||||
if err != nil {
|
||||
fmt.Println("Error getting ws port flag:", err)
|
||||
// Wrapping the error with additional context
|
||||
return 0, errors.Wrap(err, "error getting port flag")
|
||||
}
|
||||
if port == 0 {
|
||||
port = r.PortFromConfig(constant.FlagPort)
|
||||
// port, err := r.PortFromConfig(constant.FlagPort)
|
||||
// if err != nil {
|
||||
// // Optionally wrap the error if it's an internal error needing context
|
||||
// return 0, errors.Wrap(err, "error getting port from config")
|
||||
// }
|
||||
}
|
||||
return port
|
||||
return port, nil
|
||||
}
|
||||
|
||||
// GetPortFlag returns the port flag
|
||||
func (r *RootCmd) GetPortFlag() int {
|
||||
return r.port
|
||||
func (r *RootCmd) GetPortFlag() (int, error) {
|
||||
|
||||
if portFlag == 0 {
|
||||
return errs.Wrap(errors.New("port is 0"), "error getting port flag")
|
||||
}
|
||||
return r.port, nil
|
||||
}
|
||||
|
||||
func (r *RootCmd) AddPrometheusPortFlag() {
|
||||
r.Command.Flags().IntP(constant.FlagPrometheusPort, "", 0, "server prometheus listen port")
|
||||
}
|
||||
|
||||
func (r *RootCmd) getPrometheusPortFlag(cmd *cobra.Command) int {
|
||||
func (r *RootCmd) getPrometheusPortFlag(cmd *cobra.Command) (int, error) {
|
||||
port, _ := cmd.Flags().GetInt(constant.FlagPrometheusPort)
|
||||
if port == 0 {
|
||||
port = r.PortFromConfig(constant.FlagPrometheusPort)
|
||||
}
|
||||
return port
|
||||
return port, nil
|
||||
}
|
||||
|
||||
func (r *RootCmd) GetPrometheusPortFlag() int {
|
||||
@ -177,10 +189,12 @@ func (r *RootCmd) AddCommand(cmds ...*cobra.Command) {
|
||||
r.Command.AddCommand(cmds...)
|
||||
}
|
||||
|
||||
func (r *RootCmd) GetPortFromConfig(portType string) int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (r *RootCmd) PortFromConfig(portType string) int {
|
||||
return r.cmdItf.GetPortFromConfig(portType)
|
||||
func (r *RootCmd)
|
||||
PortFromConfig(portType string) (int, error) {
|
||||
// Retrieve the port and cache it
|
||||
port, err := r.cmdItf.GetPortFromConfig(portType)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return port, nil
|
||||
}
|
||||
|
||||
@ -16,11 +16,14 @@ package cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/OpenIMSDK/protocol/constant"
|
||||
"github.com/spf13/cobra"
|
||||
"google.golang.org/grpc"
|
||||
|
||||
"github.com/OpenIMSDK/tools/errs"
|
||||
|
||||
config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||||
|
||||
"github.com/OpenIMSDK/tools/discoveryregistry"
|
||||
@ -40,77 +43,81 @@ func NewRpcCmd(name string) *RpcCmd {
|
||||
|
||||
func (a *RpcCmd) Exec() error {
|
||||
a.Command.Run = func(cmd *cobra.Command, args []string) {
|
||||
a.port = a.getPortFlag(cmd)
|
||||
a.prometheusPort = a.getPrometheusPortFlag(cmd)
|
||||
portFlag, err := a.getPortFlag(cmd)
|
||||
if err != nil {
|
||||
a.port = portFlag
|
||||
}
|
||||
var prometheusPort, err = a.getPrometheusPortFlag(cmd)
|
||||
|
||||
if err != nil {
|
||||
return errs.Wrap(err, "Failed to get Prometheus port")
|
||||
}
|
||||
|
||||
a.prometheusPort = prometheusPort
|
||||
}
|
||||
return a.Execute()
|
||||
}
|
||||
|
||||
func (a *RpcCmd) StartSvr(name string, rpcFn func(discov discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) error) error {
|
||||
if a.GetPortFlag() == 0 {
|
||||
return errors.New("port is required")
|
||||
portFlag, err := a.GetPortFlag()
|
||||
if err != nil {
|
||||
return errs.Wrap(err, "error getting port flag")
|
||||
} else {
|
||||
a.port = portFlag
|
||||
}
|
||||
|
||||
portFlag, err = a.GetPortFlag()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return startrpc.Start(a.GetPortFlag(), name, a.GetPrometheusPortFlag(), rpcFn)
|
||||
}
|
||||
|
||||
func (a *RpcCmd) GetPortFromConfig(portType string) int {
|
||||
switch a.Name {
|
||||
case RpcPushServer:
|
||||
if portType == constant.FlagPort {
|
||||
return config2.Config.RpcPort.OpenImPushPort[0]
|
||||
func (a *RpcCmd) GetPortFromConfig(portType string) (int, error) {
|
||||
portConfigMap := map[string]map[string]int{
|
||||
RpcPushServer: {
|
||||
constant.FlagPort: config2.Config.RpcPort.OpenImPushPort[0],
|
||||
constant.FlagPrometheusPort: config2.Config.Prometheus.PushPrometheusPort[0],
|
||||
},
|
||||
RpcAuthServer: {
|
||||
constant.FlagPort: config2.Config.RpcPort.OpenImAuthPort[0],
|
||||
constant.FlagPrometheusPort: config2.Config.Prometheus.AuthPrometheusPort[0],
|
||||
},
|
||||
RpcConversationServer: {
|
||||
constant.FlagPort: config2.Config.RpcPort.OpenImConversationPort[0],
|
||||
constant.FlagPrometheusPort: config2.Config.Prometheus.ConversationPrometheusPort[0],
|
||||
},
|
||||
RpcFriendServer: {
|
||||
constant.FlagPort: config2.Config.RpcPort.OpenImFriendPort[0],
|
||||
constant.FlagPrometheusPort: config2.Config.Prometheus.FriendPrometheusPort[0],
|
||||
},
|
||||
RpcGroupServer: {
|
||||
constant.FlagPort: config2.Config.RpcPort.OpenImGroupPort[0],
|
||||
constant.FlagPrometheusPort: config2.Config.Prometheus.GroupPrometheusPort[0],
|
||||
},
|
||||
RpcMsgServer: {
|
||||
constant.FlagPort: config2.Config.RpcPort.OpenImMessagePort[0],
|
||||
constant.FlagPrometheusPort: config2.Config.Prometheus.MessagePrometheusPort[0],
|
||||
},
|
||||
RpcThirdServer: {
|
||||
constant.FlagPort: config2.Config.RpcPort.OpenImThirdPort[0],
|
||||
constant.FlagPrometheusPort: config2.Config.Prometheus.ThirdPrometheusPort[0],
|
||||
},
|
||||
RpcUserServer: {
|
||||
constant.FlagPort: config2.Config.RpcPort.OpenImUserPort[0],
|
||||
constant.FlagPrometheusPort: config2.Config.Prometheus.UserPrometheusPort[0],
|
||||
},
|
||||
}
|
||||
if portType == constant.FlagPrometheusPort {
|
||||
return config2.Config.Prometheus.PushPrometheusPort[0]
|
||||
}
|
||||
case RpcAuthServer:
|
||||
if portType == constant.FlagPort {
|
||||
return config2.Config.RpcPort.OpenImAuthPort[0]
|
||||
}
|
||||
if portType == constant.FlagPrometheusPort {
|
||||
return config2.Config.Prometheus.AuthPrometheusPort[0]
|
||||
}
|
||||
case RpcConversationServer:
|
||||
if portType == constant.FlagPort {
|
||||
return config2.Config.RpcPort.OpenImConversationPort[0]
|
||||
}
|
||||
if portType == constant.FlagPrometheusPort {
|
||||
return config2.Config.Prometheus.ConversationPrometheusPort[0]
|
||||
}
|
||||
case RpcFriendServer:
|
||||
if portType == constant.FlagPort {
|
||||
return config2.Config.RpcPort.OpenImFriendPort[0]
|
||||
}
|
||||
if portType == constant.FlagPrometheusPort {
|
||||
return config2.Config.Prometheus.FriendPrometheusPort[0]
|
||||
}
|
||||
case RpcGroupServer:
|
||||
if portType == constant.FlagPort {
|
||||
return config2.Config.RpcPort.OpenImGroupPort[0]
|
||||
}
|
||||
if portType == constant.FlagPrometheusPort {
|
||||
return config2.Config.Prometheus.GroupPrometheusPort[0]
|
||||
}
|
||||
case RpcMsgServer:
|
||||
if portType == constant.FlagPort {
|
||||
return config2.Config.RpcPort.OpenImMessagePort[0]
|
||||
}
|
||||
if portType == constant.FlagPrometheusPort {
|
||||
return config2.Config.Prometheus.MessagePrometheusPort[0]
|
||||
}
|
||||
case RpcThirdServer:
|
||||
if portType == constant.FlagPort {
|
||||
return config2.Config.RpcPort.OpenImThirdPort[0]
|
||||
}
|
||||
if portType == constant.FlagPrometheusPort {
|
||||
return config2.Config.Prometheus.ThirdPrometheusPort[0]
|
||||
}
|
||||
case RpcUserServer:
|
||||
if portType == constant.FlagPort {
|
||||
return config2.Config.RpcPort.OpenImUserPort[0]
|
||||
}
|
||||
if portType == constant.FlagPrometheusPort {
|
||||
return config2.Config.Prometheus.UserPrometheusPort[0]
|
||||
|
||||
if portMap, ok := portConfigMap[a.Name]; ok {
|
||||
if port, ok := portMap[portType]; ok {
|
||||
return port, nil
|
||||
} else {
|
||||
return 0, errs.Wrap(errs.New("port type '%s' not found", portType), fmt.Sprintf("Failed to get port for %s", a.Name))
|
||||
}
|
||||
}
|
||||
return 0
|
||||
|
||||
return 0, errs.Wrap(fmt.Errorf("server name '%s' not found", a.Name), "Failed to get port configuration")
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user