Refactor link repository and improve logging

This commit is contained in:
Andrey Chervyakov 2021-03-18 20:53:59 +06:00
parent ddaf0dcbfc
commit 458ae28901
7 changed files with 63 additions and 19 deletions

View file

@ -14,8 +14,7 @@ func main() {
conf := cgnolink.NewConfig()
pool := database.Pool(conf)
database.Migrate(pool)
pool := database.InitPool(conf)
server := appserver.NewServer(conf, pool)
@ -23,5 +22,5 @@ func main() {
}
func configureLogger() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: "2006-01-02 15:04:05"})
}

View file

@ -6,16 +6,34 @@ import (
"fmt"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/knadh/koanf"
"github.com/rs/zerolog/log"
"time"
)
func Pool(config *koanf.Koanf) *pgxpool.Pool {
connStr, err := getConnectionString(config)
if err != nil {
func InitPool(config *koanf.Koanf) *pgxpool.Pool {
pool := NewPool(config)
if err := runMigrations(pool); err != nil {
log.Fatal().Err(err).Msg("Couldn't apply migrations")
panic(err)
}
pool, err := pgxpool.Connect(context.Background(), connStr)
return pool
}
func NewPool(config *koanf.Koanf) *pgxpool.Pool {
connStr, err := getConnectionString(config)
if err != nil {
log.Fatal().Err(err).Msg("Couldn't construct DB connection string")
panic(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
pool, err := pgxpool.Connect(ctx, connStr)
if err != nil {
log.Fatal().Err(err).Msg("Couldn't connect to DB")
panic(err)
}

View file

@ -4,24 +4,30 @@ import (
"context"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/jackc/tern/migrate"
"time"
)
func Migrate(pool *pgxpool.Pool) {
conn, err := pool.Acquire(context.Background())
func runMigrations(pool *pgxpool.Pool) (err error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
conn, err := pool.Acquire(ctx)
if err != nil {
panic(err)
return
}
migrator, err := migrate.NewMigrator(context.Background(), conn.Conn(), "schema_version")
migrator, err := migrate.NewMigrator(ctx, conn.Conn(), "schema_version")
if err != nil {
panic(err)
return
}
if err = migrator.LoadMigrations("./migrations"); err != nil {
panic(err)
return
}
if err = migrator.Migrate(context.Background()); err != nil {
panic(err)
if err = migrator.Migrate(ctx); err != nil {
return
}
return
}

15
database/util.go Normal file
View file

@ -0,0 +1,15 @@
package database
import (
"github.com/jackc/pgx/v4/pgxpool"
"github.com/rs/zerolog/log"
)
func LogPoolState(pool *pgxpool.Pool, message string) {
stat := pool.Stat()
log.Info().
Int32("acquired_conns", stat.AcquiredConns()).
Int32("idle_conns", stat.IdleConns()).
Int32("total_conns", stat.TotalConns()).
Msg(message)
}

View file

@ -1,6 +1,7 @@
package link
import (
"cgnolink/database"
"context"
"errors"
"github.com/jackc/pgtype"
@ -15,7 +16,7 @@ const defaultContextTimeout = 10 * time.Second
type Repository interface {
Save(link *Link) error
FindById(id string) (*Link, error)
FindAll(limit int, offset int) (Links, error)
GetAll(limit int, offset int) (Links, error)
Update(link *Link) error
DeleteById(id string) error
}
@ -37,6 +38,7 @@ func (r *PgRepository) Save(link *Link) error {
VALUES ($1, $2, $3, $4::timestamp)
`
database.LogPoolState(r.pool, "Saving link")
_, err := r.pool.Exec(ctx, sql, link.Id, link.Name, link.RedirectURL.String(), link.CreationTime.Format("2006-01-02 15:04:05"))
if err != nil {
return err
@ -55,6 +57,7 @@ func (r *PgRepository) FindById(id string) (*Link, error) {
WHERE id = $1
`
database.LogPoolState(r.pool, "Finding link by ID")
entity, err := mapRowToEntity(r.pool.QueryRow(ctx, sql, id))
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
@ -67,7 +70,7 @@ func (r *PgRepository) FindById(id string) (*Link, error) {
return entity, nil
}
func (r *PgRepository) FindAll(limit int, offset int) (Links, error) {
func (r *PgRepository) GetAll(limit int, offset int) (Links, error) {
if limit < 0 {
return nil, errors.New("limit can't be negative")
}
@ -85,6 +88,7 @@ func (r *PgRepository) FindAll(limit int, offset int) (Links, error) {
OFFSET $2
`
database.LogPoolState(r.pool, "Getting all links")
rows, err := r.pool.Query(ctx, sql, limit, offset)
if err != nil {
return nil, err
@ -122,6 +126,7 @@ func (r *PgRepository) Update(link *Link) error {
WHERE id = $3
`
database.LogPoolState(r.pool, "Updating link")
_, err := r.pool.Exec(ctx, sql, link.Name, link.RedirectURL.String(), link.Id)
if err != nil {
return err
@ -138,6 +143,7 @@ func (r *PgRepository) DeleteById(id string) error {
DELETE FROM links WHERE id = $1
`
database.LogPoolState(r.pool, "Deleting link")
_, err := r.pool.Exec(ctx, sql, id)
if err != nil {
return err

View file

@ -64,7 +64,7 @@ func (s *PgService) GetById(id string) (*Link, error) {
}
func (s *PgService) GetAll(limit int, offset int) (Links, error) {
links, err := s.rep.FindAll(limit, offset)
links, err := s.rep.GetAll(limit, offset)
if err != nil {
return nil, apperrors.UnknownError{Err: err}
}

View file

@ -26,7 +26,7 @@ func Logger() echo.MiddlewareFunc {
Str("user_agent", req.UserAgent()).
Int("status", res.Status).
Str("latency", stop.Sub(start).String()).
Send()
Msg("Request")
return
}