Browse Source

use doozer's file2gostring to provide html temp as go string

Xiang Li 12 years ago
parent
commit
494b35596b
4 changed files with 101 additions and 1 deletions
  1. 26 0
      web/file2gostring.sh
  2. 4 0
      web/index.go
  3. 70 0
      web/index.html
  4. 1 1
      web/web.go

+ 26 - 0
web/file2gostring.sh

@@ -0,0 +1,26 @@
+#!/bin/sh
+
+set -e
+
+munge() {
+    printf %s "$1" | tr . _ | tr -d -c '[:alnum:]_'
+}
+
+quote() {
+    sed 's/\\/\\\\/g' | sed 's/"/\\"/g' | sed 's/$/\\n/' | tr -d '\n'
+}
+
+pkg_path=$1 ; shift
+file=$1     ; shift
+
+pkg=`basename $pkg_path`
+
+printf 'package %s\n' "$pkg"
+printf '\n'
+printf '// This file was generated from %s.\n' "$file"
+printf '\n'
+printf 'var '
+munge "`basename $file`"
+printf ' string = "'
+quote
+printf '"\n'

File diff suppressed because it is too large
+ 4 - 0
web/index.go


+ 70 - 0
web/index.html

@@ -0,0 +1,70 @@
+<html>
+<head>
+<title>etcd Web Interface</title>
+<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
+<script type="text/javascript">
+    $(function() {
+
+    var conn;
+    var content = $("#content");
+
+    function update(response) {
+        // if set
+        if (response.action == "SET") {
+
+            if (response.expiration > "1970") {
+                t = response.key + "=" + response.value
+                        + "  " + response.expiration
+            } else {
+                t = response.key + "=" + response.value
+            }
+
+            id = response.key.replace(new RegExp("/", 'g'), "\\/");
+
+            if ($("#store_" + id).length == 0) {
+                if (response.expiration > "1970") {
+                    t = response.key + "=" + response.value
+                        + "  " + response.expiration
+                } else {
+                    t = response.key + "=" + response.value
+                }
+
+                var e = $('<div id="store_' + response.key + '"/>')
+                    .text(t)
+                e.appendTo(content)
+            }
+            else {
+
+                $("#store_" + id)
+                    .text(t)
+            }
+        }
+        // if delete
+        else if (response.action == "DELETE") {
+            id = response.key.replace(new RegExp("/", 'g'), "\\/");
+
+            $("#store_" + id).remove()
+        }
+    }
+
+
+    if (window["WebSocket"]) {
+        conn = new WebSocket("ws://{{.Address}}/ws");
+        conn.onclose = function(evt) {
+
+        }
+        conn.onmessage = function(evt) {
+            var response = JSON.parse(evt.data)
+            update(response)
+        }
+    } else {
+        appendLog($("<div><b>Your browser does not support WebSockets.</b></div>"))
+    }
+    });
+</script>
+</head>
+<body>
+    <div id="leader">Leader: {{.Leader}}</div>
+    <div id="content"></div>
+</body>
+</html>

+ 1 - 1
web/web.go

@@ -25,7 +25,7 @@ func mainHandler(c http.ResponseWriter, req *http.Request) {
 }
 
 func Start(server *raft.Server, port int) {
-	mainTempl = template.Must(template.ParseFiles("web/index.html"))
+	mainTempl = template.Must(template.New("index.html").Parse(index_html))
 	s = server
 
 	go h.run()

Some files were not shown because too many files changed in this diff