forked from voxpupuli/openvoxview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
115 lines (93 loc) · 2.55 KB
/
main.go
File metadata and controls
115 lines (93 loc) · 2.55 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
package main
import (
"fmt"
"io/fs"
"log"
"net/http"
"strings"
"github.com/gin-gonic/gin"
"github.com/sebastianrakel/openvoxview/config"
"github.com/sebastianrakel/openvoxview/handler"
)
var (
VERSION = "0.1.0"
COMMIT = "dirty"
)
func main() {
if config.PrintVersion(VERSION) {
return
}
log.Printf("OpenVox View - %s (%s)", VERSION, COMMIT)
cfg, err := config.GetConfig()
if err != nil {
panic(err)
}
log.Printf("LISTEN: %s", cfg.Listen)
log.Printf("PORT: %d", cfg.Port)
log.Printf("PUPPETDB_ADDRESS: %s", cfg.GetPuppetDbAddress())
log.Printf("TRUSTED_PROXIES: %#v", cfg.TrustedProxies)
r := gin.Default()
r.NoRoute(func(c *gin.Context) {
if strings.HasPrefix(c.Request.URL.Path, "/api/") {
c.Next()
return
}
c.Redirect(http.StatusTemporaryRedirect, "/ui/?#/")
})
uiFSSub, _ := fs.Sub(uiFS, "ui/dist/spa")
r.StaticFS("ui", http.FS(uiFSSub))
r.Use(AllowCORS)
if len(cfg.TrustedProxies) > 0 {
r.SetTrustedProxies(cfg.TrustedProxies)
}
pdbHandler := handler.NewPdbHandler(cfg)
viewHandler := handler.NewViewHandler(cfg)
api := r.Group("/api/v1/")
{
api.GET("meta", func(c *gin.Context) {
type metaResponse struct {
UnreportedHours uint64
}
response := metaResponse{
UnreportedHours: cfg.UnreportedHours,
}
c.JSON(http.StatusOK, handler.NewSuccessResponse(response))
})
api.GET("version", func(c *gin.Context) {
type versionResponse struct {
Version string
}
response := versionResponse{
Version: VERSION,
}
c.JSON(http.StatusOK, handler.NewSuccessResponse(response))
})
view := api.Group("view")
{
view.GET("node_overview", viewHandler.NodesOverview)
view.GET("metrics", viewHandler.Metrics)
view.GET("predefined", viewHandler.PredefinedViews)
view.GET("predefined/:viewName", viewHandler.PredefinedViewsResult)
view.GET("predefined/:viewName/meta", viewHandler.PredefinedViewsMeta)
}
pdb := api.Group("pdb")
{
pdb.POST("query", pdbHandler.PdbExecuteQuery)
pdb.GET("query/history", pdbHandler.PdbQueryHistory)
pdb.GET("query/predefined", pdbHandler.PdbQueryPredefined)
pdb.GET("fact-names", pdbHandler.PdbGetFactNames)
pdb.POST("event-counts", pdbHandler.PdbGetEventCounts)
}
}
r.Run(fmt.Sprintf("%s:%d", cfg.Listen, cfg.Port))
}
func AllowCORS(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE")
c.Header("Access-Control-Allow-Headers", "Authorization, *")
if c.Request.Method == http.MethodOptions {
c.Status(http.StatusNoContent)
return
}
c.Next()
}