-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
137 lines (114 loc) · 3.38 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"context"
"database/sql"
"embed"
"fmt"
"net/http"
"os/signal"
"syscall"
"time"
_sentenceHandlerHttpDelivery "elegant-tw-api/sentence/delivery/http"
_sentenceRepo "elegant-tw-api/sentence/repository/postgresql"
_sentenceUsecase "elegant-tw-api/sentence/usecase"
"elegant-tw-api/utils"
"github.com/gin-gonic/gin"
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/golang-migrate/migrate/v4/source/iofs"
"github.com/sirupsen/logrus"
_ "github.com/lib/pq"
)
//go:embed db/migrations/*.sql
var fs embed.FS
func main() {
logrus.Info("Reading environment variables...")
cfg, err := utils.Read()
if err != nil {
logrus.Fatal(err)
}
logrus.Info("Connecting to database...")
db, err := sql.Open(
"postgres",
fmt.Sprintf(
"host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
cfg.DBHost, cfg.DBPort, cfg.DBUsername, cfg.DBPassword, cfg.DBName,
),
)
if err != nil {
logrus.Fatal(err)
}
if err = db.Ping(); err != nil {
logrus.Fatal(err)
}
logrus.Info("Connected to database.")
logrus.Info("Preparing migrate database files.")
d, err := iofs.New(fs, "db/migrations")
if err != nil {
logrus.Fatal(err)
}
logrus.Info("Migrate database files are ready.")
logrus.Info("Starting migration.")
m, err := migrate.NewWithSourceInstance("iofs", d,
fmt.Sprintf(
"postgres://%s:%s@%s:%s/%s?sslmode=disable",
cfg.DBUsername, cfg.DBPassword, cfg.DBHost, cfg.DBPort, cfg.DBName,
),
)
if err != nil {
logrus.Fatal(err)
}
if err := m.Up(); err != nil {
if err == migrate.ErrNoChange {
logrus.Info("Nothing to migrate.")
} else {
logrus.Fatal(err)
}
} else {
logrus.Info("Migration completed.")
}
gin.SetMode(cfg.GinMode)
router := gin.Default()
utils.BuildCORSConfig(router, cfg)
if cfg.RateLimitEnabled {
logrus.Info("Rate limit is enable.")
utils.RateLimitInit(router, cfg)
} else {
logrus.Info("Rate limit is disabled.")
}
sentenceRepo := _sentenceRepo.NewpostgresqlSentenceRepoistory(db)
sentenceUsecase := _sentenceUsecase.NewSentenceUsecase(sentenceRepo)
_sentenceHandlerHttpDelivery.NewSentenceHandler(router, sentenceUsecase)
logrus.Info("HTTP server started.")
srvStart(router, *cfg)
}
func srvStart(router *gin.Engine, cfg utils.Config) {
// Create context that listens for the interrupt signal from the OS.
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
srv := &http.Server{
Addr: cfg.ServerAddr + ":" + cfg.ServerPort,
Handler: router,
}
// Initializing the server in a goroutine so that
// it won't block the graceful shutdown handling below
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logrus.Fatalf("listen: %s\n", err)
}
}()
// Listen for the interrupt signal.
<-ctx.Done()
// Restore default behavior on the interrupt signal and notify user of shutdown.
stop()
logrus.Println("shutting down gracefully, press Ctrl+C again to force")
// The context is used to inform the server it has 5 seconds to finish
// the request it is currently handling
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
logrus.Fatal("Server forced to shutdown: ", err)
}
logrus.Println("Server exiting")
}