首次提交:初始化项目代码
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"cloudnest/internal/domain/auth/entity"
|
||||
fileentity "cloudnest/internal/domain/file/entity"
|
||||
"cloudnest/internal/pkg/logger"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
gormlogger "gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
// MySQLClient provides MySQL database operations.
|
||||
// MySQLClient 提供 MySQL 数据库操作。
|
||||
//
|
||||
// This struct wraps the GORM database connection and provides methods
|
||||
// for database initialization and migration.
|
||||
//
|
||||
// 此结构体封装了 GORM 数据库连接,并提供了数据库初始化和迁移的方法。
|
||||
type MySQLClient struct {
|
||||
DB *gorm.DB
|
||||
}
|
||||
|
||||
// NewMySQLClient creates a new MySQL client.
|
||||
// NewMySQLClient 创建一个新的 MySQL 客户端。
|
||||
//
|
||||
// Parameters:
|
||||
// - dsn: The MySQL data source name in format "user:password@tcp(host:port)/database".
|
||||
//
|
||||
// Returns:
|
||||
// - *MySQLClient: A pointer to the new MySQLClient instance.
|
||||
// - error: An error if the connection fails.
|
||||
//
|
||||
// Note:
|
||||
// - The connection is established immediately.
|
||||
// - GORM logging is enabled at Info level.
|
||||
//
|
||||
// 参数:
|
||||
// - dsn: MySQL 数据源名称,格式为 "user:password@tcp(host:port)/database"。
|
||||
//
|
||||
// 返回值:
|
||||
// - *MySQLClient: 新创建的 MySQLClient 实例指针。
|
||||
// - error: 如果连接失败则返回错误。
|
||||
//
|
||||
// 注意:
|
||||
// - 连接会立即建立。
|
||||
// - GORM 日志级别为 Info。
|
||||
func NewMySQLClient(dsn string) (*MySQLClient, error) {
|
||||
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
|
||||
Logger: gormlogger.Default.LogMode(gormlogger.Info),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
logger.Info("mysql connected")
|
||||
return &MySQLClient{DB: db}, nil
|
||||
}
|
||||
|
||||
// Migrate runs database migrations for all registered models.
|
||||
// Migrate 对所有注册的模型运行数据库迁移。
|
||||
//
|
||||
// Returns:
|
||||
// - error: An error if migration fails.
|
||||
//
|
||||
// Note:
|
||||
// - This method should be called once during application startup.
|
||||
// - It automatically creates or updates database tables based on model definitions.
|
||||
//
|
||||
// 返回值:
|
||||
// - error: 如果迁移失败则返回错误。
|
||||
//
|
||||
// 注意:
|
||||
// - 此方法应在应用程序启动时调用一次。
|
||||
// - 它会根据模型定义自动创建或更新数据库表。
|
||||
func (c *MySQLClient) Migrate() error {
|
||||
if err := c.DB.AutoMigrate(&entity.User{}, &fileentity.Favorite{}, &fileentity.DeletedFile{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.setTableComment("users", "用户表")
|
||||
c.setTableComment("favorites", "用户收藏表")
|
||||
c.setTableComment("deleted_files", "回收站文件表")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *MySQLClient) setTableComment(tableName, comment string) {
|
||||
sql := "ALTER TABLE `" + tableName + "` COMMENT = '" + comment + "'"
|
||||
if err := c.DB.Exec(sql).Error; err != nil {
|
||||
logger.Error("failed to set table comment", "table", tableName, "error", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"cloudnest/internal/pkg/logger"
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
// RedisClient provides Redis cache operations.
|
||||
// RedisClient 提供 Redis 缓存操作。
|
||||
//
|
||||
// This struct wraps the go-redis client and provides methods for cache operations.
|
||||
//
|
||||
// 此结构体封装了 go-redis 客户端,并提供了缓存操作的方法。
|
||||
type RedisClient struct {
|
||||
Client *redis.Client
|
||||
}
|
||||
|
||||
// NewRedisClient creates a new Redis client.
|
||||
// NewRedisClient 创建一个新的 Redis 客户端。
|
||||
//
|
||||
// Parameters:
|
||||
// - addr: The Redis server address (host:port).
|
||||
// - password: The Redis password (empty string for no password).
|
||||
// - db: The Redis database number (0-15).
|
||||
//
|
||||
// Returns:
|
||||
// - *RedisClient: A pointer to the new RedisClient instance.
|
||||
// - error: An error if the connection fails.
|
||||
//
|
||||
// Note:
|
||||
// - A PING command is sent to verify the connection.
|
||||
//
|
||||
// 参数:
|
||||
// - addr: Redis 服务器地址(host:port)。
|
||||
// - password: Redis 密码(无密码时为空字符串)。
|
||||
// - db: Redis 数据库编号(0-15)。
|
||||
//
|
||||
// 返回值:
|
||||
// - *RedisClient: 新创建的 RedisClient 实例指针。
|
||||
// - error: 如果连接失败则返回错误。
|
||||
//
|
||||
// 注意:
|
||||
// - 会发送 PING 命令来验证连接。
|
||||
func NewRedisClient(addr, password string, db int) (*RedisClient, error) {
|
||||
client := redis.NewClient(&redis.Options{
|
||||
Addr: addr,
|
||||
Password: password,
|
||||
DB: db,
|
||||
})
|
||||
|
||||
_, err := client.Ping(client.Context()).Result()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
logger.Info("redis connected")
|
||||
return &RedisClient{Client: client}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user