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

51 lines
1.2 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"
2023-03-07 12:12:16 +08:00
"OpenIM/pkg/common/cmd"
2023-02-23 19:15:30 +08:00
"OpenIM/pkg/common/config"
"OpenIM/pkg/common/log"
2023-03-13 12:34:56 +08:00
"context"
2022-04-06 15:33:16 +08:00
"fmt"
2023-03-07 17:21:48 +08:00
"github.com/OpenIMSDK/openKeeper"
2023-03-13 15:39:47 +08:00
"net"
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-07 17:59:34 +08:00
func main() {
2023-03-08 11:45:54 +08:00
apiCmd := cmd.NewApiCmd()
apiCmd.AddPortFlag()
apiCmd.AddApi(run)
if err := apiCmd.Execute(); err != nil {
2023-03-13 15:39:47 +08:00
panic(err.Error())
2023-03-07 17:59:34 +08:00
}
2023-03-06 18:26:23 +08:00
}
2023-03-08 11:45:54 +08:00
func run(port int) error {
2023-03-07 12:12:16 +08:00
if port == 0 {
port = config.Config.Api.GinPort[0]
}
2023-03-08 18:38:35 +08:00
zk, err := openKeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, 10, config.Config.Zookeeper.UserName, config.Config.Zookeeper.Password)
2023-03-07 17:21:48 +08:00
if err != nil {
return err
}
2023-02-24 17:42:01 +08:00
log.NewPrivateLog(constant.LogFileName)
2023-03-07 17:21:48 +08:00
router := api.NewGinRouter(zk)
2023-03-13 15:39:47 +08:00
var address string
2022-05-07 19:52:18 +08:00
if config.Config.Api.ListenIP != "" {
2023-03-13 15:39:47 +08:00
address = net.JoinHostPort(config.Config.Api.ListenIP, strconv.Itoa(port))
} else {
address = net.JoinHostPort("0.0.0.0", strconv.Itoa(port))
2022-05-07 19:52:18 +08:00
}
2023-03-08 13:34:12 +08:00
fmt.Println("start api server, address: ", address, ", OpenIM version: ", config.Version)
2023-03-13 12:34:56 +08:00
log.ZInfo(context.Background(), "start server success", "address", address, "version", config.Version)
2023-03-07 17:21:48 +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
}