u
This commit is contained in:
1 parent
4ce951dd56
commit
8a3dea8545
44 files changed
+1599
-30
No files matched your search
@@ -0,0 +1,50 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"allapp/db/repo"
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func initDB() (*pgxpool.Pool, *repo.Queries, error) {
|
||||
cfg := conf.Postgres
|
||||
|
||||
sslMode := "disable"
|
||||
if cfg.SslMode {
|
||||
sslMode = "require"
|
||||
}
|
||||
|
||||
dsn := fmt.Sprintf(
|
||||
"postgres://%s:%s@%s:%d/%s?sslmode=%s&TimeZone=%s",
|
||||
cfg.User, cfg.Password, cfg.Host, cfg.Port, cfg.Dbname, sslMode, cfg.TimeZone,
|
||||
)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// 使用可修改配置
|
||||
cfgPool, err := pgxpool.ParseConfig(dsn)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("parse pgx config failed: %w", err)
|
||||
}
|
||||
|
||||
cfgPool.MaxConns = cfg.MaxOpenConns
|
||||
cfgPool.MinConns = cfg.MaxIdleConns
|
||||
cfgPool.MaxConnLifetime = cfg.ConnMaxLifetime
|
||||
|
||||
pool, err := pgxpool.NewWithConfig(ctx, cfgPool)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("create pgx pool failed: %w", err)
|
||||
}
|
||||
|
||||
// 测试连接
|
||||
if err := pool.Ping(ctx); err != nil {
|
||||
return nil, nil, fmt.Errorf("ping database failed: %w", err)
|
||||
}
|
||||
|
||||
queries := repo.New(pool)
|
||||
return pool, queries, nil
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"allapp/db"
|
||||
"allapp/utils/redisx"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -25,6 +27,22 @@ func InitConfig() error {
|
||||
initLogger()
|
||||
initUniqueId()
|
||||
|
||||
// 加载数据库
|
||||
pool, queries, err := initDB()
|
||||
if err != nil {
|
||||
return fmt.Errorf("加载数据库失败: %w", err)
|
||||
}
|
||||
|
||||
db.Pool = pool
|
||||
db.Queries = queries
|
||||
|
||||
// 加载 redis
|
||||
redis, err := initRedis()
|
||||
if err != nil {
|
||||
return fmt.Errorf("加载Redis失败: %w", err)
|
||||
}
|
||||
redisx.Client = redis
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -59,6 +77,9 @@ func defaultConfig() *Config {
|
||||
MaxAge: 30,
|
||||
Compress: true,
|
||||
},
|
||||
Redis: RedisConfig{
|
||||
Port: 6379,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ func initLogger() {
|
||||
ExtraKeys: []string{"request_id"},
|
||||
ZapOptions: []zap.Option{
|
||||
zap.AddCaller(), // 打开 caller
|
||||
zap.AddCallerSkip(2), // 跳过一层 Fiber 调用
|
||||
zap.AddCallerSkip(2), // 跳过 2 层 Fiber 调用
|
||||
},
|
||||
CoreConfigs: []fiberZap.CoreConfig{
|
||||
{
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func initRedis() (*redis.Client, error) {
|
||||
cfg := GetConf().Redis
|
||||
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port),
|
||||
Password: cfg.Password,
|
||||
DB: cfg.Db,
|
||||
})
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if err := rdb.Ping(ctx).Err(); err != nil {
|
||||
_ = rdb.Close()
|
||||
return nil, fmt.Errorf("redis ping failed: %w", err)
|
||||
}
|
||||
|
||||
return rdb, nil
|
||||
}
|
||||
@@ -11,6 +11,7 @@ type Config struct {
|
||||
Postgres PostgresConfig `mapstructure:"postgres"`
|
||||
Log LogConfig `mapstructure:"log"`
|
||||
Wechat WechatConfig `mapstructure:"wechat"`
|
||||
Redis RedisConfig `mapstructure:"redis"`
|
||||
}
|
||||
|
||||
type ServerConfig struct {
|
||||
@@ -55,3 +56,10 @@ type WechatConfig struct {
|
||||
AppId string `mapstructure:"app_id"`
|
||||
AppSecret string `mapstructure:"app_secret"`
|
||||
}
|
||||
|
||||
type RedisConfig struct {
|
||||
Host string `mapstructure:"host"`
|
||||
Port int `mapstructure:"port"`
|
||||
Db int `mapstructure:"db"`
|
||||
Password string `mapstructure:"password"`
|
||||
}
|
||||
Reference in new issue
Block a user