Introduce database and first endpoint with service/repository #1

Merged
robin merged 6 commits from dev into main 2026-07-10 14:14:05 +02:00
3 changed files with 51 additions and 0 deletions
Showing only changes of commit 53ab1aee4f - Show all commits
+17
View File
@@ -15,6 +15,7 @@ import (
"github.com/joho/godotenv"
"github.com/robindittmar/dttmr-api/internal/api/router"
"github.com/robindittmar/dttmr-api/internal/database"
"github.com/robindittmar/dttmr-api/internal/telemetry"
)
@@ -22,6 +23,7 @@ type Config struct {
Environment string
Port int
OTLPEndpoint string
DatabaseURL string
}
func main() {
@@ -62,6 +64,18 @@ func run(serviceName string, serviceVersion string) error {
}
}()
db, err := database.New(context.Background(), cfg.DatabaseURL)
if err != nil {
slog.Error("Failed to initialize database", slog.Any("error", err))
return err
}
defer func() {
err := db.Close()
if err != nil {
slog.Error("Failed to close database connection", slog.Any("error", err))
}
}()
srv := makeServer(cfg)
go func() {
slog.Info("Starting http server", "addr", srv.Addr)
@@ -103,6 +117,7 @@ func loadConfig() *Config {
envFlag := flag.String("env", "development", "environment to use")
portFlag := flag.Int("port", 8080, "port to listen on")
otlpEndpointFlag := flag.String("otlp-endpoint", "localhost:4317", "otlp endpoint")
databaseUrlFlag := flag.String("database-url", "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable", "database connection string")
flag.Parse()
@@ -110,11 +125,13 @@ func loadConfig() *Config {
Environment: *envFlag,
Port: *portFlag,
OTLPEndpoint: *otlpEndpointFlag,
DatabaseURL: *databaseUrlFlag,
}
assignStringFromEnv("DTTMR_ENVIRONMENT", &cfg.Environment)
assignIntFromEnv("DTTMR_PORT", &cfg.Port)
assignStringFromEnv("DTTMR_OTLP_ENDPOINT", &cfg.OTLPEndpoint)
assignStringFromEnv("DTTMR_DATABASE_URL", &cfg.DatabaseURL)
return cfg
}
+5
View File
@@ -3,6 +3,7 @@ module github.com/robindittmar/dttmr-api
go 1.26
require (
github.com/jackc/pgx/v5 v5.10.0
github.com/joho/godotenv v1.5.1
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.69.0
go.opentelemetry.io/otel v1.44.0
@@ -21,11 +22,15 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.29.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.44.0 // indirect
go.opentelemetry.io/otel/metric v1.44.0 // indirect
go.opentelemetry.io/proto/otlp v1.10.0 // indirect
golang.org/x/net v0.56.0 // indirect
golang.org/x/sync v0.21.0 // indirect
golang.org/x/sys v0.46.0 // indirect
golang.org/x/text v0.39.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20260706201446-f0a921348800 // indirect
+29
View File
@@ -0,0 +1,29 @@
package database
import (
"context"
"database/sql"
"fmt"
"time"
_ "github.com/jackc/pgx/v5/stdlib"
)
func New(ctx context.Context, connURL string) (*sql.DB, error) {
db, err := sql.Open("pgx", connURL)
if err != nil {
return nil, fmt.Errorf("failed to connect to database: %w", err)
}
db.SetMaxOpenConns(10)
db.SetMaxIdleConns(10)
db.SetConnMaxLifetime(5 * time.Minute)
db.SetConnMaxIdleTime(5 * time.Minute)
if err := db.PingContext(ctx); err != nil {
return nil, fmt.Errorf("database unreachable: %w", err)
}
return db, nil
}