项目的目录框架
基于这个框架,后续会一个个实现
加载配置文件
在项目中用到了redis、mongodb,这两个数据库应该要在程序启动时连接,在程序的运行中只是调用,而不是每次调到时都重新连接。
配置文件 application/conf/config.ini
[mysql]
username='root'
password='root'
host='127.0.0.1'
port=3306
database='im-demo'
[redis]
host='127.0.0.1'
port='6379'
password=''
db=0
[mongodb]
username='root'
password='root'
host='127.0.0.1'
port=27017
db='admin'
读取配置文件 application/conf/config.go
package conf
import (
"fmt"
"github.com/spf13/viper"
)
var (
MysqlConf MysqlIni
RedisConf RedisIni
MongoDbConf MongoDbIni
)
type MysqlIni struct {
Username string
Password string
Host string
Port int
Database string
}
type RedisIni struct {
Host string
Port string
Password string
Db int
}
type MongoDbIni struct {
Username string
Password string
Host string
Port int
Db string
}
// viper 读取ini配置文件
func init() {
config := viper.New()
config.AddConfigPath("./application/conf/")
config.SetConfigName("config")
config.SetConfigType("ini")
if err := config.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
fmt.Println("找不到配置文件..")
} else {
fmt.Println("配置文件出错..")
}
fmt.Println(err.Error())
return
}
MysqlConf = MysqlIni{
Username: config.GetString("mysql.username"),
Password: config.GetString("mysql.password"),
Host: config.GetString("mysql.host"),
Port: config.GetInt("mysql.port"),
Database: config.GetString("mysql.database"),
}
RedisConf = RedisIni{
Host: config.GetString("redis.host"),
Port: config.GetString("redis.port"),
Password: config.GetString("redis.password"),
Db: config.GetInt("redis.db"),
}
MongoDbConf = MongoDbIni{
Username: config.GetString("mongodb.username"),
Password: config.GetString("mongodb.password"),
Host: config.GetString("mongodb.host"),
Port: config.GetInt("mongodb.port"),
Db: config.GetString("mongodb.db"),
}
}
项目初始化
package application
import (
"context"
"fmt"
"im-demo/application/conf"
"github.com/go-redis/redis"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var (
MysqlClient *gorm.DB
RedisClient *redis.Client
MongoDBClient *mongo.Client
MongoDataBaseName = "im-demo"
MongoMsgCollection = "message"
MongoSessionListCollection = "session_list"
MongoFriendRequestCollection = "friend_request"
)
func init() {
// MysqlClient = intiMysql()
RedisClient = initRedis()
MongoDBClient = initMongoDB()
}
func intiMysql() *gorm.DB {
fmt.Println("mysql is connecting...")
uri := fmt.Sprintf("%s:%s@(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local", conf.MysqlConf.Username, conf.MysqlConf.Password, conf.MysqlConf.Host, conf.MysqlConf.Port, conf.MysqlConf.Database)
db, err := gorm.Open("mysql", uri)
if err != nil {
panic(err)
} else {
fmt.Println("mysql connectiton success")
}
// 设置全局表名禁用复数
db.SingularTable(true)
return db
}
func initRedis() *redis.Client {
fmt.Println("redis is connecting...")
client := redis.NewClient(&redis.Options{
Addr: conf.RedisConf.Host + ":" + conf.RedisConf.Port, // "127.0.0.1:6379"
Password: conf.RedisConf.Password, // ""
DB: conf.RedisConf.Db, // 0
})
err := client.Ping().Err()
if err != nil {
panic(err)
} else {
fmt.Println("redis connectiton success")
}
return client
}
func initMongoDB() *mongo.Client {
fmt.Println("mongodb is connecting...")
uri := fmt.Sprintf("mongodb://%s:%s@%s:%d/%s", conf.MongoDbConf.Username, conf.MongoDbConf.Password, conf.MongoDbConf.Host, conf.MongoDbConf.Port, conf.MongoDbConf.Db)
clientOptions := options.Client().ApplyURI(uri)
client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
panic(err)
}
// 检查连接
err = client.Ping(context.TODO(), nil)
if err != nil {
panic(err)
}
println("mongodb connectiton success")
return client
}
这里只是项目前期,关键的IM还在后续
IM即时通讯从0到1的实践,相关文章:
IM即时通讯-项目框架搭建(二)<本文>
IM即时通讯-用户注册登录,及gin+JWT鉴权(三)
IM即时通讯-核心结构体设计(四)
IM即时通讯-消息id(五)
IM即时通讯-会话列表和会话信箱(六)
IM即时通讯-1.0版成果展示与后续扩展(七)
版权声明:本文为qq_36034503原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。