Перенести пользователей в PostgreSQL и добавить пароли
This commit is contained in:
+24
-10
@@ -1,4 +1,4 @@
|
||||
// Command server starts the in-memory messenger API.
|
||||
// Command server starts the messenger API over PostgreSQL.
|
||||
package main
|
||||
|
||||
import (
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
"messenger/internal/app"
|
||||
"messenger/internal/platform/config"
|
||||
"messenger/internal/platform/postgres"
|
||||
"messenger/internal/server"
|
||||
)
|
||||
|
||||
@@ -23,23 +24,36 @@ func main() {
|
||||
}
|
||||
log := newLogger(cfg)
|
||||
|
||||
ln, err := net.Listen("tcp", cfg.Addr())
|
||||
if err != nil {
|
||||
log.Error("listen failed", "addr", cfg.Addr(), "error", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
|
||||
defer stop()
|
||||
|
||||
log.Info("starting", "env", cfg.Env, "addr", cfg.Addr(), "shutdown_timeout", cfg.ShutdownTimeout.String())
|
||||
a := app.New(log, cfg.MaxBodyBytes)
|
||||
if err := server.Run(ctx, log, ln, a.Engine, a.Health, cfg.ShutdownTimeout); err != nil {
|
||||
if err := run(ctx, log, cfg); err != nil {
|
||||
log.Error("server stopped with error", "error", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// run connects the dependencies before it listens, so the port accepts
|
||||
// connections only once the schema is in place.
|
||||
func run(ctx context.Context, log *slog.Logger, cfg config.Config) error {
|
||||
pool, err := postgres.NewPool(ctx, cfg.DatabaseURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
if err := postgres.Migrate(ctx, pool); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ln, err := net.Listen("tcp", cfg.Addr())
|
||||
if err != nil {
|
||||
return fmt.Errorf("listen on %s: %w", cfg.Addr(), err)
|
||||
}
|
||||
log.Info("starting", "env", cfg.Env, "addr", cfg.Addr(), "shutdown_timeout", cfg.ShutdownTimeout.String())
|
||||
a := app.New(app.Deps{Log: log, DB: pool, MaxBodyBytes: cfg.MaxBodyBytes})
|
||||
return server.Run(ctx, log, ln, a.Engine, a.Health, cfg.ShutdownTimeout)
|
||||
}
|
||||
|
||||
func newLogger(cfg config.Config) *slog.Logger {
|
||||
opts := &slog.HandlerOptions{Level: cfg.LogLevel}
|
||||
if cfg.Env == "dev" {
|
||||
|
||||
Reference in New Issue
Block a user