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

59 lines
1.4 KiB

4 years ago
package main
import (
2 years ago
"OpenIM/internal/api"
"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) {
port, _ := cmd.Flags().GetInt("port")
2 years ago
configFolderPath, _ := cmd.Flags().GetString("config_folder_path")
fmt.Printf("Starting server on port %s with config file at %s\n", port, configFolderPath)
if err := run(configFolderPath, port); err != nil {
2 years ago
panic(err.Error())
}
},
}
func init() {
2 years ago
startCmd.Flags().IntP("port", "p", 10002, "Port to listen on")
startCmd.Flags().StringP("config_path", "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
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() {
if err := startCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
3 years ago
}
4 years ago
}