|
@@ -1,7 +1,6 @@
|
|
package dashboard
|
|
package dashboard
|
|
|
|
|
|
import (
|
|
import (
|
|
- "html/template"
|
|
|
|
"math/rand"
|
|
"math/rand"
|
|
"net/http"
|
|
"net/http"
|
|
"time"
|
|
"time"
|
|
@@ -13,17 +12,12 @@ import (
|
|
"github.com/rakyll/statik/fs"
|
|
"github.com/rakyll/statik/fs"
|
|
)
|
|
)
|
|
|
|
|
|
-const (
|
|
|
|
- dashboard = "index.html"
|
|
|
|
- dashboardPath = "dashboard/html/index.html"
|
|
|
|
- sessionTimeout = time.Hour * 24 // TODO replace with config
|
|
|
|
-)
|
|
|
|
|
|
+const sessionTimeout = time.Hour * 24 // TODO replace with config
|
|
|
|
|
|
var (
|
|
var (
|
|
// Cache of HTML templates
|
|
// Cache of HTML templates
|
|
- templates = template.Must(template.ParseFiles(dashboardPath))
|
|
|
|
- config *Config
|
|
|
|
- sessions map[string]*session
|
|
|
|
|
|
+ config *Config
|
|
|
|
+ sessions map[string]*session
|
|
)
|
|
)
|
|
|
|
|
|
var upgrader = websocket.Upgrader{
|
|
var upgrader = websocket.Upgrader{
|
|
@@ -39,7 +33,6 @@ type Config struct {
|
|
|
|
|
|
// Begin collecting data and listening for dashboard clients
|
|
// Begin collecting data and listening for dashboard clients
|
|
func Run(c *Config) {
|
|
func Run(c *Config) {
|
|
- log.Info("Dashboard run")
|
|
|
|
statikFS, _ := fs.New()
|
|
statikFS, _ := fs.New()
|
|
config = c
|
|
config = c
|
|
sessions = map[string]*session{}
|
|
sessions = map[string]*session{}
|
|
@@ -54,19 +47,7 @@ func Run(c *Config) {
|
|
log.WithError(err).Error("Dashboard server failed to start")
|
|
log.WithError(err).Error("Dashboard server failed to start")
|
|
}
|
|
}
|
|
|
|
|
|
-func indexHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
- c, err := r.Cookie("SID")
|
|
|
|
- _, sidExists := sessions[c.Value]
|
|
|
|
- if err != nil || !sidExists {
|
|
|
|
- // No SID cookie
|
|
|
|
- startSession(w, r)
|
|
|
|
- }
|
|
|
|
- w.WriteHeader(http.StatusOK)
|
|
|
|
- templates.ExecuteTemplate(w, dashboard, nil)
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
func webSocketHandler(w http.ResponseWriter, r *http.Request) {
|
|
func webSocketHandler(w http.ResponseWriter, r *http.Request) {
|
|
- log.Info("websocket handler")
|
|
|
|
cookie, err := r.Cookie("SID")
|
|
cookie, err := r.Cookie("SID")
|
|
if err != nil {
|
|
if err != nil {
|
|
// TODO error
|
|
// TODO error
|
|
@@ -112,19 +93,3 @@ func startSession(w http.ResponseWriter, r *http.Request) *session {
|
|
sessions[sessionID] = sess
|
|
sessions[sessionID] = sess
|
|
return sess
|
|
return sess
|
|
}
|
|
}
|
|
-
|
|
|
|
-// TODO unused
|
|
|
|
-func getSession(r *http.Request) *session {
|
|
|
|
- c, err := r.Cookie("SID")
|
|
|
|
- if err != nil {
|
|
|
|
- return nil
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- sid := c.Value
|
|
|
|
- sess, ok := sessions[sid]
|
|
|
|
- if !ok {
|
|
|
|
- return nil
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return sess
|
|
|
|
-}
|
|
|