-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
55 lines (47 loc) · 1.68 KB
/
Copy pathmain.go
File metadata and controls
55 lines (47 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"context"
"fmt"
"log"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/kelseyhightower/envconfig"
"github.com/the-code-genin/golang_integration_testing/http"
"github.com/the-code-genin/golang_integration_testing/repository"
"github.com/the-code-genin/golang_integration_testing/service"
)
// Config holds environment variables
type Config struct {
PostgresUser string `envconfig:"POSTGRES_USER" default:"postgres"`
PostgresPassword string `envconfig:"POSTGRES_PASSWORD" default:"password"`
PostgresHost string `envconfig:"POSTGRES_HOST" default:"localhost"`
PostgresPort string `envconfig:"POSTGRES_PORT" default:"5432"`
PostgresDB string `envconfig:"POSTGRES_DB" default:"postgres"`
ServerPort int `envconfig:"SERVER_PORT" default:"8080"`
}
func main() {
var cfg Config
if err := envconfig.Process("", &cfg); err != nil {
log.Fatalf("failed to load environment variables: %v", err)
}
// Build Postgres connection string
connStr := fmt.Sprintf(
"postgres://%s:%s@%s:%s/%s?sslmode=disable",
cfg.PostgresUser, cfg.PostgresPassword, cfg.PostgresHost, cfg.PostgresPort, cfg.PostgresDB,
)
// Connect to Postgres using pgxpool
connPool, err := pgxpool.New(context.Background(), connStr)
if err != nil {
log.Fatalf("failed to connect to postgres: %v", err)
}
defer connPool.Close()
// Initialize repository and service
repo := repository.NewRepository(connPool)
svc := service.NewService(repo)
// Start the HTTP server
server := http.NewServer(svc)
addr := fmt.Sprintf(":%d", cfg.ServerPort)
log.Printf("starting server on %s...", addr)
if err := server.Start(addr); err != nil {
log.Fatalf("server stopped with error: %v", err)
}
}