From 56f0cc74891e8c617d66cddbd1cdb86ffb38e0ac Mon Sep 17 00:00:00 2001 From: Gordon <46924906+FGadvancer@users.noreply.github.com> Date: Tue, 19 Mar 2024 15:10:09 +0800 Subject: [PATCH] refactor: unified naming for module startup functions. --- go.sum | 4 ++-- internal/push/offlinepush/fcm/push.go | 14 +++++++++----- internal/push/push_rpc_server.go | 5 ++++- internal/push/push_to_client.go | 6 +++--- pkg/common/cmd/root.go | 4 +--- pkg/common/config/parse.go | 26 ++++++++++++++------------ 6 files changed, 33 insertions(+), 26 deletions(-) diff --git a/go.sum b/go.sum index e11121048..48634af45 100644 --- a/go.sum +++ b/go.sum @@ -20,8 +20,8 @@ github.com/IBM/sarama v1.42.2 h1:VoY4hVIZ+WQJ8G9KNY/SQlWguBQXQ9uvFPOnrcu8hEw= github.com/IBM/sarama v1.42.2/go.mod h1:FLPGUGwYqEs62hq2bVG6Io2+5n+pS6s/WOXVKWSLFtE= github.com/OpenIMSDK/protocol v0.0.56 h1:mbVFyDBachEsmJLfYW5AU1z2KL8AUEpoHG8RPCIxjgg= github.com/OpenIMSDK/protocol v0.0.56/go.mod h1:F25dFrwrIx3lkNoiuf6FkCfxuwf8L4Z8UIsdTHP/r0Y= -github.com/OpenIMSDK/tools v0.0.46-alpha.5 h1:jFirsgpSIT1G0ue0li7EiS6d4P2hp4rWc4DVBH+all4= -github.com/OpenIMSDK/tools v0.0.46-alpha.5/go.mod h1:kGgLtr1egGJnnGaBsF7SNcylx7KB4Md5MyRqz+GUMS0= +github.com/OpenIMSDK/tools v0.0.46-alpha.6 h1:QJ+IQh8s+Z7K7xx5wnjVU1VZY0qNy+zvqxGpUWOADl8= +github.com/OpenIMSDK/tools v0.0.46-alpha.6/go.mod h1:kGgLtr1egGJnnGaBsF7SNcylx7KB4Md5MyRqz+GUMS0= github.com/QcloudApi/qcloud_sign_golang v0.0.0-20141224014652-e4130a326409/go.mod h1:1pk82RBxDY/JZnPQrtqHlUFfCctgdorsd9M06fMynOM= github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 h1:uSoVVbwJiQipAclBbw+8quDsfcvFjOpI5iCf4p/cqCs= github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs= diff --git a/internal/push/offlinepush/fcm/push.go b/internal/push/offlinepush/fcm/push.go index b192bf139..1581c6996 100644 --- a/internal/push/offlinepush/fcm/push.go +++ b/internal/push/offlinepush/fcm/push.go @@ -16,6 +16,7 @@ package fcm import ( "context" + "github.com/OpenIMSDK/tools/errs" "path/filepath" firebase "firebase.google.com/go" @@ -39,21 +40,24 @@ type Fcm struct { // NewClient initializes a new FCM client using the Firebase Admin SDK. // It requires the FCM service account credentials file located within the project's configuration directory. -func NewClient(pushConf *config.Push, cache cache.MsgModel) *Fcm { - projectRoot := config.GetProjectRoot() +func NewClient(pushConf *config.Push, cache cache.MsgModel) (*Fcm, error) { + projectRoot, err := config.GetProjectRoot() + if err != nil { + return nil, err + } credentialsFilePath := filepath.Join(projectRoot, "config", pushConf.Fcm.ServiceAccount) opt := option.WithCredentialsFile(credentialsFilePath) fcmApp, err := firebase.NewApp(context.Background(), nil, opt) if err != nil { - return nil + return nil, errs.Wrap(err) } ctx := context.Background() fcmMsgClient, err := fcmApp.Messaging(ctx) if err != nil { - return nil + return nil, errs.Wrap(err) } - return &Fcm{fcmMsgCli: fcmMsgClient, cache: cache} + return &Fcm{fcmMsgCli: fcmMsgClient, cache: cache}, nil } func (f *Fcm) Push(ctx context.Context, userIDs []string, title, content string, opts *offlinepush.Opts) error { diff --git a/internal/push/push_rpc_server.go b/internal/push/push_rpc_server.go index ebfb47660..23a20f9d9 100644 --- a/internal/push/push_rpc_server.go +++ b/internal/push/push_rpc_server.go @@ -40,7 +40,10 @@ func Start(config *config.GlobalConfig, client discoveryregistry.SvcDiscoveryReg return err } cacheModel := cache.NewMsgCacheModel(rdb, config.MsgCacheTimeout, &config.Redis) - offlinePusher := NewOfflinePusher(&config.Push, &config.IOSPush, cacheModel) + offlinePusher, err := NewOfflinePusher(&config.Push, &config.IOSPush, cacheModel) + if err != nil { + return err + } database := controller.NewPushDatabase(cacheModel) groupRpcClient := rpcclient.NewGroupRpcClient(client, config.RpcRegisterName.OpenImGroupName) conversationRpcClient := rpcclient.NewConversationRpcClient(client, config.RpcRegisterName.OpenImConversationName) diff --git a/internal/push/push_to_client.go b/internal/push/push_to_client.go index d4e01c8db..ee65a419a 100644 --- a/internal/push/push_to_client.go +++ b/internal/push/push_to_client.go @@ -75,19 +75,19 @@ func NewPusher(config *config.GlobalConfig, discov discoveryregistry.SvcDiscover } } -func NewOfflinePusher(pushConf *config.Push, iOSPushConf *config.IOSPush, cache cache.MsgModel) offlinepush.OfflinePusher { +func NewOfflinePusher(pushConf *config.Push, iOSPushConf *config.IOSPush, cache cache.MsgModel) (offlinepush.OfflinePusher, error) { var offlinePusher offlinepush.OfflinePusher switch pushConf.Enable { case "getui": offlinePusher = getui.NewClient(pushConf, cache) case "fcm": - offlinePusher = fcm.NewClient(pushConf, cache) + return fcm.NewClient(pushConf, cache) case "jpush": offlinePusher = jpush.NewClient(pushConf, iOSPushConf) default: offlinePusher = dummy.NewClient() } - return offlinePusher + return offlinePusher, nil } func (p *Pusher) DeleteMemberAndSetConversationSeq(ctx context.Context, groupID string, userIDs []string) error { diff --git a/pkg/common/cmd/root.go b/pkg/common/cmd/root.go index 694c03cc6..603c0bfd0 100644 --- a/pkg/common/cmd/root.go +++ b/pkg/common/cmd/root.go @@ -62,7 +62,6 @@ func WithLogName(logName string) func(*CmdOpts) { func NewRootCmd(processName, name string, opts ...func(*CmdOpts)) *RootCmd { rootCmd := &RootCmd{processName: processName, Name: name, config: config.NewGlobalConfig()} cmd := cobra.Command{ - Use: "Start openIM application", Short: fmt.Sprintf(`Start %s `, name), Long: fmt.Sprintf(`Start %s `, name), PersistentPreRunE: func(cmd *cobra.Command, args []string) error { @@ -76,7 +75,7 @@ func NewRootCmd(processName, name string, opts ...func(*CmdOpts)) *RootCmd { func (rc *RootCmd) persistentPreRun(cmd *cobra.Command, opts ...func(*CmdOpts)) error { if err := rc.initializeConfiguration(cmd); err != nil { - return fmt.Errorf("failed to get configuration from command: %w", err) + return err } cmdOpts := rc.applyOptions(opts...) @@ -178,7 +177,6 @@ func (r *RootCmd) GetPrometheusPortFlag() int { func (r *RootCmd) getConfFromCmdAndInit(cmdLines *cobra.Command) error { configFolderPath, _ := cmdLines.Flags().GetString(constant.FlagConf) - fmt.Println("The directory of the configuration file to start the process:", configFolderPath) return config2.InitConfig(r.config, configFolderPath) } diff --git a/pkg/common/config/parse.go b/pkg/common/config/parse.go index 841eaed04..d3d233057 100644 --- a/pkg/common/config/parse.go +++ b/pkg/common/config/parse.go @@ -16,7 +16,6 @@ package config import ( _ "embed" - "fmt" "os" "path/filepath" @@ -41,28 +40,27 @@ const ( func GetDefaultConfigPath() (string, error) { executablePath, err := os.Executable() if err != nil { - fmt.Println("GetDefaultConfigPath error:", err.Error()) - return "", nil + return "", errs.WrapMsg(err, "failed to get executable path") } configPath, err := genutil.OutDir(filepath.Join(filepath.Dir(executablePath), "../config/")) if err != nil { - fmt.Fprintf(os.Stderr, "failed to get output directory: %v\n", err) - os.Exit(1) + return "", errs.WrapMsg(err, "failed to get output directory", "outDir", filepath.Join(filepath.Dir(executablePath), "../config/")) } return configPath, nil } // getProjectRoot returns the absolute path of the project root directory. -func GetProjectRoot() string { - executablePath, _ := os.Executable() - +func GetProjectRoot() (string, error) { + executablePath, err := os.Executable() + if err != nil { + return "", errs.Wrap(err) + } projectRoot, err := genutil.OutDir(filepath.Join(filepath.Dir(executablePath), "../../../../..")) if err != nil { - fmt.Fprintf(os.Stderr, "failed to get output directory: %v\n", err) - os.Exit(1) + return "", errs.Wrap(err) } - return projectRoot + return projectRoot, nil } func GetOptionsByNotification(cfg NotificationConf) msgprocessor.Options { @@ -94,7 +92,11 @@ func initConfig(config any, configName, configFolderPath string) error { if !os.IsNotExist(err) { return errs.WrapMsg(err, "stat config path error", "config Folder Path", configFolderPath) } - configFolderPath = filepath.Join(GetProjectRoot(), "config", configName) + path, err := GetProjectRoot() + if err != nil { + return err + } + configFolderPath = filepath.Join(path, "config", configName) } data, err := os.ReadFile(configFolderPath) if err != nil {