-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathmain.go
More file actions
215 lines (191 loc) · 5.07 KB
/
main.go
File metadata and controls
215 lines (191 loc) · 5.07 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Package main is the entry point of the example application.
package main
import (
"fmt"
"net/http"
"time"
gconfig "github.com/pilinux/gorest/config"
gdatabase "github.com/pilinux/gorest/database"
gserver "github.com/pilinux/gorest/lib/server"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
"github.com/pilinux/gorest/example/database/migrate"
"github.com/pilinux/gorest/example/router"
)
func main() {
// set configs
err := gconfig.Config()
if err != nil {
fmt.Println(err)
return
}
// read configs
configure := gconfig.GetConfig()
if gconfig.IsRDBMS() {
// Initialize RDBMS client
if err := gdatabase.InitDB().Error; err != nil {
fmt.Println(err)
return
}
// Drop all tables from DB
/*
if err := migrate.DropAllTables(); err != nil {
fmt.Println(err)
return
}
*/
// Start DB migration
if err := migrate.StartMigration(*configure); err != nil {
fmt.Println(err)
return
}
// Manually set foreign key for MySQL and PostgreSQL
if err := migrate.SetPkFk(); err != nil {
fmt.Println(err)
return
}
}
if gconfig.IsRedis() {
// Initialize REDIS client
if _, err := gdatabase.InitRedis(); err != nil {
fmt.Println(err)
return
}
}
if gconfig.IsMongo() {
// Initialize MONGO client
if _, err := gdatabase.InitMongo(); err != nil {
fmt.Println(err)
return
}
// Example of dropping index "countryCode" from collection "geocodes" in database "map"
/*
indexes := []string{"countryCode"}
if err := gdatabase.MongoDropIndex("map", "geocodes", indexes); err != nil {
fmt.Println(err)
return
}
*/
// Example of dropping all indexes from collection "geocodes" in database "map"
/*
if err := gdatabase.MongoDropAllIndexes("map", "geocodes"); err != nil {
fmt.Println(err)
return
}
*/
// Create new index for "countryCode" field
index := mongo.IndexModel{Keys: bson.D{{Key: "countryCode", Value: 1}}}
if err := gdatabase.MongoCreateIndex("map", "geocodes", index); err != nil {
fmt.Println(err)
return
}
// Example of creating many indexes
/*
indexes := []mongo.IndexModel{
{
Keys: bson.D{{Key: "state", Value: 1}},
},
{
Keys: bson.D{{Key: "countryCode", Value: 1}},
},
}
if err := gdatabase.MongoCreateIndexes("map", "geocodes", indexes); err != nil {
fmt.Println(err)
return
}
*/
}
// example of using sentry in separate goroutines
/*
var GoroutineLogger *log.Logger
sentryHook, err := middleware.NewSentryHook(
configure.Logger.SentryDsn,
configure.Server.ServerEnv,
configure.Version,
configure.Logger.PerformanceTracing,
configure.Logger.TracesSampleRate,
)
if err != nil {
fmt.Println(err)
}
if err == nil {
if sentryHook != nil {
defer func() {
sentryHook.Flush(5 * time.Second)
}()
GoroutineLogger = log.New()
GoroutineLogger.AddHook(sentryHook)
// this log level is independent of the global log level
GoroutineLogger.SetLevel(log.DebugLevel)
GoroutineLogger.SetFormatter(&log.JSONFormatter{})
GoroutineLogger.
WithFields(log.Fields{
"time": time.Now().Format(time.RFC3339),
"ref": "main",
}).
Debug("testing sentry integration in the main function")
}
}
if GoroutineLogger == nil {
fmt.Println("failed to create a logger for separate goroutines")
}
if GoroutineLogger != nil {
if configure.Logger.SentryDsn != "" {
// Example goroutine to keep logging periodically
go func() {
i := 0
for {
i++
ref := fmt.Sprintf("goroutine - %d", i)
fmt.Println("ref:", ref)
fmt.Println("testing sentry integration in a separate goroutine")
GoroutineLogger.
WithFields(log.Fields{
"time": time.Now().Format(time.RFC3339),
"ref": ref,
}).
Info("testing sentry integration in a separate goroutine")
time.Sleep(5 * time.Second)
}
}()
}
}
*/
r, err := router.SetupRouter(configure)
if err != nil {
fmt.Println(err)
return
}
// Attaches the router to a http.Server
srv := &http.Server{
Addr: configure.Server.ServerHost + ":" + configure.Server.ServerPort,
Handler: r,
// Add timeout to prevent Slowloris attacks
ReadTimeout: 30 * time.Second, // max time to read the entire request including body
ReadHeaderTimeout: 5 * time.Second, // max time to read the request header
WriteTimeout: 5 * time.Second, // max time to generate and send the response
IdleTimeout: 60 * time.Second, // important for keep-alive connections
}
// Start shutdown watcher
const shutdownTimeout = 30 * time.Second
done := make(chan struct{})
// Wait for interrupt signal to gracefully shutdown the server
go func() {
err := gserver.GracefulShutdown(
srv,
shutdownTimeout,
done,
gdatabase.CloseAllDB,
)
if err != nil {
fmt.Println(err)
}
}()
// Start the server
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
fmt.Printf("server error: %v\n", err)
}
// Wait for the graceful shutdown to complete
<-done
fmt.Println("server shutdown complete")
}