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")
configPath, _ := cmd.Flags().GetString("config_path")
fmt.Printf("Starting server on port %s with config file at %s\n", port, configPath)
if err := run(port); err != nil {
panic(err.Error())
}
},
}
func init() {
startCmd.Flags().IntP("port", "port", 10002, "Port to listen on")
startCmd.Flags().StringP("config_path", "config_path", "", "Path to config file folder")
}
func run(port int) error {
2 years ago
if err := config.InitConfig(); 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
}