Compare commits
No commits in common. "941deaf9df0d9a1534d8e9fb3defe82dec3b15a1" and "d54fbcacf0ad21edfa78d0a02e53929aa79c077d" have entirely different histories.
941deaf9df
...
d54fbcacf0
33
app.go
33
app.go
@ -64,9 +64,6 @@ type Config struct {
|
|||||||
// default map[string]DatabaseConfig{}
|
// default map[string]DatabaseConfig{}
|
||||||
Databases map[string]DatabaseConfig
|
Databases map[string]DatabaseConfig
|
||||||
|
|
||||||
// default false
|
|
||||||
CreateRouter bool
|
|
||||||
|
|
||||||
// default false
|
// default false
|
||||||
CreateSSEBroker bool
|
CreateSSEBroker bool
|
||||||
|
|
||||||
@ -90,7 +87,7 @@ type App struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Paseto struct {
|
type Paseto struct {
|
||||||
SecretKey paseto.V4AsymmetricSecretKey
|
AsymmetricKey paseto.V4AsymmetricSecretKey
|
||||||
PublicKey paseto.V4AsymmetricPublicKey
|
PublicKey paseto.V4AsymmetricPublicKey
|
||||||
Duration time.Duration
|
Duration time.Duration
|
||||||
}
|
}
|
||||||
@ -104,7 +101,6 @@ func NewApp(config ...Config) *App {
|
|||||||
Timezone: "UTC",
|
Timezone: "UTC",
|
||||||
Paseto: nil,
|
Paseto: nil,
|
||||||
Databases: make(map[string]DatabaseConfig),
|
Databases: make(map[string]DatabaseConfig),
|
||||||
CreateRouter: false,
|
|
||||||
CreateSSEBroker: false,
|
CreateSSEBroker: false,
|
||||||
CreateSession: false,
|
CreateSession: false,
|
||||||
CreateMailer: false,
|
CreateMailer: false,
|
||||||
@ -166,11 +162,11 @@ func NewApp(config ...Config) *App {
|
|||||||
var ak paseto.V4AsymmetricSecretKey
|
var ak paseto.V4AsymmetricSecretKey
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if os.Getenv("PASETO_SECRET_KEY") != "" {
|
if os.Getenv("PASETO_ASYMMETRIC_KEY") != "" {
|
||||||
slog.Debug("using paseto secret key from env")
|
slog.Debug("using paseto asymmetric key from env")
|
||||||
ak, err = paseto.NewV4AsymmetricSecretKeyFromHex(os.Getenv("PASETO_SECRET_KEY"))
|
ak, err = paseto.NewV4AsymmetricSecretKeyFromHex(os.Getenv("PASETO_ASYMMETRIC_KEY"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("error creating secret key", "error", err)
|
slog.Error("error creating asymmetric key", "error", err)
|
||||||
ak = paseto.NewV4AsymmetricSecretKey()
|
ak = paseto.NewV4AsymmetricSecretKey()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -191,24 +187,15 @@ func NewApp(config ...Config) *App {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cfg.Paseto = &Paseto{
|
cfg.Paseto = &Paseto{
|
||||||
SecretKey: ak,
|
AsymmetricKey: ak,
|
||||||
PublicKey: pk,
|
PublicKey: pk,
|
||||||
Duration: duration,
|
Duration: duration,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
app := &App{config: cfg}
|
app := &App{
|
||||||
|
config: cfg,
|
||||||
if cfg.CreateRouter {
|
Router: newRouter(),
|
||||||
app.Router = newRouter()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create PGX pools automatically if there are entries in Databases with driver 'pgx'
|
|
||||||
for dbName, dbConfig := range cfg.Databases {
|
|
||||||
if dbConfig.DriverName == "pgx" {
|
|
||||||
slog.Debug("creating pgx pool", "database", dbName)
|
|
||||||
app.newPGXPool(dbName)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
slog.Info(
|
slog.Info(
|
||||||
@ -228,7 +215,7 @@ func NewApp(config ...Config) *App {
|
|||||||
)
|
)
|
||||||
|
|
||||||
if cfg.EnvMode != EnvironmentProduction {
|
if cfg.EnvMode != EnvironmentProduction {
|
||||||
slog.Debug("paseto_secret_key", "key", cfg.Paseto.SecretKey.ExportHex())
|
slog.Debug("paseto_assymetric_key", "key", cfg.Paseto.AsymmetricKey.ExportHex())
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.CreateSSEBroker {
|
if cfg.CreateSSEBroker {
|
||||||
|
|||||||
24
pgx.go
24
pgx.go
@ -20,7 +20,7 @@ var (
|
|||||||
pgxMutex sync.RWMutex
|
pgxMutex sync.RWMutex
|
||||||
)
|
)
|
||||||
|
|
||||||
func (a *App) newPGXPool(name string) *pgxpool.Pool {
|
func (a *App) NewPGXPool(name string) *pgxpool.Pool {
|
||||||
pgxMutex.Lock()
|
pgxMutex.Lock()
|
||||||
defer pgxMutex.Unlock()
|
defer pgxMutex.Unlock()
|
||||||
|
|
||||||
@ -44,32 +44,22 @@ func (a *App) newPGXPool(name string) *pgxpool.Pool {
|
|||||||
return dbPool
|
return dbPool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) GetPGXPool(name string) *pgxpool.Pool {
|
func (a *App) GetPGXPool(name string) (*pgxpool.Pool, bool) {
|
||||||
pgxMutex.RLock()
|
pgxMutex.RLock()
|
||||||
defer pgxMutex.RUnlock()
|
defer pgxMutex.RUnlock()
|
||||||
|
|
||||||
pool, exists := pgxPools[name]
|
pool, exists := pgxPools[name]
|
||||||
if !exists {
|
return pool, exists
|
||||||
slog.Error("database connection not found", "name", name)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return pool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) ClosePGXPool(name string) {
|
func (a *App) ClosePGXPools() {
|
||||||
pgxMutex.Lock()
|
pgxMutex.Lock()
|
||||||
defer pgxMutex.Unlock()
|
defer pgxMutex.Unlock()
|
||||||
|
|
||||||
pool, exists := pgxPools[name]
|
for name, pool := range pgxPools {
|
||||||
if !exists {
|
|
||||||
slog.Error("database connection not found", "name", name)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
pool.Close()
|
pool.Close()
|
||||||
delete(pgxPools, name)
|
delete(pgxPools, name)
|
||||||
slog.Info("closed database connection", "name", name)
|
slog.Info("closed database connection", "name", name)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NumericToFloat64(n pgtype.Numeric) float64 {
|
func NumericToFloat64(n pgtype.Numeric) float64 {
|
||||||
@ -100,7 +90,7 @@ func NumericToInt64(n pgtype.Numeric) int64 {
|
|||||||
|
|
||||||
func FloatToNumeric(number float64, precision int) (value pgtype.Numeric) {
|
func FloatToNumeric(number float64, precision int) (value pgtype.Numeric) {
|
||||||
parse := strconv.FormatFloat(number, 'f', precision, 64)
|
parse := strconv.FormatFloat(number, 'f', precision, 64)
|
||||||
slog.Debug("parse", "parse", parse)
|
slog.Info("parse", "parse", parse)
|
||||||
|
|
||||||
if err := value.Scan(parse); err != nil {
|
if err := value.Scan(parse); err != nil {
|
||||||
slog.Error("error scanning numeric", "error", err)
|
slog.Error("error scanning numeric", "error", err)
|
||||||
|
|||||||
15
router.go
15
router.go
@ -29,25 +29,12 @@ func (r *Router) Use(mw ...Middleware) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Router) Group(basePath string, fn func(r *Router)) {
|
func (r *Router) Group(fn func(r *Router)) {
|
||||||
sub := &Router{
|
sub := &Router{
|
||||||
ServeMux: r.ServeMux,
|
ServeMux: r.ServeMux,
|
||||||
routeChain: slices.Clone(r.routeChain),
|
routeChain: slices.Clone(r.routeChain),
|
||||||
isSub: true,
|
isSub: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Añadir middleware para manejar el basePath
|
|
||||||
sub.Use(func(next http.Handler) http.Handler {
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
||||||
if !strings.HasPrefix(req.URL.Path, basePath) {
|
|
||||||
http.NotFound(w, req)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
req.URL.Path = strings.TrimPrefix(req.URL.Path, basePath)
|
|
||||||
next.ServeHTTP(w, req)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
fn(sub)
|
fn(sub)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user