-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
209 lines (173 loc) · 5.27 KB
/
Copy pathmain.go
File metadata and controls
209 lines (173 loc) · 5.27 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
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/gin-contrib/cache/persistence"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v8"
"gorm.io/gorm"
docs "sentinel/docs"
swaggerfiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"github.com/opentracing/opentracing-go"
"github.com/uber/jaeger-client-go"
jaegercfg "github.com/uber/jaeger-client-go/config"
jaegerlog "github.com/uber/jaeger-client-go/log"
"github.com/uber/jaeger-lib/metrics"
"sentinel/config"
"sentinel/internal/handlers"
"sentinel/pkg/constants"
"sentinel/pkg/db/postgres"
rediscfg "sentinel/pkg/db/redis"
"sentinel/pkg/logger"
"sentinel/pkg/utils"
)
// @title Sentinel API
// @description: This is a simple API for checking the expiration date of the certificates of the given domain names.
// @version 1.0.0
// @schemes http https
// @contact.name Yunus Emre Alpu
// @contact.url https://yunusemrealpu.netlify.app
// @contact.email YunusAlpu@icloud.com
// @BasePath /sentinel
var isConfigSuccess = false
// var equals string = strings.Repeat("=", 50)
// APP_NAME = "localhost:8080/sentinel/"
const (
APP_NAME = "sentinel"
)
func main() {
var dbConn *gorm.DB
var cacheConn *redis.Client
var cacheContext context.Context
var inAppCache *persistence.InMemoryStore
mode := config.C.App.Mode
port := config.C.App.Port
router := gin.New()
router.Use(gin.RecoveryWithWriter(gin.DefaultErrorWriter))
// If cache is active, create cache connection
if config.C.Cache.Active {
inAppCache = rediscfg.NewInAppCacheStore(time.Duration(config.C.App.Expire) * time.Second)
cacheConn, cacheContext = rediscfg.NewRedisCacheConnection(config.C.Cache.Url)
}
// If db is active, create db connection
if config.C.DB.Active {
dbConn = postgres.NewPostgresDB(config.C.DB.Url)
utils.GlobalDataService = utils.NewDataService(dbConn)
logger.CLogger.Info("GlobalDataService initialized with database connection")
}
jaegerCfgInstance := jaegercfg.Configuration{
ServiceName: config.C.Jaeger.ServiceName,
Sampler: &jaegercfg.SamplerConfig{
Type: jaeger.SamplerTypeConst,
Param: 1,
},
Reporter: &jaegercfg.ReporterConfig{
LogSpans: config.C.Jaeger.LogSpans,
LocalAgentHostPort: config.C.Jaeger.Host,
},
}
tracer, closer, err := jaegerCfgInstance.NewTracer(
jaegercfg.Logger(jaegerlog.StdLogger),
jaegercfg.Metrics(metrics.NullFactory),
)
if err != nil {
log.Fatal("cannot create tracer", err)
}
// create application service
sentinelsvc := handlers.NewSentinelService(
inAppCache,
cacheConn,
cacheContext,
dbConn,
)
// check env and set gin mode
setApplicationMode(mode, router)
sentinelsvc.InitRouter(router)
opentracing.SetGlobalTracer(tracer)
defer closer.Close()
logger.CLogger.Info("Opentracing connected")
// Create HTTP server with timeouts
srv := &http.Server{
Addr: ":" + port,
Handler: router,
ReadTimeout: constants.ServerReadTimeout,
WriteTimeout: constants.ServerWriteTimeout,
IdleTimeout: constants.ServerIdleTimeout,
MaxHeaderBytes: constants.ServerMaxHeaderBytes,
}
// Run server in goroutine
go func() {
logger.CLogger.Info("INIT: Application " + APP_NAME + " started on port " + port)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.CLogger.Fatal("INIT: Server failed to start: ", err)
}
}()
// Graceful shutdown
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
logger.CLogger.Info("SHUTDOWN: Shutting down server...")
// Create shutdown context with timeout
ctx, cancel := context.WithTimeout(context.Background(), constants.ShutdownTimeout)
defer cancel()
// Shutdown server gracefully
if err := srv.Shutdown(ctx); err != nil {
logger.CLogger.Fatal("SHUTDOWN: Server forced to shutdown: ", err)
}
// Close database connection if active
if config.C.DB.Active && dbConn != nil {
sqlDB, err := dbConn.DB()
if err == nil {
sqlDB.Close()
}
}
// Close Redis connection if active
if config.C.Cache.Active && cacheConn != nil {
cacheConn.Close()
}
logger.CLogger.Info("SHUTDOWN: Server exited gracefully")
}
// Initialize Application
func init() {
isConfigSuccess = configureApplication()
if !isConfigSuccess {
logger.CLogger.Error("INIT: Application configuration failed.")
os.Exit(1)
} else {
logger.CLogger.Info("INIT: Application configuration success.")
}
}
// Configure Application with config file
func configureApplication() bool {
dir, err := os.Getwd()
if err != nil {
logger.CLogger.Error("INIT: Cannot get current working directory os.Getwd()")
return false
} else {
config.ReadConfig(dir)
return true
}
}
// Set Application Mode
func setApplicationMode(md string, router *gin.Engine) {
gin.SetMode(gin.ReleaseMode)
if md == "prod" || md == "production" {
gin.SetMode(gin.ReleaseMode)
} else {
router.Use(gin.Logger())
// logger time gonna be in UTC+3 (Asia/Istanbul)
gin.SetMode(gin.DebugMode)
}
// check env and set swagger
if !(md == "prod" || md == "production") {
docs.SwaggerInfo.BasePath = handlers.API_PREFIX
// Endpoint for swagger: http://localhost:{targetPort}/sentinel/swagger/index.html
router.GET(handlers.API_PREFIX+"/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
}
}