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

72 lines
2.0 KiB

4 years ago
package main
import (
"bytes"
2 years ago
"context"
3 years ago
"fmt"
2 years ago
"net"
"strconv"
2 years ago
"time"
2 years ago
2 years ago
"gopkg.in/yaml.v3"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/internal/api"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/cmd"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
2 years ago
"github.com/OpenIMSDK/openKeeper"
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
var err error
rdb, err := cache.NewRedis()
2 years ago
if err != nil {
return err
}
2 years ago
var client discoveryregistry.SvcDiscoveryRegistry
2 years ago
client, err = openKeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema,
openKeeper.WithFreq(time.Hour), openKeeper.WithUserNameAndPassword(config.Config.Zookeeper.UserName,
config.Config.Zookeeper.Password), openKeeper.WithRoundRobin(), openKeeper.WithTimeout(10))
2 years ago
if err != nil {
return err
}
buf := bytes.NewBuffer(nil)
if err := yaml.NewEncoder(buf).Encode(config.Config); err != nil {
return err
}
2 years ago
if err := client.RegisterConf2Registry(constant.OpenIMCommonConfigKey, buf.Bytes()); err != nil {
return err
}
2 years ago
log.NewPrivateLog(constant.LogFileName)
2 years ago
router := api.NewGinRouter(client, rdb)
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.ZError(context.Background(), "api run failed ", err, "address", address)
2 years ago
return err
}
return nil
}