This commit is contained in:
oneao committed 2026-04-02 22:07:21 +08:00
1 parent ae0d245605
commit 4725ee75ca
26 files changed
+1089 -67

No files matched your search

@@ -0,0 +1,18 @@
package boots
import (
"allapp-go/internal/config"
"allapp-go/pkg/logger"
)
func InitLogger(cfg *config.Config) {
logger.Init(logger.Config{
Dir: "logs",
Level: "info",
MaxSize: 100, // MB
MaxBackups: 10,
MaxAge: 7, // 天
Compress: true,
})
defer logger.Sync()
}
@@ -0,0 +1,11 @@
package boots
import (
"allapp-go/internal/config"
"allapp-go/pkg/jwtx"
)
// InitJwt 初始化Jwt
func InitJwt(cfg *config.Config) {
jwtx.Init(cfg.JWT.Secret, cfg.JWT.AccessExpiry)
}
@@ -0,0 +1,84 @@
package boots
import (
"allapp-go/internal/config"
"context"
"fmt"
"net/url"
"time"
"github.com/jackc/pgx/v5/pgxpool"
)
func InitPgsql(cfg *config.Config) (*pgxpool.Pool, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
pg := cfg.Postgres
// ssl
sslMode := "disable"
if pg.SslMode {
sslMode = "require"
}
// 密码转义
password := url.QueryEscape(pg.Password)
// DSN
dsn := fmt.Sprintf(
"postgres://%s:%s@%s:%d/%s?sslmode=%s",
pg.User,
password,
pg.Host,
pg.Port,
pg.Dbname,
sslMode,
)
// 解析配置
conf, err := pgxpool.ParseConfig(dsn)
if err != nil {
return nil, err
}
// ======================
// 连接池配置
// ======================
// 最大连接数
if pg.MaxOpenConns > 0 {
conf.MaxConns = int32(pg.MaxOpenConns)
}
// 最小连接数(近似 idle)
if pg.MaxIdleConns > 0 {
conf.MinConns = int32(pg.MaxIdleConns)
}
// 连接最大生命周期
if pg.ConnMaxLifetime > 0 {
conf.MaxConnLifetime = pg.ConnMaxLifetime
}
// ======================
// 时区设置(正确方式 ⭐)
// ======================
if pg.TimeZone != "" {
conf.ConnConfig.RuntimeParams["TimeZone"] = pg.TimeZone
}
// 创建连接池
db, err := pgxpool.NewWithConfig(ctx, conf)
if err != nil {
return nil, err
}
// 测试连接
if err := db.Ping(ctx); err != nil {
db.Close()
return nil, err
}
return db, nil
}
@@ -0,0 +1,32 @@
package boots
import (
"allapp-go/internal/config"
"allapp-go/internal/router"
"fmt"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/requestid"
)
func RunServer(cfg *config.Config) (*fiber.App, error) {
app := fiber.New()
// ===== middleware =====
app.Use(requestid.New())
// TODO: 你可以加 logger middleware / recover
// ===== router =====
router.Setup(app)
addr := fmt.Sprintf(":%d", cfg.App.Port)
go func() {
if err := app.Listen(addr); err != nil {
panic(err)
}
}()
return app, nil
}
@@ -0,0 +1,12 @@
package boots
import (
"allapp-go/internal/config"
"allapp-go/pkg/uniqueid"
)
// InitUniqueId 初始化雪花id
func InitUniqueId(cfg *config.Config) {
options := uniqueid.NewIdGeneratorOptions(cfg.UniqueID.WorkerID)
uniqueid.SetIdGenerator(options)
}