-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (68 loc) · 1.96 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
76
77
78
79
80
81
package main
import (
"database/sql"
"fmt"
"log/slog"
"pnb/api"
"pnb/service"
"pnb/service/store"
"pnb/worker"
_ "github.com/jackc/pgx/v5/stdlib"
_ "github.com/joho/godotenv/autoload"
"github.com/kelseyhightower/envconfig"
"github.com/pressly/goose/v3"
)
func main() {
slog.Info("Starting up...")
var cfg Config
err := envconfig.Process("pnb", &cfg)
if err != nil {
slog.Error("Failed to process config", err)
panic(err)
}
slog.Info("Connecting to database...")
db, err := sql.Open("pgx", cfg.ConnString())
if err != nil {
slog.Error("Failed to connect to database", err)
panic(err)
}
defer db.Close()
slog.Info("Pinging database...")
err = db.Ping()
if err != nil {
slog.Error("Failed to ping database", err)
}
slog.Info("Running migrations...")
err = goose.Up(db, cfg.MigrationPath())
if err != nil {
slog.Error("Failed to run migrations", err)
panic(err)
}
querier := store.New(db)
service := service.NewService(querier)
slog.Info("Creating worker...")
err = worker.Init(service)
if err != nil {
slog.Error("Failed to create worker", err)
panic(err)
}
slog.Info("Init API")
api.Init(service, cfg.Port)
}
type Config struct {
Port string `envconfig:"port" required:"true"`
OpenAIKey string `envconfig:"openai_api_key" required:"true"`
NewsAPIBaseURL string `envconfig:"news_api_base_url" required:"true"`
NewsAPIKey string `envconfig:"news_api_key" required:"true"`
DBAddress string `envconfig:"db_address" required:"true"`
DBName string `envconfig:"db_name" required:"true"`
DBUser string `envconfig:"db_user" required:"true"`
DBPassword string `envconfig:"db_password" required:"true"`
DBMigrationPath string `envconfig:"db_migration_path" required:"true"`
}
func (c Config) ConnString() string {
return fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable", c.DBUser, c.DBPassword, c.DBAddress, c.DBName)
}
func (c Config) MigrationPath() string {
return c.DBMigrationPath
}