forked from mtusi2026/mtusi2026lab7
lab7: безопасность, mTLS, OpenBao и PKI lifecycle
This commit is contained in:
+21
-17
@@ -7,14 +7,16 @@ import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/jackc/pgx/v5/stdlib"
|
||||
"github.com/pressly/goose/v3"
|
||||
|
||||
"mtusi-messenger/internal/config"
|
||||
"mtusi-messenger/internal/logger"
|
||||
"mtusi-messenger/internal/storage/postgres"
|
||||
"mtusi-messenger/internal/storage/postgres/migrations"
|
||||
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -26,8 +28,10 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func run(args []string) error {
|
||||
cfg, err := config.Load()
|
||||
func run(args []string) (err error) {
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
cfg, err := config.LoadOperator()
|
||||
if err != nil {
|
||||
return fmt.Errorf("load config: %w", err)
|
||||
}
|
||||
@@ -42,14 +46,14 @@ func run(args []string) error {
|
||||
command = args[0]
|
||||
}
|
||||
|
||||
db, err := sql.Open("pgx", cfg.Postgres.DSN())
|
||||
poolCfg, err := postgres.PoolConfig(cfg.Postgres)
|
||||
if err != nil {
|
||||
return fmt.Errorf("open database: %w", err)
|
||||
return fmt.Errorf("database config: %w", err)
|
||||
}
|
||||
db := stdlib.OpenDB(*poolCfg.ConnConfig)
|
||||
defer func() { err = errors.Join(err, db.Close()) }()
|
||||
|
||||
defer func() { _ = db.Close() }()
|
||||
|
||||
pingCtx, cancel := context.WithTimeout(context.Background(), cfg.Postgres.ConnectTimeout)
|
||||
pingCtx, cancel := context.WithTimeout(ctx, cfg.Postgres.ConnectTimeout)
|
||||
defer cancel()
|
||||
|
||||
if err := db.PingContext(pingCtx); err != nil {
|
||||
@@ -62,27 +66,27 @@ func run(args []string) error {
|
||||
return fmt.Errorf("set dialect: %w", err)
|
||||
}
|
||||
|
||||
if err := goCommand(db, command); err != nil {
|
||||
if err := goCommand(ctx, db, command); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func goCommand(db *sql.DB, command string) error {
|
||||
func goCommand(ctx context.Context, db *sql.DB, command string) error {
|
||||
switch command {
|
||||
case "up":
|
||||
return goose.Up(db, ".")
|
||||
return goose.UpContext(ctx, db, ".")
|
||||
case "up-by-one":
|
||||
return goose.UpByOne(db, ".")
|
||||
return goose.UpByOneContext(ctx, db, ".")
|
||||
case "down":
|
||||
return goose.Down(db, ".")
|
||||
return goose.DownContext(ctx, db, ".")
|
||||
case "status":
|
||||
return goose.Status(db, ".")
|
||||
return goose.StatusContext(ctx, db, ".")
|
||||
case "version":
|
||||
return goose.Version(db, ".")
|
||||
return goose.VersionContext(ctx, db, ".")
|
||||
case "redo":
|
||||
return goose.Redo(db, ".")
|
||||
return goose.RedoContext(ctx, db, ".")
|
||||
default:
|
||||
return errors.New("unknown command: " + command + " (up, up-by-one, down, status, version, redo)")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user