Files
open-im-server/cmd/api/main.go
T

59 lines
1.4 KiB
Go
Raw Normal View History

2021-05-26 19:15:25 +08:00
package main
import (
2023-02-23 19:15:30 +08:00
"OpenIM/internal/api"
"OpenIM/pkg/common/config"
"OpenIM/pkg/common/log"
2022-04-06 15:33:16 +08:00
"fmt"
2023-03-06 18:26:23 +08:00
"github.com/spf13/cobra"
"os"
2022-07-31 01:14:26 +08:00
2021-05-26 19:15:25 +08:00
"strconv"
2023-02-23 19:15:30 +08:00
"OpenIM/pkg/common/constant"
2021-05-26 19:15:25 +08:00
)
2023-03-06 18:26:23 +08:00
var startCmd = &cobra.Command{
Use: "start",
Short: "Start the server",
Run: func(cmd *cobra.Command, args []string) {
2023-03-06 18:47:48 +08:00
port, _ := cmd.Flags().GetInt(constant.FlagPort)
configFolderPath, _ := cmd.Flags().GetString(constant.FlagConf)
fmt.Printf("Starting server on port %d with config file at %s\n", port, configFolderPath)
2023-03-06 18:41:59 +08:00
if err := run(configFolderPath, port); err != nil {
2023-03-06 18:26:23 +08:00
panic(err.Error())
}
},
}
func init() {
2023-03-06 18:47:48 +08:00
startCmd.Flags().IntP(constant.FlagPort, "p", 10002, "Port to listen on")
startCmd.Flags().StringP(constant.FlagConf, "c", "", "Path to config file folder")
2023-03-06 18:26:23 +08:00
}
2023-03-06 18:41:59 +08:00
func run(configFolderPath string, port int) error {
if err := config.InitConfig(configFolderPath); err != nil {
2023-03-06 18:26:23 +08:00
return err
2023-02-23 19:51:58 +08:00
}
2023-02-24 17:42:01 +08:00
log.NewPrivateLog(constant.LogFileName)
router := api.NewGinRouter()
2023-03-06 18:26:23 +08:00
address := constant.LocalHost + ":" + strconv.Itoa(port)
2022-05-07 19:52:18 +08:00
if config.Config.Api.ListenIP != "" {
2023-03-06 18:26:23 +08:00
address = config.Config.Api.ListenIP + ":" + strconv.Itoa(port)
2022-05-07 19:52:18 +08:00
}
2022-12-14 16:14:56 +08:00
fmt.Println("start api server, address: ", address, ", OpenIM version: ", constant.CurrentVersion)
2023-02-23 19:15:30 +08:00
err := router.Run(address)
2022-04-06 15:33:16 +08:00
if err != nil {
2022-09-14 12:31:24 +08:00
log.Error("", "api run failed ", address, err.Error())
2023-03-06 18:26:23 +08:00
return err
}
return nil
}
func main() {
if err := startCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
2022-04-06 15:33:16 +08:00
}
2021-05-26 19:15:25 +08:00
}