forked from falcosecurity/falcosidekick-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
140 lines (115 loc) · 3.98 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
package main
import (
"flag"
"fmt"
"net"
"net/http"
"os"
"github.com/falcosecurity/falcosidekick-ui/configuration"
"github.com/falcosecurity/falcosidekick-ui/internal/api"
"github.com/falcosecurity/falcosidekick-ui/internal/broadcast"
"github.com/falcosecurity/falcosidekick-ui/internal/database/redis"
"github.com/falcosecurity/falcosidekick-ui/internal/models"
"github.com/falcosecurity/falcosidekick-ui/internal/utils"
validator "github.com/go-playground/validator/v10"
echo "github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
echoSwagger "github.com/swaggo/echo-swagger"
_ "github.com/falcosecurity/falcosidekick-ui/docs"
)
// const (
// light string = "light"
// dark string = "dark"
// )
// go:embed dist
// var frontend embed.FS
type CustomValidator struct {
validator *validator.Validate
}
func init() {
addr := flag.String("a", "0.0.0.0", "Listen Address")
port := flag.Int("p", 2802, "Listen Port")
// darkmod := flag.Bool("d", false, "Enable dark mode as default")
version := flag.Bool("v", false, "Print version")
dev := flag.Bool("x", false, "Allow CORS for development")
redisserver := flag.String("r", "localhost:6379", "Redis server address")
flag.Parse()
if *version {
v := configuration.GetVersionInfo()
fmt.Println(v.String())
os.Exit(0)
}
configuration.CreateConfiguration()
config := configuration.GetConfiguration()
if ip := net.ParseIP(*addr); ip == nil {
utils.WriteLog("error", "Failed to parse Listen Address", true)
}
config.ListenAddress = *addr
config.ListenPort = *port
// config.DisplayMode = light
// if *darkmod {
// config.DisplayMode = dark
// }
config.RedisServer = *redisserver
config.DevMode = *dev
redis.CreateClient()
redis.CreateIndex(redis.GetClient())
models.CreateOutputs()
broadcast.CreateBroadcast()
}
// @title Falcosidekick UI
// @version 1.0
// @description Falcosidekick UI
// @contact.name Falco Authors
// @contact.url https://github.com/falcosecurity
// @contact.email [email protected]
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @accept json
// @produce json
// @schemes http
// @host <your-domain>:2802
// @BasePath /api/v1
func main() {
e := echo.New()
v := new(CustomValidator)
c := configuration.GetConfiguration()
e.Validator = v
if c.DevMode {
utils.WriteLog("info", "DEV mode enabled", false)
e.Use(middleware.CORS())
}
e.GET("/docs/*", echoSwagger.WrapHandler)
e.GET("/docs", func(c echo.Context) error { //2
if err := c.Redirect(http.StatusPermanentRedirect, "docs/"); err != nil {
return err
}
return nil
})
e.POST("/", api.AddEvent).Name = "add-event"
// e.Use(middleware.BodyDump(func(c echo.Context, reqBody, resBody []byte) {
// }))
apiRoute := e.Group("/api/v1")
apiRoute.GET("/configuration", api.GetConfiguration).Name = "get-configuration"
apiRoute.GET("/version", api.GetVersionInfo).Name = "get-version"
apiRoute.GET("/healthz", api.Healthz).Name = "healthz"
apiRoute.GET("/outputs", api.GetOutputs).Name = "list-outputs"
apiRoute.GET("/config", api.GetConfiguration).Name = "display-cnfig"
apiRoute.GET("/ws", broadcast.WebSocketBroadcast).Name = "websocket"
eventsRoute := apiRoute.Group("/events")
eventsRoute.POST("/add", api.AddEvent).Name = "add-event"
eventsRoute.GET("/count", api.CountEvent).Name = "count-events"
eventsRoute.GET("/count/:groupby", api.CountByEvent).Name = "count-events-by"
eventsRoute.GET("/search", api.Search).Name = "search-keys"
e.Static("/*", "frontend/dist").Name = "webui-home"
e.Static("/dashboard", "frontend/dist").Name = "webui-dashboard"
e.Static("/events", "frontend/dist").Name = "webui-events"
e.Static("/info", "frontend/dist").Name = "webui-info"
e.Logger.Fatal(e.Start(fmt.Sprintf("%v:%v", c.ListenAddress, c.ListenPort)))
}
func (cv *CustomValidator) Validate(i interface{}) error {
if err := cv.validator.Struct(i); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return nil
}