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

51 lines
1.2 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"
2 years ago
"context"
3 years ago
"fmt"
2 years ago
"github.com/OpenIMSDK/openKeeper"
2 years ago
"net"
4 years ago
"strconv"
2 years ago
"OpenIM/pkg/common/constant"
4 years ago
)
2 years ago
func main() {
2 years ago
apiCmd := cmd.NewApiCmd()
apiCmd.AddPortFlag()
apiCmd.AddApi(run)
if err := apiCmd.Execute(); err != nil {
2 years ago
panic(err.Error())
2 years ago
}
2 years ago
}
2 years ago
func run(port int) error {
2 years ago
if port == 0 {
port = config.Config.Api.GinPort[0]
}
2 years ago
zk, err := openKeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, 10, config.Config.Zookeeper.UserName, config.Config.Zookeeper.Password)
2 years ago
if err != nil {
return err
}
2 years ago
log.NewPrivateLog(constant.LogFileName)
2 years ago
router := api.NewGinRouter(zk)
2 years ago
var address string
if config.Config.Api.ListenIP != "" {
2 years ago
address = net.JoinHostPort(config.Config.Api.ListenIP, strconv.Itoa(port))
} else {
address = net.JoinHostPort("0.0.0.0", strconv.Itoa(port))
}
2 years ago
fmt.Println("start api server, address: ", address, ", OpenIM version: ", config.Version)
2 years ago
log.ZInfo(context.Background(), "start server success", "address", address, "version", config.Version)
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
}