-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
112 lines (82 loc) · 2.68 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
package main
import (
"fmt"
"github.com/arkits/onhub-web/db"
"github.com/arkits/onhub-web/domain"
"github.com/arkits/onhub-web/handlers"
"github.com/gin-contrib/cors"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
"github.com/gobuffalo/packr/v2"
"github.com/op/go-logging"
"github.com/spf13/viper"
)
var (
version string
logger = logging.MustGetLogger("main")
)
func init() {
// Setup the Application wide config through Viper
SetupConfig()
// Setup Logger
domain.SetupLogger()
// Setup the DB
err := db.InitDatabase()
if err != nil {
logger.Fatalf("Failed InitDatabase - %v", err)
}
// Set Gin's Release Mode
SetGinReleaseMode()
}
func main() {
port := viper.GetString("server.port")
serviceName := viper.GetString("server.name")
r := gin.New()
// Recovery middleware recovers from any panics and writes a 500 if there was one.
r.Use(gin.Recovery())
// Add MetricsMiddleware
r.Use(domain.MetricsMiddleware())
// Allow all origins - CORS
r.Use(cors.Default())
// Expose the Frontend
box := packr.New("web-assets", "./web/build")
r.Use(domain.StaticServe("/", box))
r.Use(domain.StaticServe(fmt.Sprintf("/%s", serviceName), box))
// Use Gzip compression
r.Use(gzip.Gzip(gzip.DefaultCompression))
// Expose Version Endpoint
r.GET(fmt.Sprintf("/%s/api", serviceName), handlers.VersionHandler)
// Devices Endpoints
r.GET(fmt.Sprintf("/%s/api/devices", serviceName), handlers.GetAllDevicesHandler)
// Network Metrics Endpoints
r.GET(fmt.Sprintf("/%s/api/network-metrics", serviceName), handlers.GetNetworkMetricsHandler)
r.GET(fmt.Sprintf("/%s/api/network-metrics/status", serviceName), handlers.GetNetworkMetricsStatusHandler)
r.POST(fmt.Sprintf("/%s/api/network-metrics/start-polling", serviceName), handlers.KickOffNetworkMetricsPolling)
// Expose Metrics Endpoint
r.GET(fmt.Sprintf("/%s/api/metrics", serviceName), handlers.MetricsHandler)
// Run the Web Server
logger.Infof("Running on http://localhost:%v/%v", port, serviceName)
r.Run(":" + port)
}
// SetupConfig - Setup the application config by reading the config file via Viper
func SetupConfig() {
viper.SetConfigName("config")
viper.AddConfigPath(".")
viper.AddConfigPath("/opt/software/onhub-web")
if err := viper.ReadInConfig(); err != nil {
logger.Fatalf("Error reading config file! - %s", err)
}
// If the version is not set, then initialize it to 0.0.1
if version == "" {
version = "0.0.1"
}
viper.Set("server.version", version)
}
// SetGinReleaseMode set Gin's release mode based on the Config
func SetGinReleaseMode() {
releaseMode := viper.GetBool("server.release_mode")
if releaseMode {
logger.Debugf("Running in ReleaseMode")
gin.SetMode(gin.ReleaseMode)
}
}