-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
142 lines (118 loc) · 4.23 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
138
139
140
141
142
package main
import (
"context"
"errors"
"github.com/myrteametrics/myrtea-engine-api/v5/internals/export"
"github.com/myrteametrics/myrtea-engine-api/v5/internals/handlers"
"github.com/myrteametrics/myrtea-engine-api/v5/internals/metrics"
"github.com/myrteametrics/myrtea-engine-api/v5/internals/service"
"net/http"
"os"
"os/signal"
"syscall"
"time"
_ "github.com/lib/pq"
"github.com/myrteametrics/myrtea-engine-api/v5/internals/app"
"github.com/myrteametrics/myrtea-engine-api/v5/internals/router"
plugin "github.com/myrteametrics/myrtea-engine-api/v5/plugins"
"github.com/myrteametrics/myrtea-sdk/v5/helpers"
"github.com/myrteametrics/myrtea-sdk/v5/server"
"github.com/spf13/viper"
"go.uber.org/zap"
)
var (
// Version is the binary version (tag) + build number (CI pipeline)
Version string
// BuildDate is the date of build
BuildDate string
)
// @version 1.0
// @description Myrtea Engine-API Swagger
// @termsOfService http://swagger.io/terms/
// @contact.name Myrtea Metrics
// @contact.url https://myrteametrics.ai/en/
// @contact.email [email protected]
// @securityDefinitions.apikey Bearer
// @in header
// @name Authorization
func main() {
hostname, _ := os.Hostname()
metrics.InitMetricLabels(hostname)
app.InitConfiguration()
zapConfig := helpers.InitLogger(viper.GetBool("LOGGER_PRODUCTION"))
app.Init()
defer app.Stop()
zap.L().Info("Starting Engine-API", zap.String("version", Version), zap.String("build_date", BuildDate))
// Starting plugin core
core := &plugin.Core{}
core.RegisterPlugins()
core.Start()
defer core.Stop()
serverPort := viper.GetInt("HTTP_SERVER_PORT")
serverEnableTLS := viper.GetBool("HTTP_SERVER_ENABLE_TLS")
serverTLSCert := viper.GetString("HTTP_SERVER_TLS_FILE_CRT")
serverTLSKey := viper.GetString("HTTP_SERVER_TLS_FILE_KEY")
routerConfig := router.Config{
Production: viper.GetBool("LOGGER_PRODUCTION"),
CORS: viper.GetBool("HTTP_SERVER_API_ENABLE_CORS"),
Security: viper.GetBool("HTTP_SERVER_API_ENABLE_SECURITY"),
GatewayMode: viper.GetBool("HTTP_SERVER_API_ENABLE_GATEWAY_MODE"),
AuthenticationMode: viper.GetString("AUTHENTICATION_MODE"),
LogLevel: zapConfig.Level,
}
// Exports
directDownload := viper.GetBool("EXPORT_DIRECT_DOWNLOAD")
indirectDownloadUrl := viper.GetString("EXPORT_INDIRECT_DOWNLOAD_URL")
exportWrapper := export.NewWrapper(
viper.GetString("EXPORT_BASE_PATH"), // basePath
viper.GetInt("EXPORT_WORKERS_COUNT"), // workersCount
viper.GetInt("EXPORT_DISK_RETENTION_DAYS"), // diskRetentionDays
viper.GetInt("EXPORT_QUEUE_MAX_SIZE"), // queueMaxSize
)
exportWrapper.Init(context.Background())
// Init services
serviceManager := service.NewManager()
if err := serviceManager.LoadConnectors(); err != nil {
zap.L().Error("Error loading service connectors", zap.Error(err))
}
if err := serviceManager.LoadPlugins(core); err != nil {
zap.L().Error("Error loading service plugins", zap.Error(err))
}
// Init router services struct (used to inject services into the router)
routerServices := router.Services{
PluginCore: core,
ProcessorHandler: handlers.NewProcessorHandler(),
ExportHandler: handlers.NewExportHandler(exportWrapper, directDownload, indirectDownloadUrl),
ServiceHandler: handlers.NewServiceHandler(serviceManager),
}
mux := router.New(routerConfig, routerServices)
var srv *http.Server
if serverEnableTLS {
srv = server.NewSecuredServer(serverPort, serverTLSCert, serverTLSKey, mux)
} else {
srv = server.NewUnsecuredServer(serverPort, mux)
}
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
var err error
if serverEnableTLS {
err = srv.ListenAndServeTLS(serverTLSCert, serverTLSKey)
} else {
err = srv.ListenAndServe()
}
if err != nil && !errors.Is(err, http.ErrServerClosed) {
zap.L().Fatal("Server listen", zap.Error(err))
}
}()
zap.L().Info("Server Started", zap.String("addr", srv.Addr))
<-done
ctxShutDown, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer func() {
cancel()
}()
if err := srv.Shutdown(ctxShutDown); err != nil {
zap.L().Fatal("Server shutdown failed", zap.Error(err))
}
zap.L().Info("Server shutdown")
}