You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/cmd/api/main.go

64 lines
1.5 KiB

4 years ago
package main
import (
2 years ago
"OpenIM/internal/api"
2 years ago
"OpenIM/pkg/common/cmd"
2 years ago
"OpenIM/pkg/common/config"
"OpenIM/pkg/common/log"
3 years ago
"fmt"
2 years ago
"github.com/spf13/cobra"
"os"
4 years ago
"strconv"
2 years ago
"OpenIM/pkg/common/constant"
4 years ago
)
2 years ago
var startCmd = &cobra.Command{
Use: "start",
Short: "Start the server",
Run: func(cmd *cobra.Command, args []string) {
2 years ago
port, _ := cmd.Flags().GetInt(constant.FlagPort)
configFolderPath, _ := cmd.Flags().GetString(constant.FlagConf)
2 years ago
if err := run(configFolderPath, port); err != nil {
2 years ago
panic(err.Error())
}
},
}
func init() {
2 years ago
startCmd.Flags().IntP(constant.FlagPort, "p", 0, "Port to listen on")
2 years ago
startCmd.Flags().StringP(constant.FlagConf, "c", "", "Path to config file folder")
2 years ago
}
2 years ago
func run(configFolderPath string, port int) error {
if err := config.InitConfig(configFolderPath); err != nil {
2 years ago
return err
2 years ago
}
2 years ago
if port == 0 {
port = config.Config.Api.GinPort[0]
}
2 years ago
log.NewPrivateLog(constant.LogFileName)
router := api.NewGinRouter()
2 years ago
address := constant.LocalHost + ":" + strconv.Itoa(port)
if config.Config.Api.ListenIP != "" {
2 years ago
address = config.Config.Api.ListenIP + ":" + strconv.Itoa(port)
}
2 years ago
fmt.Println("start api server, address: ", address, ", OpenIM version: ", constant.CurrentVersion)
2 years ago
err := router.Run(address)
3 years ago
if err != nil {
2 years ago
log.Error("", "api run failed ", address, err.Error())
2 years ago
return err
}
return nil
}
func main() {
2 years ago
rootCmd := cmd.NewRootCmd()
rootCmd.AddCommand(startCmd)
2 years ago
if err := startCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
3 years ago
}
4 years ago
}