2024-01-16 01:58:20 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2024-01-28 07:20:59 +00:00
|
|
|
"context"
|
2024-01-16 01:58:20 +00:00
|
|
|
"fmt"
|
2024-02-22 08:30:40 +00:00
|
|
|
|
2024-02-24 13:19:27 +00:00
|
|
|
"github.com/redis/go-redis/v9"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2024-02-22 08:30:40 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/portal/config"
|
|
|
|
|
2024-01-16 01:58:20 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/portal/db/models"
|
2024-02-24 13:19:27 +00:00
|
|
|
"github.com/go-gorm/caches/v4"
|
2024-01-28 07:20:59 +00:00
|
|
|
"go.uber.org/fx"
|
2024-01-16 01:58:20 +00:00
|
|
|
"gorm.io/driver/mysql"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
type DatabaseParams struct {
|
|
|
|
fx.In
|
2024-02-24 13:41:40 +00:00
|
|
|
Config *config.Manager
|
|
|
|
Logger *zap.Logger
|
|
|
|
LoggerLevel *zap.AtomicLevel
|
2024-01-16 01:58:20 +00:00
|
|
|
}
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
var Module = fx.Module("db",
|
|
|
|
fx.Options(
|
|
|
|
fx.Provide(NewDatabase),
|
|
|
|
),
|
|
|
|
)
|
2024-01-16 01:58:20 +00:00
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
func NewDatabase(lc fx.Lifecycle, params DatabaseParams) *gorm.DB {
|
2024-02-22 08:30:40 +00:00
|
|
|
username := params.Config.Config().Core.DB.Username
|
|
|
|
password := params.Config.Config().Core.DB.Password
|
|
|
|
host := params.Config.Config().Core.DB.Host
|
|
|
|
port := params.Config.Config().Core.DB.Port
|
|
|
|
dbname := params.Config.Config().Core.DB.Name
|
|
|
|
charset := params.Config.Config().Core.DB.Charset
|
2024-01-16 01:58:20 +00:00
|
|
|
|
2024-02-22 08:32:47 +00:00
|
|
|
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True&loc=Local", username, password, host, port, dbname, charset)
|
2024-01-16 01:58:20 +00:00
|
|
|
|
2024-02-24 13:41:40 +00:00
|
|
|
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
|
|
|
|
Logger: newLogger(params.Logger, params.LoggerLevel),
|
|
|
|
})
|
2024-01-16 01:58:20 +00:00
|
|
|
if err != nil {
|
2024-01-28 07:20:59 +00:00
|
|
|
panic(err)
|
2024-01-16 01:58:20 +00:00
|
|
|
}
|
2024-01-16 06:24:47 +00:00
|
|
|
|
2024-02-24 13:19:27 +00:00
|
|
|
cacher := getCacher(params.Config, params.Logger)
|
|
|
|
if cacher != nil {
|
|
|
|
cache := &caches.Caches{Conf: &caches.Config{
|
|
|
|
Cacher: cacher,
|
|
|
|
}}
|
|
|
|
err := db.Use(cache)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
lc.Append(fx.Hook{
|
|
|
|
OnStart: func(ctx context.Context) error {
|
|
|
|
return db.AutoMigrate(
|
|
|
|
&models.APIKey{},
|
|
|
|
&models.Blocklist{},
|
2024-02-25 14:47:40 +00:00
|
|
|
&models.DNSLink{},
|
2024-01-28 07:20:59 +00:00
|
|
|
&models.Download{},
|
2024-02-26 12:47:05 +00:00
|
|
|
&models.EmailVerification{},
|
|
|
|
&models.PasswordReset{},
|
2024-01-28 07:20:59 +00:00
|
|
|
&models.Pin{},
|
|
|
|
&models.PublicKey{},
|
|
|
|
&models.Upload{},
|
|
|
|
&models.User{},
|
2024-02-28 16:36:53 +00:00
|
|
|
&models.S3Upload{},
|
2024-01-28 07:20:59 +00:00
|
|
|
&models.S5Challenge{},
|
|
|
|
&models.TusLock{},
|
|
|
|
&models.TusUpload{},
|
|
|
|
)
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return db
|
2024-01-16 06:24:47 +00:00
|
|
|
}
|
2024-02-24 13:19:27 +00:00
|
|
|
|
|
|
|
func getCacheMode(cm *config.Manager, logger *zap.Logger) string {
|
|
|
|
|
|
|
|
if cm.Config().Core.DB.Cache == nil {
|
|
|
|
return "none"
|
|
|
|
}
|
|
|
|
|
|
|
|
switch cm.Config().Core.DB.Cache.Mode {
|
|
|
|
case "", "none":
|
|
|
|
return "none"
|
|
|
|
case "memory":
|
|
|
|
return "memory"
|
|
|
|
case "redis":
|
|
|
|
return "redis"
|
|
|
|
default:
|
|
|
|
logger.Fatal("invalid cache mode", zap.String("mode", cm.Config().Core.DB.Cache.Mode))
|
|
|
|
}
|
|
|
|
|
|
|
|
return "none"
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCacher(cm *config.Manager, logger *zap.Logger) caches.Cacher {
|
|
|
|
mode := getCacheMode(cm, logger)
|
|
|
|
|
|
|
|
switch mode {
|
|
|
|
case "none":
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case "memory":
|
|
|
|
return &memoryCacher{}
|
|
|
|
case "redis":
|
|
|
|
rcfg, ok := cm.Config().Core.DB.Cache.Options.(config.RedisConfig)
|
|
|
|
if !ok {
|
|
|
|
logger.Fatal("invalid redis config")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &redisCacher{
|
|
|
|
redis.NewClient(&redis.Options{
|
|
|
|
Addr: rcfg.Address,
|
|
|
|
Password: rcfg.Password,
|
|
|
|
DB: rcfg.DB,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|