Browse Source

set up statik file system and move session management to occur over websockets

Jordan Schalm 8 years ago
parent
commit
55eedaa9f5
7 changed files with 97 additions and 58 deletions
  1. 1 0
      .gitignore
  2. 9 0
      build.sh
  3. 17 54
      dashboard/dashboard.go
  4. 41 0
      dashboard/js/html/index.html
  5. 11 0
      dashboard/js/src/index.js
  6. 14 4
      glide.lock
  7. 4 0
      glide.yaml

+ 1 - 0
.gitignore

@@ -2,3 +2,4 @@
 goguerrilla.conf
 /guerrillad
 vendor
+statik

+ 9 - 0
build.sh

@@ -0,0 +1,9 @@
+#!/bin/bash
+
+# Build frontend to `dashboard/js/build`
+# cd dashboard/js && npm i && cd ../../
+cd dashboard/js && npm run build && cd ../../
+# Build statik file system in `dashboard/statik`
+statik -src=dashboard/js/build -dest=dashboard
+# Build guerrillad
+make guerrillad

+ 17 - 54
dashboard/dashboard.go

@@ -6,8 +6,11 @@ import (
 	"net/http"
 	"time"
 
+	log "github.com/Sirupsen/logrus"
+	_ "github.com/flashmob/go-guerrilla/dashboard/statik"
 	"github.com/gorilla/mux"
 	"github.com/gorilla/websocket"
+	"github.com/rakyll/statik/fs"
 )
 
 const (
@@ -33,11 +36,12 @@ type Config struct {
 }
 
 func Run(c *Config) {
+	statikFS, _ := fs.New()
 	config = c
 	sessions = map[string]*session{}
 	r := mux.NewRouter()
-	r.HandleFunc("/", indexHandler)
 	r.HandleFunc("/ws", webSocketHandler)
+	r.PathPrefix("/").Handler(http.FileServer(statikFS))
 
 	rand.Seed(time.Now().UnixNano())
 
@@ -57,61 +61,19 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
 	templates.ExecuteTemplate(w, dashboard, nil)
 }
 
-// func loginHandler(w http.ResponseWriter, r *http.Request) {
-// 	switch r.Method {
-// 	case "GET":
-// 		if isLoggedIn(r) {
-// 			http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
-// 		} else {
-// 			templates.ExecuteTemplate(w, login, nil)
-// 		}
-//
-// 	case "POST":
-// 		user := r.FormValue("username")
-// 		pass := r.FormValue("password")
-//
-// 		if user == config.Username && pass == config.Password {
-// 			err := startSession(w, r)
-// 			if err != nil {
-// 				w.WriteHeader(http.StatusInternalServerError)
-// 				// TODO Internal error
-// 				return
-// 			}
-// 			http.Redirect(w, r, "/", http.StatusSeeOther)
-// 		} else {
-// 			templates.ExecuteTemplate(w, login, nil) // TODO info about failed login
-// 		}
-//
-// 	default:
-// 		w.WriteHeader(http.StatusMethodNotAllowed)
-// 	}
-// }
-//
-// func logoutHandler(w http.ResponseWriter, r *http.Request) {
-// 	switch r.Method {
-// 	case "POST":
-// 		sess := getSession(r)
-// 		if sess == nil {
-// 			w.WriteHeader(http.StatusForbidden)
-// 			return
-// 		}
-//
-// 		store.unsubscribe(sess.id)
-// 		sess.expires = time.Now()
-// 		http.Redirect(w, r, "/", http.StatusSeeOther)
-//
-// 	default:
-// 		w.WriteHeader(http.StatusMethodNotAllowed)
-// 	}
-// }
-
 func webSocketHandler(w http.ResponseWriter, r *http.Request) {
-	sess := getSession(r)
-	if sess == nil {
+	log.Info("dashboard:112")
+	cookie, err := r.Cookie("SID")
+	if err != nil {
+		// TODO error
 		w.WriteHeader(http.StatusInternalServerError)
-		// TODO Internal error
-		return
 	}
+	sess, sidExists := sessions[cookie.Value]
+	if !sidExists {
+		// No SID cookie
+		sess = startSession(w, r)
+	}
+
 	conn, err := upgrader.Upgrade(w, r, nil)
 	if err != nil {
 		w.WriteHeader(http.StatusInternalServerError)
@@ -127,7 +89,7 @@ func webSocketHandler(w http.ResponseWriter, r *http.Request) {
 	go sess.transmit()
 }
 
-func startSession(w http.ResponseWriter, r *http.Request) {
+func startSession(w http.ResponseWriter, r *http.Request) *session {
 	sessionID := newSessionID()
 
 	cookie := &http.Cookie{
@@ -143,6 +105,7 @@ func startSession(w http.ResponseWriter, r *http.Request) {
 
 	http.SetCookie(w, cookie)
 	sessions[sessionID] = sess
+	return sess
 }
 
 func getSession(r *http.Request) *session {

+ 41 - 0
dashboard/js/html/index.html

@@ -0,0 +1,41 @@
+<!DOCTYPE html>
+<html>
+	<head>
+		<title>Guerrilla | Dashboard</title>
+	</head>
+	<body>
+		<canvas id="ram-graph" width="500" height="200"></canvas>
+	</body>
+	<!--script src="https://cdnjs.cloudflare.com/ajax/libs/smoothie/1.27.0/smoothie.min.js"></script-->
+	<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
+	<!--script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script-->
+
+	<!-- imports global MG variable -->
+	<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
+	<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.1.1/d3.min.js"></script>
+	<script src="https://cdnjs.cloudflare.com/ajax/libs/metrics-graphics/2.11.0/metricsgraphics.js"></script>
+	<script>
+
+	var data = [1,2,3,4,5,6,7,8,9,10].map(n => ({date: moment().add(n, 'days').format('YYYY-MM-DD'), value: n}));
+	console.log(data);
+	MG.data_graphic({
+		title: "Line Chart",
+		description: "This is a simple line chart. You can remove the area portion by adding area: false to the arguments list.",
+		data: data,
+		width: 600,
+		height: 200,
+		right: 40,
+		target: document.getElementById('ram-graph'),
+		x_accessor: 'date',
+		y_accessor: 'value'
+	});
+
+	// var smoothie = new SmoothieChart();
+	// var ram = new TimeSeries();
+	//
+	// smoothie.addTimeSeries(ram);
+	// smoothie.streamTo(document.getElementById('ram-graph'), 1000);
+
+
+	</script>
+</html>

+ 11 - 0
dashboard/js/src/index.js

@@ -7,3 +7,14 @@ ReactDOM.render(
   <App />,
   document.getElementById('root')
 );
+
+var conn = new WebSocket('ws://localhost:8080/ws');
+conn.onclose = function(event) {
+	console.log(event);
+}
+
+conn.onmessage = function(event) {
+	console.log(JSON.parse(event.data));
+	var point = JSON.parse(event.data);
+	// ram.append(new Date(point.t).getTime(), point.y);
+}

+ 14 - 4
glide.lock

@@ -1,21 +1,31 @@
-hash: d96dd7a4b78faacec2eb40ef44e4e6598abf0be703021cae6dacd8080b6f4f54
-updated: 2016-12-16T15:58:00.591876692-08:00
+hash: ce60d2a4a0a6b12d61788d65a3c64a31f0174e91ab796544260398dd0aa43114
+updated: 2017-01-24T11:26:52.507726508-08:00
 imports:
 - name: github.com/garyburd/redigo
   version: 8873b2f1995f59d4bcdd2b0dc9858e2cb9bf0c13
   subpackages:
   - internal
   - redis
+- name: github.com/gorilla/context
+  version: 08b5f424b9271eedf6f9f0ce86cb9396ed337a42
+- name: github.com/gorilla/mux
+  version: 392c28fe23e1c45ddba891b0320b3b5df220beea
+- name: github.com/gorilla/websocket
+  version: 0674c7c7968d9fac5f0f678325161ec31df406af
 - name: github.com/inconshreveable/mousetrap
   version: 76626ae9c91c4f2a10f34cad8ce83ea42c93bb75
+- name: github.com/rakyll/statik
+  version: 274df120e9065bdd08eb1120e0375e3dc1ae8465
+  subpackages:
+  - fs
 - name: github.com/Sirupsen/logrus
   version: d26492970760ca5d33129d2d799e34be5c4782eb
 - name: github.com/sloonz/go-qprintable
   version: 775b3a4592d5bfc47b0ba398ec0d4dba018e5926
 - name: github.com/spf13/cobra
-  version: b62566898a99f2db9c68ed0026aa0a052e59678d
+  version: 0f056af21f5f368e5b0646079d0094a2c64150f7
 - name: github.com/spf13/pflag
-  version: 25f8b5b07aece3207895bf19f7ab517eb3b22a40
+  version: a232f6d9f87afaaa08bafaff5da685f974b83313
 - name: github.com/ziutek/mymysql
   version: e08c2f35356576b3c3690c252fe5dca728ae9292
   subpackages:

+ 4 - 0
glide.yaml

@@ -15,3 +15,7 @@ import:
   - godrv
 - package: gopkg.in/iconv.v1
   version: ~1.1.1
+- package: github.com/rakyll/statik
+  version: ~0.1.0
+  subpackages:
+  - fs