Initial commit: VPN panel on Go, PostgreSQL 17, Docker, Xray-core

This commit is contained in:
vpn-panel
2026-05-21 18:55:14 +03:00
commit 3c2f5226d1
27 changed files with 1778 additions and 0 deletions
+45
View File
@@ -0,0 +1,45 @@
package database
import (
"context"
"embed"
"fmt"
"strings"
"github.com/jackc/pgx/v5/pgxpool"
)
//go:embed migrations/*.sql
var migrationFS embed.FS
func Connect(ctx context.Context, databaseURL string) (*pgxpool.Pool, error) {
pool, err := pgxpool.New(ctx, databaseURL)
if err != nil {
return nil, fmt.Errorf("подключение к PostgreSQL: %w", err)
}
if err := pool.Ping(ctx); err != nil {
pool.Close()
return nil, fmt.Errorf("ping PostgreSQL: %w", err)
}
return pool, nil
}
func Migrate(ctx context.Context, pool *pgxpool.Pool) error {
entries, err := migrationFS.ReadDir("migrations")
if err != nil {
return err
}
for _, e := range entries {
if e.IsDir() || !strings.HasSuffix(e.Name(), ".sql") {
continue
}
data, err := migrationFS.ReadFile("migrations/" + e.Name())
if err != nil {
return err
}
if _, err := pool.Exec(ctx, string(data)); err != nil {
return fmt.Errorf("миграция %s: %w", e.Name(), err)
}
}
return nil
}