Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion internal/pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
)

type App struct {
cfg *config

logger *slog.Logger

dbConn *pgxpool.Pool
Expand All @@ -34,7 +36,7 @@ func NewApp() (*App, error) {

app := &App{}
// - config
// TODO: add config
app.initConfig()
// - logger
app.initLogger()
// - db
Expand Down
13 changes: 13 additions & 0 deletions internal/pkg/app/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package app

type config struct {
HTTP *configHTTP
DB *configDB
}

func (a *App) initConfig() {
c := new(config)
c.HTTP = newHTTPConfig()
c.DB = newDBConfig()
a.cfg = c
}
14 changes: 2 additions & 12 deletions internal/pkg/app/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,17 @@ package app

import (
"context"
"os"
"time"

"github.com/jackc/pgx/v4/pgxpool"
)

func (a *App) initDBConn() error {
databaseURL := os.Getenv("DATABASE_URL")

config, err := pgxpool.ParseConfig(databaseURL)
cfg, err := pgxpool.ParseConfig(a.cfg.DB.URL)
if err != nil {
return err
}

config.MaxConns = 10
config.MaxConnIdleTime = 30 * time.Minute

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

db, err := pgxpool.ConnectConfig(ctx, config)
db, err := pgxpool.ConnectConfig(context.TODO(), cfg)
if err != nil {
return err
}
Expand Down
24 changes: 24 additions & 0 deletions internal/pkg/app/db_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package app

import "os"

type configDB struct {
URL string
}

func newDBConfig() *configDB {
c := &configDB{}
c.Load()
c.Validate()
return c
}

func (c *configDB) Load() {
c.URL = os.Getenv("DATABASE_URL")
}

func (c *configDB) Validate() {
if c.URL == "" {
panic("env DATABASE_URL must be set")
}
}
15 changes: 9 additions & 6 deletions internal/pkg/app/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package app
import (
"context"
"fmt"
"log/slog"
"net/http"
"os"
"time"

"github.com/gin-gonic/gin"

Expand All @@ -23,21 +22,25 @@ func (a *App) initServer() error {

a.handlers = handlers.NewHandlers(a.logger, a.repository)

//gin.SetMode(gin.ReleaseMode)
gin.SetMode(a.cfg.HTTP.GinMode)
router := gin.Default()
router.POST("/api/v1/bookings/:workshop_id", a.handlers.CreateBooking)
router.GET("/api/v1/bookings/:workshop_id", a.handlers.ListBookings)

a.router = router

server := &http.Server{
Addr: fmt.Sprintf(":%s", os.Getenv("HTTP_PORT")),
Handler: router,
Addr: fmt.Sprintf(":%s", a.cfg.HTTP.Server.Port),
Handler: http.TimeoutHandler(router, a.cfg.HTTP.Server.HandlerTimeout, "Timeout"),
ErrorLog: slog.NewLogLogger(a.logger.Handler(), slog.LevelError),
ReadHeaderTimeout: a.cfg.HTTP.Server.ReadHeaderTimeout,
ReadTimeout: a.cfg.HTTP.Server.ReadTimeout,
IdleTimeout: a.cfg.HTTP.Server.IdleTimeout,
}

a.http = server
a.closers = append(a.closers, func() error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), a.cfg.HTTP.Server.ShutdownTimeout)
defer cancel()

return a.http.Shutdown(ctx)
Expand Down
83 changes: 83 additions & 0 deletions internal/pkg/app/http_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package app

import (
"fmt"
"os"
"time"

"github.com/gin-gonic/gin"
)

type configHTTP struct {
Server struct {
Port string
HandlerTimeout time.Duration
ReadHeaderTimeout time.Duration
ReadTimeout time.Duration
IdleTimeout time.Duration
ShutdownTimeout time.Duration
}
GinMode string // "debug" | "release" | "test"
}

func newHTTPConfig() *configHTTP {
c := &configHTTP{}

// Set default
c.Server.Port = "8080"
c.Server.HandlerTimeout = 6 * time.Second
c.Server.ReadHeaderTimeout = 3 * time.Second
c.Server.ReadTimeout = 3 * time.Second
c.Server.IdleTimeout = 3 * time.Second
c.Server.ShutdownTimeout = 10 * time.Second

c.Load()
c.Validate()
return c
}

func (c *configHTTP) Load() {
if httpPort := os.Getenv("HTTP_PORT"); httpPort != "" {
c.Server.Port = httpPort
}
if httpTimeout := os.Getenv("HTTP_TIMEOUT"); httpTimeout != "" {
handlerTimeoutDuration, err := time.ParseDuration(httpTimeout)
if err != nil {
panic(fmt.Errorf("incorrect env HTTP_TIMEOUT: %w", err))
}
c.Server.HandlerTimeout = handlerTimeoutDuration
}
if httpReadHeaderTimeout := os.Getenv("HTTP_READ_HEADER_TIMEOUT"); httpReadHeaderTimeout != "" {
readHeaderTimeoutDuration, err := time.ParseDuration(httpReadHeaderTimeout)
if err != nil {
panic(fmt.Errorf("incorrect env HTTP_READ_HEADER_TIMEOUT: %w", err))
}
c.Server.ReadHeaderTimeout = readHeaderTimeoutDuration
}
if httpReadTimeout := os.Getenv("HTTP_READ_TIMEOUT"); httpReadTimeout != "" {
readTimeoutDuration, err := time.ParseDuration(httpReadTimeout)
if err != nil {
panic(fmt.Errorf("incorrect env HTTP_READ_TIMEOUT: %w", err))
}
c.Server.ReadTimeout = readTimeoutDuration
}
if httpIdleTimeout := os.Getenv("HTTP_IDLE_TIMEOUT"); httpIdleTimeout != "" {
idleTimeoutDuration, err := time.ParseDuration(httpIdleTimeout)
if err != nil {
panic(fmt.Errorf("incorrect env HTTP_IDLE_TIMEOUT: %w", err))
}
c.Server.IdleTimeout = idleTimeoutDuration
}
if httpShutdownTimeout := os.Getenv("HTTP_SHUTDOWN_TIMEOUT"); httpShutdownTimeout != "" {
shutdownTimeoutDuration, err := time.ParseDuration(httpShutdownTimeout)
if err != nil {
panic(fmt.Errorf("incorrect env HTTP_SHUTDOWN_TIMEOUT: %w", err))
}
c.Server.ShutdownTimeout = shutdownTimeoutDuration
}
c.GinMode = os.Getenv(gin.EnvGinMode)
}

func (c *configHTTP) Validate() {

}