gohttpserver/internal/database/database.go
sneak fb347b96df Replace zerolog with log/slog from stdlib
- Rewrite logger package to use slog with LevelVar for dynamic levels
- Update all packages to use *slog.Logger instead of *zerolog.Logger
- Use TextHandler for TTY (dev), JSONHandler for production
- Add make check target (runs lint + test)
- Add make test target
2025-12-27 12:02:05 +07:00

53 lines
1.1 KiB
Go

package database
import (
"context"
"log/slog"
"git.eeqj.de/sneak/gohttpserver/internal/config"
"git.eeqj.de/sneak/gohttpserver/internal/logger"
"go.uber.org/fx"
// spooky action at a distance!
// this populates the environment
// from a ./.env file automatically
// for development configuration.
// .env contents should be things like
// `DBURL=postgres://user:pass@.../`
// (without the backticks, of course)
_ "github.com/joho/godotenv/autoload"
)
type DatabaseParams struct {
fx.In
Logger *logger.Logger
Config *config.Config
}
type Database struct {
URL string
log *slog.Logger
params *DatabaseParams
}
func New(lc fx.Lifecycle, params DatabaseParams) (*Database, error) {
s := new(Database)
s.params = &params
s.log = params.Logger.Get()
s.log.Info("Database instantiated")
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
s.log.Info("Database OnStart Hook")
// FIXME connect to db
return nil
},
OnStop: func(ctx context.Context) error {
// FIXME disconnect from db
return nil
},
})
return s, nil
}