forked from cert-lv/graphoscope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
185 lines (150 loc) · 4.03 KB
/
handlers.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
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
package main
import (
"html/template"
"net"
"net/http"
"github.com/cert-lv/graphoscope/pdk"
)
/*
* Structure to be inserted in the HTML templates
* as a dynamic content
*/
type TemplateData struct {
// A list of connected data sources,
// will be used to generate sources dropdowns
Collectors map[string]pdk.SourcePlugin
// A list of shared dashboards to be loaded
Shared map[string]*Dashboard
// Helper variable to hide some HTML elements
// when non-global data sources don't exist
NonGlobalExist bool
// Nodes styling definition.
// Will be applied to the JavaScript engine
Groups string
// Query formatting rules,
// which help to format comma/space separated indicators to a valid SQL query
Formats string
// A list of all known data sources fields for the Web GUI autocomplete
Fields map[string][]string
// Built-in documentation content, N sections
Docs [3]string
// Currently signed in user
Account *Account
// A list of all registered users
Accounts []*Account
// A list of new features for the current service's version.
// Will be displayed once for each user
Features []string
// Graph UI settings
GraphSettings *GraphSettings
// Service runs in a Development or Production environment
Environment string
// The latest service's version
Version string
// Possible error to show to the user
Error string
}
/*
* Render requested HTML template.
*
* Receives a template's name, a dynamic data to insert and
* functions to run in Go templates
*/
func renderTemplate(w http.ResponseWriter, tmpl string, data *TemplateData, funcs template.FuncMap) {
// Fill static info
data.Environment = config.Environment
data.Version = version
templates := template.New("").Funcs(funcs)
// Parse needed HTML template
templates, err := templates.ParseFiles(
"assets/tmpl/"+tmpl+".html",
"assets/tmpl/modal.html",
"assets/tmpl/topbar.html",
"assets/tmpl/credits.html",
)
if err != nil {
log.Error().Msg(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = templates.ExecuteTemplate(w, tmpl+".html", data)
if err != nil {
log.Error().Msg(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
/*
* Serve main '/' web page
*/
func indexHandler(w http.ResponseWriter, r *http.Request) {
if r.RequestURI != "/" {
return
}
// Get client's IP
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
log.Error().Msg("Can't get IP for index page: " + err.Error())
return
}
// Check existing session
username, err := sessions.exists(w, r)
if err != nil {
log.Debug().
Str("ip", ip).
Msg(err.Error())
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
}
// Get account from a database
account, err := db.getAccount(username)
if err != nil {
log.Error().
Str("ip", ip).
Str("username", username).
Msg("Can't get account to get filters: " + err.Error())
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
}
// All are admins in a development environment
if config.Environment != "prod" {
account.Admin = true
}
// Get account from a database
shared, err := db.getSharedDashboards()
if err != nil {
log.Error().
Str("ip", ip).
Str("username", username).
Msg("Can't get shared dashboards: " + err.Error())
}
// Get Web UI settings
settings, err := db.getGraphSettings()
if err != nil {
log.Error().
Str("ip", ip).
Str("username", username).
Msg("Can't get graph UI settings: " + err.Error())
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
// Collect dynamic data
templateData := &TemplateData{
Account: account,
Collectors: collectors,
NonGlobalExist: nonGlobalExist,
Shared: shared,
Groups: groups,
Formats: formats,
Fields: fields,
GraphSettings: settings,
}
if account.SeenFeatures != features[0] {
templateData.Features = features
}
renderTemplate(w, "index", templateData, nil)
account.hideFeatures()
log.Info().
Str("ip", ip).
Str("username", username).
Msg("Index page requested")
}