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"`
|
Longitude float64 `mapstructure:"longitude" validate:"required"`
|
||||||
Latitude float64 `mapstructure:"latitude" validate:"required"`
|
Latitude float64 `mapstructure:"latitude" validate:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CustomElem struct {
|
type CustomElem struct {
|
||||||
Data string `mapstructure:"data" validate:"required"`
|
Data string `mapstructure:"data" validate:"required"`
|
||||||
Description string `mapstructure:"description"`
|
Description string `mapstructure:"description"`
|
||||||
|
|||||||
@ -44,7 +44,7 @@ func CheckAccessV3(ctx context.Context, ownerUserID string) (err error) {
|
|||||||
if opUserID == ownerUserID {
|
if opUserID == ownerUserID {
|
||||||
return nil
|
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 {
|
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)))
|
return errs.ErrNoPermission.Wrap(fmt.Sprintf("user %s is not admin userID", mcontext.GetOpUserID(ctx)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func CheckIMAdmin(ctx context.Context) error {
|
func CheckIMAdmin(ctx context.Context) error {
|
||||||
if utils.IsContain(mcontext.GetOpUserID(ctx), config.Config.IMAdmin.UserID) {
|
if utils.IsContain(mcontext.GetOpUserID(ctx), config.Config.IMAdmin.UserID) {
|
||||||
return nil
|
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 {
|
if portType == constant.FlagPort {
|
||||||
return config2.Config.Api.OpenImApiPort[0]
|
return config2.Config.Api.OpenImApiPort[0], nil
|
||||||
} else if portType == constant.FlagPrometheusPort {
|
} 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
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"github.com/openimsdk/open-im-server/v3/internal/msggateway"
|
||||||
|
"github.com/pkg/errors"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/OpenIMSDK/protocol/constant"
|
"github.com/OpenIMSDK/protocol/constant"
|
||||||
|
"github.com/OpenIMSDK/tools/errs"
|
||||||
"github.com/openimsdk/open-im-server/v3/internal/msggateway"
|
|
||||||
v3config "github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type MsgGatewayCmd struct {
|
type MsgGatewayCmd struct {
|
||||||
@ -39,40 +37,31 @@ func (m *MsgGatewayCmd) AddWsPortFlag() {
|
|||||||
m.Command.Flags().IntP(constant.FlagWsPort, "w", 0, "ws server listen port")
|
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)
|
port, err := cmd.Flags().GetInt(constant.FlagWsPort)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error getting ws port flag:", err)
|
return 0, errs.Wrap(err, "error getting ws port flag")
|
||||||
}
|
}
|
||||||
if port == 0 {
|
if port == 0 {
|
||||||
port = m.PortFromConfig(constant.FlagWsPort)
|
port, _ = m.PortFromConfig(constant.FlagWsPort)
|
||||||
}
|
}
|
||||||
return port
|
return port, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MsgGatewayCmd) addRunE() {
|
func (m *MsgGatewayCmd) addRunE() {
|
||||||
m.Command.RunE = func(cmd *cobra.Command, args []string) error {
|
m.Command.RunE = func(cmd *cobra.Command, args []string) error {
|
||||||
return msggateway.RunWsAndServer(m.getPortFlag(cmd), m.getWsPortFlag(cmd), m.getPrometheusPortFlag(cmd))
|
wsPort, err := m.getWsPortFlag(cmd)
|
||||||
}
|
if err != nil {
|
||||||
}
|
return errors.Wrap(err, "failed to get WS port flag")
|
||||||
|
}
|
||||||
func (m *MsgGatewayCmd) Exec() error {
|
port, err := m.getPortFlag(cmd)
|
||||||
m.addRunE()
|
if err != nil {
|
||||||
return m.Execute()
|
return err
|
||||||
}
|
}
|
||||||
|
prometheusPort, err := m.getPrometheusPortFlag(cmd)
|
||||||
func (m *MsgGatewayCmd) GetPortFromConfig(portType string) int {
|
if err != nil {
|
||||||
switch portType {
|
return err
|
||||||
case constant.FlagWsPort:
|
}
|
||||||
return v3config.Config.LongConnSvr.OpenImWsPort[0]
|
return msggateway.RunWsAndServer(port, wsPort, prometheusPort)
|
||||||
|
|
||||||
case constant.FlagPort:
|
|
||||||
return v3config.Config.LongConnSvr.OpenImMessageGatewayPort[0]
|
|
||||||
|
|
||||||
case constant.FlagPrometheusPort:
|
|
||||||
return v3config.Config.Prometheus.MessageGatewayPrometheusPort[0]
|
|
||||||
|
|
||||||
default:
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,17 +18,19 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/OpenIMSDK/protocol/constant"
|
"github.com/OpenIMSDK/protocol/constant"
|
||||||
|
"github.com/OpenIMSDK/tools/errs"
|
||||||
"github.com/OpenIMSDK/tools/log"
|
"github.com/OpenIMSDK/tools/log"
|
||||||
|
|
||||||
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RootCmdPt interface {
|
type RootCmdPt interface {
|
||||||
GetPortFromConfig(portType string) int
|
GetPortFromConfig(portType string) (int, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type RootCmd struct {
|
type RootCmd struct {
|
||||||
@ -131,32 +133,42 @@ func (r *RootCmd) AddPortFlag() {
|
|||||||
r.Command.Flags().IntP(constant.FlagPort, "p", 0, "server listen port")
|
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)
|
port, err := cmd.Flags().GetInt(constant.FlagPort)
|
||||||
if err != nil {
|
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 {
|
if port == 0 {
|
||||||
port = r.PortFromConfig(constant.FlagPort)
|
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
|
// GetPortFlag returns the port flag
|
||||||
func (r *RootCmd) GetPortFlag() int {
|
func (r *RootCmd) GetPortFlag() (int, error) {
|
||||||
return r.port
|
|
||||||
|
if portFlag == 0 {
|
||||||
|
return errs.Wrap(errors.New("port is 0"), "error getting port flag")
|
||||||
|
}
|
||||||
|
return r.port, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RootCmd) AddPrometheusPortFlag() {
|
func (r *RootCmd) AddPrometheusPortFlag() {
|
||||||
r.Command.Flags().IntP(constant.FlagPrometheusPort, "", 0, "server prometheus listen port")
|
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)
|
port, _ := cmd.Flags().GetInt(constant.FlagPrometheusPort)
|
||||||
if port == 0 {
|
if port == 0 {
|
||||||
port = r.PortFromConfig(constant.FlagPrometheusPort)
|
port = r.PortFromConfig(constant.FlagPrometheusPort)
|
||||||
}
|
}
|
||||||
return port
|
return port, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RootCmd) GetPrometheusPortFlag() int {
|
func (r *RootCmd) GetPrometheusPortFlag() int {
|
||||||
@ -177,10 +189,12 @@ func (r *RootCmd) AddCommand(cmds ...*cobra.Command) {
|
|||||||
r.Command.AddCommand(cmds...)
|
r.Command.AddCommand(cmds...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RootCmd) GetPortFromConfig(portType string) int {
|
func (r *RootCmd)
|
||||||
return 0
|
PortFromConfig(portType string) (int, error) {
|
||||||
}
|
// Retrieve the port and cache it
|
||||||
|
port, err := r.cmdItf.GetPortFromConfig(portType)
|
||||||
func (r *RootCmd) PortFromConfig(portType string) int {
|
if err != nil {
|
||||||
return r.cmdItf.GetPortFromConfig(portType)
|
return 0, err
|
||||||
|
}
|
||||||
|
return port, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,11 +16,14 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/OpenIMSDK/protocol/constant"
|
"github.com/OpenIMSDK/protocol/constant"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
|
|
||||||
|
"github.com/OpenIMSDK/tools/errs"
|
||||||
|
|
||||||
config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||||||
|
|
||||||
"github.com/OpenIMSDK/tools/discoveryregistry"
|
"github.com/OpenIMSDK/tools/discoveryregistry"
|
||||||
@ -40,77 +43,81 @@ func NewRpcCmd(name string) *RpcCmd {
|
|||||||
|
|
||||||
func (a *RpcCmd) Exec() error {
|
func (a *RpcCmd) Exec() error {
|
||||||
a.Command.Run = func(cmd *cobra.Command, args []string) {
|
a.Command.Run = func(cmd *cobra.Command, args []string) {
|
||||||
a.port = a.getPortFlag(cmd)
|
portFlag, err := a.getPortFlag(cmd)
|
||||||
a.prometheusPort = a.getPrometheusPortFlag(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()
|
return a.Execute()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *RpcCmd) StartSvr(name string, rpcFn func(discov discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) error) error {
|
func (a *RpcCmd) StartSvr(name string, rpcFn func(discov discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) error) error {
|
||||||
if a.GetPortFlag() == 0 {
|
portFlag, err := a.GetPortFlag()
|
||||||
return errors.New("port is required")
|
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)
|
return startrpc.Start(a.GetPortFlag(), name, a.GetPrometheusPortFlag(), rpcFn)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *RpcCmd) GetPortFromConfig(portType string) int {
|
func (a *RpcCmd) GetPortFromConfig(portType string) (int, error) {
|
||||||
switch a.Name {
|
portConfigMap := map[string]map[string]int{
|
||||||
case RpcPushServer:
|
RpcPushServer: {
|
||||||
if portType == constant.FlagPort {
|
constant.FlagPort: config2.Config.RpcPort.OpenImPushPort[0],
|
||||||
return config2.Config.RpcPort.OpenImPushPort[0]
|
constant.FlagPrometheusPort: config2.Config.Prometheus.PushPrometheusPort[0],
|
||||||
}
|
},
|
||||||
if portType == constant.FlagPrometheusPort {
|
RpcAuthServer: {
|
||||||
return config2.Config.Prometheus.PushPrometheusPort[0]
|
constant.FlagPort: config2.Config.RpcPort.OpenImAuthPort[0],
|
||||||
}
|
constant.FlagPrometheusPort: config2.Config.Prometheus.AuthPrometheusPort[0],
|
||||||
case RpcAuthServer:
|
},
|
||||||
if portType == constant.FlagPort {
|
RpcConversationServer: {
|
||||||
return config2.Config.RpcPort.OpenImAuthPort[0]
|
constant.FlagPort: config2.Config.RpcPort.OpenImConversationPort[0],
|
||||||
}
|
constant.FlagPrometheusPort: config2.Config.Prometheus.ConversationPrometheusPort[0],
|
||||||
if portType == constant.FlagPrometheusPort {
|
},
|
||||||
return config2.Config.Prometheus.AuthPrometheusPort[0]
|
RpcFriendServer: {
|
||||||
}
|
constant.FlagPort: config2.Config.RpcPort.OpenImFriendPort[0],
|
||||||
case RpcConversationServer:
|
constant.FlagPrometheusPort: config2.Config.Prometheus.FriendPrometheusPort[0],
|
||||||
if portType == constant.FlagPort {
|
},
|
||||||
return config2.Config.RpcPort.OpenImConversationPort[0]
|
RpcGroupServer: {
|
||||||
}
|
constant.FlagPort: config2.Config.RpcPort.OpenImGroupPort[0],
|
||||||
if portType == constant.FlagPrometheusPort {
|
constant.FlagPrometheusPort: config2.Config.Prometheus.GroupPrometheusPort[0],
|
||||||
return config2.Config.Prometheus.ConversationPrometheusPort[0]
|
},
|
||||||
}
|
RpcMsgServer: {
|
||||||
case RpcFriendServer:
|
constant.FlagPort: config2.Config.RpcPort.OpenImMessagePort[0],
|
||||||
if portType == constant.FlagPort {
|
constant.FlagPrometheusPort: config2.Config.Prometheus.MessagePrometheusPort[0],
|
||||||
return config2.Config.RpcPort.OpenImFriendPort[0]
|
},
|
||||||
}
|
RpcThirdServer: {
|
||||||
if portType == constant.FlagPrometheusPort {
|
constant.FlagPort: config2.Config.RpcPort.OpenImThirdPort[0],
|
||||||
return config2.Config.Prometheus.FriendPrometheusPort[0]
|
constant.FlagPrometheusPort: config2.Config.Prometheus.ThirdPrometheusPort[0],
|
||||||
}
|
},
|
||||||
case RpcGroupServer:
|
RpcUserServer: {
|
||||||
if portType == constant.FlagPort {
|
constant.FlagPort: config2.Config.RpcPort.OpenImUserPort[0],
|
||||||
return config2.Config.RpcPort.OpenImGroupPort[0]
|
constant.FlagPrometheusPort: config2.Config.Prometheus.UserPrometheusPort[0],
|
||||||
}
|
},
|
||||||
if portType == constant.FlagPrometheusPort {
|
}
|
||||||
return config2.Config.Prometheus.GroupPrometheusPort[0]
|
|
||||||
}
|
if portMap, ok := portConfigMap[a.Name]; ok {
|
||||||
case RpcMsgServer:
|
if port, ok := portMap[portType]; ok {
|
||||||
if portType == constant.FlagPort {
|
return port, nil
|
||||||
return config2.Config.RpcPort.OpenImMessagePort[0]
|
} else {
|
||||||
}
|
return 0, errs.Wrap(errs.New("port type '%s' not found", portType), fmt.Sprintf("Failed to get port for %s", a.Name))
|
||||||
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]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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