mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-06 04:15:46 +08:00
fix: docker compose
Signed-off-by: Xinwei Xiong (cubxxw) <3293172751nss@gmail.com>
This commit is contained in:
parent
b17b212866
commit
a1eebcaeba
@ -10,7 +10,7 @@ ENV GOPROXY=$GOPROXY
|
|||||||
# Set up the working directory
|
# Set up the working directory
|
||||||
WORKDIR /openim/openim-server
|
WORKDIR /openim/openim-server
|
||||||
|
|
||||||
COPY go.mod go.sum ./
|
COPY go.mod go.sum ./dd
|
||||||
RUN go mod download
|
RUN go mod download
|
||||||
|
|
||||||
# Copy all files to the container
|
# Copy all files to the container
|
||||||
|
@ -16,7 +16,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
@ -31,15 +30,11 @@ import (
|
|||||||
|
|
||||||
"github.com/IBM/sarama"
|
"github.com/IBM/sarama"
|
||||||
"github.com/OpenIMSDK/tools/errs"
|
"github.com/OpenIMSDK/tools/errs"
|
||||||
"github.com/OpenIMSDK/tools/utils"
|
|
||||||
"github.com/go-zookeeper/zk"
|
"github.com/go-zookeeper/zk"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
"gorm.io/driver/mysql"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
|
|
||||||
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||||||
"github.com/openimsdk/open-im-server/v3/pkg/common/kafka"
|
|
||||||
|
|
||||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||||
)
|
)
|
||||||
@ -53,6 +48,12 @@ const (
|
|||||||
configErrCode = 6001
|
configErrCode = 6001
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
colorRed = 31
|
||||||
|
colorGreen = 32
|
||||||
|
colorYellow = 33
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
cfgPath = flag.String("c", defaultCfgPath, "Path to the configuration file")
|
cfgPath = flag.String("c", defaultCfgPath, "Path to the configuration file")
|
||||||
|
|
||||||
@ -132,127 +133,131 @@ func exactIP(urll string) string {
|
|||||||
return host
|
return host
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkMysql() error {
|
// Helper function to get environment variable or default value
|
||||||
if config.Config.Mysql == nil {
|
func getEnv(key, fallback string) string {
|
||||||
return nil
|
if value, exists := os.LookupEnv(key); exists {
|
||||||
|
return value
|
||||||
}
|
}
|
||||||
var sqlDB *sql.DB
|
return fallback
|
||||||
defer func() {
|
|
||||||
if sqlDB != nil {
|
|
||||||
sqlDB.Close()
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=true&loc=Local",
|
|
||||||
config.Config.Mysql.Username, config.Config.Mysql.Password, config.Config.Mysql.Address[0], "mysql")
|
|
||||||
db, err := gorm.Open(mysql.Open(dsn), nil)
|
|
||||||
if err != nil {
|
|
||||||
return errs.Wrap(err)
|
|
||||||
} else {
|
|
||||||
sqlDB, err = db.DB()
|
|
||||||
err = sqlDB.Ping()
|
|
||||||
if err != nil {
|
|
||||||
return errs.Wrap(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// checkMongo checks the MongoDB connection
|
||||||
func checkMongo() error {
|
func checkMongo() error {
|
||||||
var client *mongo.Client
|
// Use environment variables or fallback to config
|
||||||
uri := "mongodb://sample.host:27017/?maxPoolSize=20&w=majority"
|
uri := getEnv("MONGO_URI", buildMongoURI())
|
||||||
defer func() {
|
|
||||||
if client != nil {
|
|
||||||
client.Disconnect(context.TODO())
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
if config.Config.Mongo.Uri != "" {
|
|
||||||
uri = config.Config.Mongo.Uri
|
|
||||||
} else {
|
|
||||||
mongodbHosts := ""
|
|
||||||
for i, v := range config.Config.Mongo.Address {
|
|
||||||
if i == len(config.Config.Mongo.Address)-1 {
|
|
||||||
mongodbHosts += v
|
|
||||||
} else {
|
|
||||||
mongodbHosts += v + ","
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if config.Config.Mongo.Password != "" && config.Config.Mongo.Username != "" {
|
|
||||||
uri = fmt.Sprintf("mongodb://%s:%s@%s/%s?maxPoolSize=%d&authSource=admin",
|
|
||||||
config.Config.Mongo.Username, config.Config.Mongo.Password, mongodbHosts,
|
|
||||||
config.Config.Mongo.Database, config.Config.Mongo.MaxPoolSize)
|
|
||||||
} else {
|
|
||||||
uri = fmt.Sprintf("mongodb://%s/%s/?maxPoolSize=%d&authSource=admin",
|
|
||||||
mongodbHosts, config.Config.Mongo.Database,
|
|
||||||
config.Config.Mongo.MaxPoolSize)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
|
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errs.Wrap(err)
|
return errs.Wrap(err)
|
||||||
} else {
|
}
|
||||||
err = client.Ping(context.TODO(), nil)
|
defer client.Disconnect(context.TODO())
|
||||||
if err != nil {
|
|
||||||
return errs.Wrap(err)
|
if err = client.Ping(context.TODO(), nil); err != nil {
|
||||||
}
|
return errs.Wrap(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// buildMongoURI constructs the MongoDB URI using configuration settings
|
||||||
|
func buildMongoURI() string {
|
||||||
|
// Fallback to config if environment variables are not set
|
||||||
|
username := config.Config.Mongo.Username
|
||||||
|
password := config.Config.Mongo.Password
|
||||||
|
database := config.Config.Mongo.Database
|
||||||
|
maxPoolSize := config.Config.Mongo.MaxPoolSize
|
||||||
|
|
||||||
|
mongodbHosts := strings.Join(config.Config.Mongo.Address, ",")
|
||||||
|
|
||||||
|
if username != "" && password != "" {
|
||||||
|
return fmt.Sprintf("mongodb://%s:%s@%s/%s?maxPoolSize=%d&authSource=admin",
|
||||||
|
username, password, mongodbHosts, database, maxPoolSize)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("mongodb://%s/%s?maxPoolSize=%d&authSource=admin",
|
||||||
|
mongodbHosts, database, maxPoolSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkMinio checks the MinIO connection
|
||||||
func checkMinio() error {
|
func checkMinio() error {
|
||||||
if config.Config.Object.Enable == "minio" {
|
// Check if MinIO is enabled
|
||||||
conf := config.Config.Object.Minio
|
if config.Config.Object.Enable != "minio" {
|
||||||
u, _ := url.Parse(conf.Endpoint)
|
return nil
|
||||||
minioClient, err := minio.New(u.Host, &minio.Options{
|
}
|
||||||
Creds: credentials.NewStaticV4(conf.AccessKeyID, conf.SecretAccessKey, ""),
|
|
||||||
Secure: u.Scheme == "https",
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return errs.Wrap(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
cancel, err := minioClient.HealthCheck(time.Duration(minioHealthCheckDuration) * time.Second)
|
// Prioritize environment variables
|
||||||
defer func() {
|
endpoint := getEnv("MINIO_ENDPOINT", config.Config.Object.Minio.Endpoint)
|
||||||
if cancel != nil {
|
accessKeyID := getEnv("MINIO_ACCESS_KEY_ID", config.Config.Object.Minio.AccessKeyID)
|
||||||
cancel()
|
secretAccessKey := getEnv("MINIO_SECRET_ACCESS_KEY", config.Config.Object.Minio.SecretAccessKey)
|
||||||
}
|
useSSL := getEnv("MINIO_USE_SSL", "false") // Assuming SSL is not used by default
|
||||||
}()
|
|
||||||
if err != nil {
|
if endpoint == "" || accessKeyID == "" || secretAccessKey == "" {
|
||||||
return errs.Wrap(err)
|
return ErrConfig.Wrap("MinIO configuration missing")
|
||||||
} else {
|
}
|
||||||
if minioClient.IsOffline() {
|
|
||||||
return ErrComponentStart.Wrap("Minio server is offline")
|
// Parse endpoint URL to determine if SSL is enabled
|
||||||
}
|
u, err := url.Parse(endpoint)
|
||||||
}
|
if err != nil {
|
||||||
if exactIP(config.Config.Object.ApiURL) == "127.0.0.1" || exactIP(config.Config.Object.Minio.SignEndpoint) == "127.0.0.1" {
|
return errs.Wrap(err)
|
||||||
return ErrConfig.Wrap("apiURL or Minio SignEndpoint endpoint contain 127.0.0.1")
|
}
|
||||||
}
|
secure := u.Scheme == "https" || useSSL == "true"
|
||||||
|
|
||||||
|
// Initialize MinIO client
|
||||||
|
minioClient, err := minio.New(u.Host, &minio.Options{
|
||||||
|
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
|
||||||
|
Secure: secure,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return errs.Wrap(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Perform health check
|
||||||
|
cancel, err := minioClient.HealthCheck(time.Duration(minioHealthCheckDuration) * time.Second)
|
||||||
|
if err != nil {
|
||||||
|
return errs.Wrap(err)
|
||||||
|
}
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
if minioClient.IsOffline() {
|
||||||
|
return ErrComponentStart.Wrap("Minio server is offline")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for localhost in API URL and Minio SignEndpoint
|
||||||
|
if exactIP(config.Config.Object.ApiURL) == "127.0.0.1" || exactIP(config.Config.Object.Minio.SignEndpoint) == "127.0.0.1" {
|
||||||
|
return ErrConfig.Wrap("apiURL or Minio SignEndpoint endpoint contain 127.0.0.1")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// checkRedis checks the Redis connection
|
||||||
func checkRedis() error {
|
func checkRedis() error {
|
||||||
|
// Prioritize environment variables
|
||||||
|
address := getEnv("REDIS_ADDRESS", strings.Join(config.Config.Redis.Address, ","))
|
||||||
|
username := getEnv("REDIS_USERNAME", config.Config.Redis.Username)
|
||||||
|
password := getEnv("REDIS_PASSWORD", config.Config.Redis.Password)
|
||||||
|
|
||||||
|
// Split address to handle multiple addresses for cluster setup
|
||||||
|
redisAddresses := strings.Split(address, ",")
|
||||||
|
|
||||||
var redisClient redis.UniversalClient
|
var redisClient redis.UniversalClient
|
||||||
defer func() {
|
if len(redisAddresses) > 1 {
|
||||||
if redisClient != nil {
|
// Use cluster client for multiple addresses
|
||||||
redisClient.Close()
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
if len(config.Config.Redis.Address) > 1 {
|
|
||||||
redisClient = redis.NewClusterClient(&redis.ClusterOptions{
|
redisClient = redis.NewClusterClient(&redis.ClusterOptions{
|
||||||
Addrs: config.Config.Redis.Address,
|
Addrs: redisAddresses,
|
||||||
Username: config.Config.Redis.Username,
|
Username: username,
|
||||||
Password: config.Config.Redis.Password,
|
Password: password,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
// Use regular client for single address
|
||||||
redisClient = redis.NewClient(&redis.Options{
|
redisClient = redis.NewClient(&redis.Options{
|
||||||
Addr: config.Config.Redis.Address[0],
|
Addr: redisAddresses[0],
|
||||||
Username: config.Config.Redis.Username,
|
Username: username,
|
||||||
Password: config.Config.Redis.Password,
|
Password: password,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
defer redisClient.Close()
|
||||||
|
|
||||||
|
// Ping Redis to check connectivity
|
||||||
_, err := redisClient.Ping(context.Background()).Result()
|
_, err := redisClient.Ping(context.Background()).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errs.Wrap(err)
|
return errs.Wrap(err)
|
||||||
@ -261,75 +266,110 @@ func checkRedis() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// checkZookeeper checks the Zookeeper connection
|
||||||
func checkZookeeper() error {
|
func checkZookeeper() error {
|
||||||
var c *zk.Conn
|
// Prioritize environment variables
|
||||||
defer func() {
|
schema := getEnv("ZOOKEEPER_SCHEMA", "digest")
|
||||||
if c != nil {
|
address := getEnv("ZOOKEEPER_ADDRESS", strings.Join(config.Config.Zookeeper.ZkAddr, ","))
|
||||||
c.Close()
|
username := getEnv("ZOOKEEPER_USERNAME", config.Config.Zookeeper.Username)
|
||||||
}
|
password := getEnv("ZOOKEEPER_PASSWORD", config.Config.Zookeeper.Password)
|
||||||
}()
|
|
||||||
c, _, err := zk.Connect(config.Config.Zookeeper.ZkAddr, time.Second)
|
// Split addresses to handle multiple Zookeeper nodes
|
||||||
|
zookeeperAddresses := strings.Split(address, ",")
|
||||||
|
|
||||||
|
// Connect to Zookeeper
|
||||||
|
c, _, err := zk.Connect(zookeeperAddresses, time.Second) // Adjust the timeout as necessary
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errs.Wrap(err)
|
return errs.Wrap(err)
|
||||||
} else {
|
}
|
||||||
if config.Config.Zookeeper.Username != "" && config.Config.Zookeeper.Password != "" {
|
defer c.Close()
|
||||||
if err := c.AddAuth("digest", []byte(config.Config.Zookeeper.Username+":"+config.Config.Zookeeper.Password)); err != nil {
|
|
||||||
return errs.Wrap(err)
|
// Set authentication if username and password are provided
|
||||||
}
|
if username != "" && password != "" {
|
||||||
}
|
if err := c.AddAuth(schema, []byte(username+":"+password)); err != nil {
|
||||||
_, _, err = c.Get("/")
|
|
||||||
if err != nil {
|
|
||||||
return errs.Wrap(err)
|
return errs.Wrap(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if Zookeeper is reachable
|
||||||
|
_, _, err = c.Get("/")
|
||||||
|
if err != nil {
|
||||||
|
return errs.Wrap(err)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// checkKafka checks the Kafka connection
|
||||||
func checkKafka() error {
|
func checkKafka() error {
|
||||||
var kafkaClient sarama.Client
|
// Prioritize environment variables
|
||||||
defer func() {
|
username := getEnv("KAFKA_USERNAME", config.Config.Kafka.Username)
|
||||||
if kafkaClient != nil {
|
password := getEnv("KAFKA_PASSWORD", config.Config.Kafka.Password)
|
||||||
kafkaClient.Close()
|
address := getEnv("KAFKA_ADDRESS", strings.Join(config.Config.Kafka.Addr, ","))
|
||||||
}
|
|
||||||
}()
|
// Split addresses to handle multiple Kafka brokers
|
||||||
|
kafkaAddresses := strings.Split(address, ",")
|
||||||
|
|
||||||
|
// Configure Kafka client
|
||||||
cfg := sarama.NewConfig()
|
cfg := sarama.NewConfig()
|
||||||
if config.Config.Kafka.Username != "" && config.Config.Kafka.Password != "" {
|
if username != "" && password != "" {
|
||||||
cfg.Net.SASL.Enable = true
|
cfg.Net.SASL.Enable = true
|
||||||
cfg.Net.SASL.User = config.Config.Kafka.Username
|
cfg.Net.SASL.User = username
|
||||||
cfg.Net.SASL.Password = config.Config.Kafka.Password
|
cfg.Net.SASL.Password = password
|
||||||
}
|
}
|
||||||
kafka.SetupTLSConfig(cfg)
|
// Additional Kafka setup (e.g., TLS configuration) can be added here
|
||||||
kafkaClient, err := sarama.NewClient(config.Config.Kafka.Addr, cfg)
|
// kafka.SetupTLSConfig(cfg)
|
||||||
|
|
||||||
|
// Create Kafka client
|
||||||
|
kafkaClient, err := sarama.NewClient(kafkaAddresses, cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errs.Wrap(err)
|
return errs.Wrap(err)
|
||||||
} else {
|
}
|
||||||
topics, err := kafkaClient.Topics()
|
defer kafkaClient.Close()
|
||||||
if err != nil {
|
|
||||||
return err
|
// Verify if necessary topics exist
|
||||||
}
|
topics, err := kafkaClient.Topics()
|
||||||
if !utils.IsContain(config.Config.Kafka.MsgToMongo.Topic, topics) {
|
if err != nil {
|
||||||
return ErrComponentStart.Wrap(fmt.Sprintf("kafka doesn't contain topic:%v", config.Config.Kafka.MsgToMongo.Topic))
|
return errs.Wrap(err)
|
||||||
}
|
}
|
||||||
if !utils.IsContain(config.Config.Kafka.MsgToPush.Topic, topics) {
|
|
||||||
return ErrComponentStart.Wrap(fmt.Sprintf("kafka doesn't contain topic:%v", config.Config.Kafka.MsgToPush.Topic))
|
requiredTopics := []string{
|
||||||
}
|
config.Config.Kafka.MsgToMongo.Topic,
|
||||||
if !utils.IsContain(config.Config.Kafka.LatestMsgToRedis.Topic, topics) {
|
config.Config.Kafka.MsgToPush.Topic,
|
||||||
return ErrComponentStart.Wrap(fmt.Sprintf("kafka doesn't contain topic:%v", config.Config.Kafka.LatestMsgToRedis.Topic))
|
config.Config.Kafka.LatestMsgToRedis.Topic,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, requiredTopic := range requiredTopics {
|
||||||
|
if !isTopicPresent(requiredTopic, topics) {
|
||||||
|
return ErrComponentStart.Wrap(fmt.Sprintf("Kafka doesn't contain topic: %v", requiredTopic))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isTopicPresent checks if a topic is present in the list of topics
|
||||||
|
func isTopicPresent(topic string, topics []string) bool {
|
||||||
|
for _, t := range topics {
|
||||||
|
if t == topic {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func colorPrint(colorCode int, format string, a ...interface{}) {
|
||||||
|
fmt.Printf("\x1b[%dm%s\x1b[0m\n", colorCode, fmt.Sprintf(format, a...))
|
||||||
|
}
|
||||||
|
|
||||||
func errorPrint(s string) {
|
func errorPrint(s string) {
|
||||||
fmt.Printf("\x1b[%dm%v\x1b[0m\n", 31, s)
|
colorPrint(colorRed, "%v", s)
|
||||||
}
|
}
|
||||||
|
|
||||||
func successPrint(s string) {
|
func successPrint(s string) {
|
||||||
fmt.Printf("\x1b[%dm%v\x1b[0m\n", 32, s)
|
colorPrint(colorGreen, "%v", s)
|
||||||
}
|
}
|
||||||
|
|
||||||
func warningPrint(s string) {
|
func warningPrint(s string) {
|
||||||
fmt.Printf("\x1b[%dmWarning: But %v\x1b[0m\n", 33, s)
|
colorPrint(colorYellow, "Warning: But %v", s)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user