Merge remote-tracking branch 'origin/errcode' into errcode

This commit is contained in:
withchao 2023-03-07 12:19:38 +08:00
commit 555fc52acc
4 changed files with 68 additions and 5 deletions

View File

@ -2,6 +2,7 @@ package main
import ( import (
"OpenIM/internal/api" "OpenIM/internal/api"
"OpenIM/pkg/common/cmd"
"OpenIM/pkg/common/config" "OpenIM/pkg/common/config"
"OpenIM/pkg/common/log" "OpenIM/pkg/common/log"
"fmt" "fmt"
@ -19,7 +20,6 @@ var startCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
port, _ := cmd.Flags().GetInt(constant.FlagPort) port, _ := cmd.Flags().GetInt(constant.FlagPort)
configFolderPath, _ := cmd.Flags().GetString(constant.FlagConf) configFolderPath, _ := cmd.Flags().GetString(constant.FlagConf)
fmt.Printf("Starting server on port %d with config file at %s\n", port, configFolderPath)
if err := run(configFolderPath, port); err != nil { if err := run(configFolderPath, port); err != nil {
panic(err.Error()) panic(err.Error())
} }
@ -27,7 +27,7 @@ var startCmd = &cobra.Command{
} }
func init() { func init() {
startCmd.Flags().IntP(constant.FlagPort, "p", 10002, "Port to listen on") startCmd.Flags().IntP(constant.FlagPort, "p", 0, "Port to listen on")
startCmd.Flags().StringP(constant.FlagConf, "c", "", "Path to config file folder") startCmd.Flags().StringP(constant.FlagConf, "c", "", "Path to config file folder")
} }
@ -35,6 +35,9 @@ func run(configFolderPath string, port int) error {
if err := config.InitConfig(configFolderPath); err != nil { if err := config.InitConfig(configFolderPath); err != nil {
return err return err
} }
if port == 0 {
port = config.Config.Api.GinPort[0]
}
log.NewPrivateLog(constant.LogFileName) log.NewPrivateLog(constant.LogFileName)
router := api.NewGinRouter() router := api.NewGinRouter()
address := constant.LocalHost + ":" + strconv.Itoa(port) address := constant.LocalHost + ":" + strconv.Itoa(port)
@ -51,6 +54,8 @@ func run(configFolderPath string, port int) error {
} }
func main() { func main() {
rootCmd := cmd.NewRootCmd()
rootCmd.AddCommand(startCmd)
if err := startCmd.Execute(); err != nil { if err := startCmd.Execute(); err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)

View File

@ -2,11 +2,55 @@ package main
import ( import (
"OpenIM/internal/tools" "OpenIM/internal/tools"
"OpenIM/pkg/common/cmd"
"OpenIM/pkg/common/config" "OpenIM/pkg/common/config"
"context" "OpenIM/pkg/common/constant"
"flag" "flag"
"fmt"
"github.com/spf13/cobra"
"os"
) )
var showSeqCmd = &cobra.Command{
Use: "show-seq",
Short: "Start the server",
Run: func(cmd *cobra.Command, args []string) {
configFolderPath, _ := cmd.Flags().GetString(constant.FlagConf)
config.InitConfig(configFolderPath)
},
}
var
func init() {
showSeqCmd.Flags().StringP("userID", "u", "", "openIM userID")
showSeqCmd.Flags().StringP("groupID", "g", "", "openIM groupID")
startCmd.Flags().StringP(constant.FlagConf, "c", "", "Path to config file folder")
}
func run(configFolderPath string, cmd *cobra.Command) error {
if err := config.InitConfig(configFolderPath); err != nil {
return err
}
return nil
}
func main() {
rootCmd := cmd.NewRootCmd()
rootCmd.AddCommand(showSeqCmd)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
//
func main() { func main() {
if err := config.InitConfig(""); err != nil { if err := config.InitConfig(""); err != nil {
panic(err.Error()) panic(err.Error())

13
pkg/common/cmd/root.go Normal file
View File

@ -0,0 +1,13 @@
package cmd
import (
"github.com/spf13/cobra"
)
func NewRootCmd() *cobra.Command {
return &cobra.Command{
Use: "start",
Short: "Start the server",
Long: `Start the server`,
}
}

View File

@ -331,5 +331,6 @@ const LocalHost = "0.0.0.0"
// flag parse // flag parse
const ( const (
FlagPort = "port" FlagPort = "port"
PrometheusPort = "prometheus_port"
FlagConf = "config_folder_path" FlagConf = "config_folder_path"
) )