/
main.go
101 lines (89 loc) · 2.42 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/allisson/go-env"
"github.com/allisson/postmand/repository"
"github.com/allisson/postmand/service"
"github.com/jmoiron/sqlx"
_ "github.com/joho/godotenv/autoload"
"github.com/urfave/cli/v2"
"go.uber.org/zap"
)
func main() {
// Setup logger
logger, err := zap.NewProduction()
if err != nil {
log.Fatalf("logger-setup-error: %v\n", err)
}
// nolint:errcheck
defer logger.Sync()
// Setup postgresql database
db, err := sqlx.Open("postgres", env.GetString("POSTMAND_DATABASE_URL", ""))
if err != nil {
logger.Fatal("database-setup-error", zap.Error(err))
}
if err := db.Ping(); err != nil {
logger.Fatal("database-ping-error", zap.Error(err))
}
db.SetMaxOpenConns(env.GetInt("POSTMAND_DATABASE_MAX_OPEN_CONNS", 2))
// Setup cli
app := cli.NewApp()
app.Name = "postmand"
app.Usage = "CLI"
app.Authors = []*cli.Author{
{
Name: "Allisson Azevedo",
Email: "allisson@gmail.com",
},
}
app.Commands = []*cli.Command{
{
Name: "migrate",
Aliases: []string{"m"},
Usage: "executes database migration",
Action: func(c *cli.Context) error {
migrationRepository := repository.NewMigration(
db,
env.GetString("POSTMAND_DATABASE_MIGRATION_DIR", "file:///db/migrations"),
)
migrationService := service.NewMigration(migrationRepository)
return migrationService.Run(c.Context)
},
},
{
Name: "worker",
Aliases: []string{"w"},
Usage: "executes worker to dispatch webhooks",
Action: func(c *cli.Context) error {
deliveryRepository := repository.NewDelivery(db)
pollingInterval := time.Duration(env.GetInt("POSTMAND_POLLING_INTERVAL", 1000)) * time.Millisecond
workerService := service.NewWorker(deliveryRepository, logger, pollingInterval)
// Graceful shutdown
idleConnsClosed := make(chan struct{})
go func() {
sigint := make(chan os.Signal, 1)
// interrupt signal sent from terminal
signal.Notify(sigint, os.Interrupt)
// sigterm signal sent from kubernetes
signal.Notify(sigint, syscall.SIGTERM)
<-sigint
// We received an interrupt signal, shut down.
workerService.Shutdown(c.Context)
close(idleConnsClosed)
}()
logger.Info("worker-started")
workerService.Run(c.Context)
<-idleConnsClosed
return nil
},
},
}
err = app.Run(os.Args)
if err != nil {
logger.Fatal("cli-error", zap.Error(err))
}
}