mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-12-11 23:47:32 +08:00
feat: add openim flag api configpath env set
Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com>
This commit is contained in:
parent
f72e41207f
commit
4618eda6fd
@ -60,6 +60,8 @@ func run(port int) error {
|
|||||||
logger.Info(context.Background(), "api start init discov client")
|
logger.Info(context.Background(), "api start init discov client")
|
||||||
|
|
||||||
var client discoveryregistry.SvcDiscoveryRegistry
|
var client discoveryregistry.SvcDiscoveryRegistry
|
||||||
|
|
||||||
|
// Determine whether zk is passed according to whether it is a clustered deployment
|
||||||
client, err = discovery_register.NewDiscoveryRegister(config.Config.Envs.Discovery)
|
client, err = discovery_register.NewDiscoveryRegister(config.Config.Envs.Discovery)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error(context.Background(), "Failed to initialize discovery register", err)
|
logger.Error(context.Background(), "Failed to initialize discovery register", err)
|
||||||
|
|||||||
@ -28,6 +28,7 @@ type ApiCmd struct {
|
|||||||
func NewApiCmd() *ApiCmd {
|
func NewApiCmd() *ApiCmd {
|
||||||
ret := &ApiCmd{NewRootCmd("api")}
|
ret := &ApiCmd{NewRootCmd("api")}
|
||||||
ret.SetRootCmdPt(ret)
|
ret.SetRootCmdPt(ret)
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,11 +37,13 @@ func (a *ApiCmd) AddApi(f func(port int) error) {
|
|||||||
return f(a.getPortFlag(cmd))
|
return f(a.getPortFlag(cmd))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ApiCmd) GetPortFromConfig(portType string) int {
|
func (a *ApiCmd) GetPortFromConfig(portType string) int {
|
||||||
fmt.Println("GetPortFromConfig:", portType)
|
fmt.Println("GetPortFromConfig:", portType)
|
||||||
if portType == constant.FlagPort {
|
if portType == constant.FlagPort {
|
||||||
return config2.Config.Api.OpenImApiPort[0]
|
return config2.Config.Api.OpenImApiPort[0]
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -53,38 +53,50 @@ func WithLogName(logName string) func(*CmdOpts) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRootCmd(name string, opts ...func(*CmdOpts)) (rootCmd *RootCmd) {
|
func NewRootCmd(name string, opts ...func(*CmdOpts)) *RootCmd {
|
||||||
rootCmd = &RootCmd{Name: name}
|
rootCmd := &RootCmd{Name: name}
|
||||||
c := cobra.Command{
|
cmd := cobra.Command{
|
||||||
Use: "start openIM application",
|
Use: "Start openIM application",
|
||||||
Short: fmt.Sprintf(`Start %s `, name),
|
Short: fmt.Sprintf(`Start %s `, name),
|
||||||
Long: fmt.Sprintf(`Start %s `, name),
|
Long: fmt.Sprintf(`Start %s `, name),
|
||||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
if err := rootCmd.getConfFromCmdAndInit(cmd); err != nil {
|
return rootCmd.persistentPreRun(cmd, opts...)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
cmdOpts := &CmdOpts{}
|
|
||||||
for _, opt := range opts {
|
|
||||||
opt(cmdOpts)
|
|
||||||
}
|
|
||||||
if cmdOpts.loggerPrefixName == "" {
|
|
||||||
cmdOpts.loggerPrefixName = "OpenIM.log.all"
|
|
||||||
}
|
|
||||||
if err := log.InitFromConfig(cmdOpts.loggerPrefixName, name, config.Config.Log.RemainLogLevel, config.Config.Log.IsStdout, config.Config.Log.IsJson, config.Config.Log.StorageLocation, config.Config.Log.RemainRotationCount, config.Config.Log.RotationTime); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
rootCmd.Command = c
|
rootCmd.Command = cmd
|
||||||
rootCmd.addConfFlag()
|
rootCmd.addConfFlag()
|
||||||
return rootCmd
|
return rootCmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (rc *RootCmd) persistentPreRun(cmd *cobra.Command, opts ...func(*CmdOpts)) error {
|
||||||
|
if err := rc.getConfFromCmdAndInit(cmd); err != nil {
|
||||||
|
return fmt.Errorf("failed to get configuration from command: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cmdOpts := defaultCmdOpts()
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(cmdOpts)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := log.InitFromConfig(cmdOpts.loggerPrefixName, rc.Name, config.Config.Log.RemainLogLevel, config.Config.Log.IsStdout, config.Config.Log.IsJson, config.Config.Log.StorageLocation, config.Config.Log.RemainRotationCount, config.Config.Log.RotationTime); err != nil {
|
||||||
|
return fmt.Errorf("failed to initialize from config: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func defaultCmdOpts() *CmdOpts {
|
||||||
|
return &CmdOpts{
|
||||||
|
loggerPrefixName: "OpenIM.log.all",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (r *RootCmd) SetRootCmdPt(cmdItf RootCmdPt) {
|
func (r *RootCmd) SetRootCmdPt(cmdItf RootCmdPt) {
|
||||||
r.cmdItf = cmdItf
|
r.cmdItf = cmdItf
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RootCmd) addConfFlag() {
|
func (r *RootCmd) addConfFlag() {
|
||||||
r.Command.Flags().StringP(constant.FlagConf, "c", "", "Path to config file folder")
|
r.Command.Flags().StringP(constant.FlagConf, "c", "", "path to config file folder")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RootCmd) AddPortFlag() {
|
func (r *RootCmd) AddPortFlag() {
|
||||||
|
|||||||
@ -21,30 +21,33 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
"github.com/openimsdk/open-im-server/v3/pkg/msgprocessor"
|
|
||||||
|
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
|
|
||||||
"github.com/OpenIMSDK/protocol/constant"
|
"github.com/OpenIMSDK/protocol/constant"
|
||||||
|
"github.com/openimsdk/open-im-server/v3/pkg/msgprocessor"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed version
|
//go:embed version
|
||||||
var Version string
|
var Version string
|
||||||
|
|
||||||
var (
|
|
||||||
_, b, _, _ = runtime.Caller(0)
|
|
||||||
// Root folder of this project.
|
|
||||||
Root = filepath.Join(filepath.Dir(b), "../../..")
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
FileName = "config.yaml"
|
FileName = "config.yaml"
|
||||||
NotificationFileName = "notification.yaml"
|
NotificationFileName = "notification.yaml"
|
||||||
DefaultFolderPath = "../config/"
|
DefaultFolderPath = "../config/"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// getProjectRoot returns the absolute path of the project root directory
|
||||||
|
func getProjectRoot() string {
|
||||||
|
// Program counter (PC): This represents the address of the function.
|
||||||
|
// File path: The full path to the source file from which the function was called.
|
||||||
|
// Line number: The line number in the source file from which the function was called.
|
||||||
|
// Success flag: it will be true if the information was successfully fetched, false otherwise.
|
||||||
|
_, b, _, _ := runtime.Caller(0) // pkg/common/config/parse.go
|
||||||
|
return filepath.Join(filepath.Dir(b), "../../..")
|
||||||
|
}
|
||||||
|
|
||||||
func GetOptionsByNotification(cfg NotificationConf) msgprocessor.Options {
|
func GetOptionsByNotification(cfg NotificationConf) msgprocessor.Options {
|
||||||
opts := msgprocessor.NewOptions()
|
opts := msgprocessor.NewOptions()
|
||||||
|
|
||||||
if cfg.UnreadCount {
|
if cfg.UnreadCount {
|
||||||
opts = msgprocessor.WithOptions(opts, msgprocessor.WithUnreadCount(true))
|
opts = msgprocessor.WithOptions(opts, msgprocessor.WithUnreadCount(true))
|
||||||
}
|
}
|
||||||
@ -61,40 +64,31 @@ func GetOptionsByNotification(cfg NotificationConf) msgprocessor.Options {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func initConfig(config interface{}, configName, configFolderPath string) error {
|
func initConfig(config interface{}, configName, configFolderPath string) error {
|
||||||
if configFolderPath == "" {
|
configFolderPath = filepath.Join(configFolderPath, configName)
|
||||||
configFolderPath = DefaultFolderPath
|
_, err := os.Stat(configFolderPath)
|
||||||
}
|
|
||||||
configPath := filepath.Join(configFolderPath, configName)
|
|
||||||
defer func() {
|
|
||||||
fmt.Println("use config", configPath)
|
|
||||||
}()
|
|
||||||
_, err := os.Stat(configPath)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !os.IsNotExist(err) {
|
if !os.IsNotExist(err) {
|
||||||
return err
|
return fmt.Errorf("stat config path error: %w", err)
|
||||||
}
|
}
|
||||||
configPath = filepath.Join(Root, "config", configName)
|
configFolderPath = filepath.Join(getProjectRoot(), "config", configName)
|
||||||
} else {
|
|
||||||
Root = filepath.Dir(configPath)
|
|
||||||
}
|
}
|
||||||
data, err := os.ReadFile(configPath)
|
data, err := os.ReadFile(configFolderPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("read file error: %w", err)
|
||||||
}
|
}
|
||||||
if err = yaml.Unmarshal(data, config); err != nil {
|
if err = yaml.Unmarshal(data, config); err != nil {
|
||||||
return err
|
return fmt.Errorf("unmarshal yaml error: %w", err)
|
||||||
}
|
}
|
||||||
|
fmt.Println("use config", configFolderPath)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitConfig(configFolderPath string) error {
|
func InitConfig(configFolderPath string) error {
|
||||||
err := initConfig(&Config, FileName, configFolderPath)
|
if configFolderPath == "" {
|
||||||
if err != nil {
|
configFolderPath = DefaultFolderPath
|
||||||
|
}
|
||||||
|
if err := initConfig(&Config, FileName, configFolderPath); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = initConfig(&Config.Notification, NotificationFileName, configFolderPath)
|
return initConfig(&Config.Notification, NotificationFileName, configFolderPath)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user