/
main.go
75 lines (63 loc) · 1.54 KB
/
main.go
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"context"
"fmt"
"log"
"github.com/alexdunne/gs-onboarding/internal/api"
"github.com/alexdunne/gs-onboarding/internal/database"
"github.com/pkg/errors"
"github.com/spf13/viper"
"go.uber.org/zap"
)
type Config struct {
Port int
DatabaseDSN string
RedisURL string
}
func loadConfig() (*Config, error) {
viper.SetConfigFile(".env")
if err := viper.ReadInConfig(); err != nil {
return nil, errors.Wrap(err, "failed to read env file")
}
return &Config{
Port: viper.GetInt("API_PORT"),
DatabaseDSN: fmt.Sprintf(
"postgres://%s:%s@%s:%s/%s",
viper.GetString("DATABASE_USER"),
viper.GetString("DATABASE_PASSWORD"),
viper.GetString("DATABASE_HOST"),
viper.GetString("DATABASE_PORT"),
viper.GetString("DATABASE_DB"),
),
RedisURL: viper.GetString("REDIS_URL"),
}, nil
}
func main() {
cfg, err := loadConfig()
if err != nil {
log.Fatal(errors.Wrap(err, "loading config"))
}
logger, err := zap.NewProduction()
if err != nil {
log.Fatal(errors.Wrap(err, "creating logger"))
}
defer logger.Sync()
ctx := context.Background()
db, err := database.New(ctx, cfg.DatabaseDSN)
if err != nil {
log.Fatal(errors.Wrap(err, "opening database connection"))
}
defer db.Close()
cache, err := api.NewCache(ctx, cfg.RedisURL, db, logger)
if err != nil {
log.Fatal(errors.Wrap(err, "opening cache connection"))
}
defer cache.Close()
h := api.Handler{
Cache: cache,
}
s := api.NewServer(cfg.Port, logger, h)
if err := s.Start(); err != nil {
logger.Fatal("running server", zap.Error(err))
}
}