u
This commit is contained in:
1 parent
c6671c8a8c
commit
f0cb963d23
76 files changed
+6467
-498
No files matched your search
@@ -0,0 +1,58 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"allapp-go/internal/bootstrap/boots"
|
||||
"allapp-go/internal/config"
|
||||
"allapp-go/pkg/db"
|
||||
"allapp-go/pkg/logger"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type App struct {
|
||||
Config *config.Config
|
||||
PgSql *pgxpool.Pool
|
||||
}
|
||||
|
||||
// ======================
|
||||
// 初始化
|
||||
// ======================
|
||||
|
||||
func NewApp() (*App, error) {
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 日志
|
||||
boots.InitLogger(cfg)
|
||||
|
||||
// 数据库
|
||||
pgsql, err := boots.InitPgsql(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
db.Init(pgsql)
|
||||
|
||||
// 其他组件
|
||||
boots.InitUniqueId(cfg)
|
||||
boots.InitJwt(cfg)
|
||||
|
||||
return &App{
|
||||
Config: cfg,
|
||||
PgSql: pgsql,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ======================
|
||||
// 释放资源
|
||||
// ======================
|
||||
|
||||
func (a *App) Close() {
|
||||
if a.PgSql != nil {
|
||||
a.PgSql.Close()
|
||||
}
|
||||
|
||||
logger.Sync()
|
||||
}
|
||||
@@ -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,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)
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"allapp-go/internal/config"
|
||||
"allapp-go/internal/errors"
|
||||
"allapp-go/internal/middleware"
|
||||
"allapp-go/internal/router"
|
||||
"allapp-go/pkg/logger"
|
||||
"fmt"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/gofiber/fiber/v3/middleware/recover"
|
||||
"github.com/gofiber/fiber/v3/middleware/requestid"
|
||||
)
|
||||
|
||||
func RunServer(cfg *config.Config) error {
|
||||
app := fiber.New(fiber.Config{
|
||||
ErrorHandler: errors.ErrorHandler(),
|
||||
})
|
||||
|
||||
// ===== middleware =====
|
||||
app.Use(recover.New())
|
||||
app.Use(requestid.New())
|
||||
app.Use(middleware.Logger())
|
||||
|
||||
// ===== router =====
|
||||
router.SetupRouter(app)
|
||||
|
||||
addr := fmt.Sprintf(":%d", cfg.App.Port)
|
||||
|
||||
logger.Log.Info("服务启动")
|
||||
|
||||
return app.Listen(addr)
|
||||
}
|
||||
Reference in new issue
Block a user