mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
refactor remove web pacakge
This commit is contained in:
parent
c5f9afa0e8
commit
01bbad31c7
30
web/conn.go
30
web/conn.go
@ -1,30 +0,0 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"code.google.com/p/go.net/websocket"
|
||||
)
|
||||
|
||||
type connection struct {
|
||||
// The websocket connection.
|
||||
ws *websocket.Conn
|
||||
|
||||
// Buffered channel of outbound messages.
|
||||
send chan string
|
||||
}
|
||||
|
||||
func (c *connection) writer() {
|
||||
for message := range c.send {
|
||||
err := websocket.Message.Send(c.ws, message)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
c.ws.Close()
|
||||
}
|
||||
|
||||
func wsHandler(ws *websocket.Conn) {
|
||||
c := &connection{send: make(chan string, 256), ws: ws}
|
||||
h.register <- c
|
||||
defer func() { h.unregister <- c }()
|
||||
c.writer()
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
#!/bin/sh
|
||||
|
||||
# this file is copied from doozerd.
|
||||
|
||||
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'
|
61
web/hub.go
61
web/hub.go
@ -1,61 +0,0 @@
|
||||
package web
|
||||
|
||||
type hub struct {
|
||||
// status
|
||||
open bool
|
||||
|
||||
// Registered connections.
|
||||
connections map[*connection]bool
|
||||
|
||||
// Inbound messages from the connections.
|
||||
broadcast chan string
|
||||
|
||||
// Register requests from the connections.
|
||||
register chan *connection
|
||||
|
||||
// Unregister requests from connections.
|
||||
unregister chan *connection
|
||||
}
|
||||
|
||||
var h = hub{
|
||||
open: false,
|
||||
broadcast: make(chan string),
|
||||
register: make(chan *connection),
|
||||
unregister: make(chan *connection),
|
||||
connections: make(map[*connection]bool),
|
||||
}
|
||||
|
||||
func Hub() *hub {
|
||||
return &h
|
||||
}
|
||||
|
||||
func HubOpen() bool {
|
||||
return h.open
|
||||
}
|
||||
|
||||
func (h *hub) run() {
|
||||
h.open = true
|
||||
for {
|
||||
select {
|
||||
case c := <-h.register:
|
||||
h.connections[c] = true
|
||||
case c := <-h.unregister:
|
||||
delete(h.connections, c)
|
||||
close(c.send)
|
||||
case m := <-h.broadcast:
|
||||
for c := range h.connections {
|
||||
select {
|
||||
case c.send <- m:
|
||||
default:
|
||||
delete(h.connections, c)
|
||||
close(c.send)
|
||||
go c.ws.Close()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (h *hub) Send(msg string) {
|
||||
h.broadcast <- msg
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
package web
|
||||
|
||||
// This file was generated from index.html.
|
||||
|
||||
var index_html string = "<html>\n<head>\n<title>etcd Web Interface</title>\n<script type=\"text/javascript\" src=\"//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js\"></script>\n<script type=\"text/javascript\">\n $(function() {\n\n var conn;\n var content = $(\"#content\");\n\n function update(response) {\n // if set\n if (response.action == \"SET\") {\n\n if (response.expiration > \"1970\") {\n t = response.key + \"=\" + response.value\n + \" \" + response.expiration\n } else {\n t = response.key + \"=\" + response.value\n }\n\n id = response.key.replace(new RegExp(\"/\", 'g'), \"\\\\/\");\n\n if ($(\"#store_\" + id).length == 0) {\n if (response.expiration > \"1970\") {\n t = response.key + \"=\" + response.value\n + \" \" + response.expiration\n } else {\n t = response.key + \"=\" + response.value\n }\n\n var e = $('<div id=\"store_' + response.key + '\"/>')\n .text(t)\n e.appendTo(content)\n }\n else {\n\n $(\"#store_\" + id)\n .text(t)\n }\n }\n // if delete\n else if (response.action == \"DELETE\") {\n id = response.key.replace(new RegExp(\"/\", 'g'), \"\\\\/\");\n\n $(\"#store_\" + id).remove()\n }\n }\n\n\n if (window[\"WebSocket\"]) {\n conn = new WebSocket(\"ws://{{.Address}}/ws\");\n conn.onclose = function(evt) {\n\n }\n conn.onmessage = function(evt) {\n var response = JSON.parse(evt.data)\n update(response)\n }\n } else {\n appendLog($(\"<div><b>Your browser does not support WebSockets.</b></div>\"))\n }\n });\n</script>\n</head>\n<body>\n <div id=\"leader\">Leader: {{.Leader}}</div>\n <div id=\"content\"></div>\n</body>\n</html>\n"
|
@ -1,70 +0,0 @@
|
||||
<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>
|
50
web/web.go
50
web/web.go
@ -1,50 +0,0 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"code.google.com/p/go.net/websocket"
|
||||
"fmt"
|
||||
"github.com/coreos/go-raft"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
var mainTempl *template.Template
|
||||
var mainPage *MainPage
|
||||
|
||||
type MainPage struct {
|
||||
Leader string
|
||||
Address string
|
||||
}
|
||||
|
||||
func mainHandler(c http.ResponseWriter, req *http.Request) {
|
||||
p := mainPage
|
||||
|
||||
mainTempl.Execute(c, p)
|
||||
}
|
||||
|
||||
func Start(raftServer raft.Server, webURL string) {
|
||||
u, _ := url.Parse(webURL)
|
||||
|
||||
webMux := http.NewServeMux()
|
||||
|
||||
server := &http.Server{
|
||||
Handler: webMux,
|
||||
Addr: u.Host,
|
||||
}
|
||||
|
||||
mainPage = &MainPage{
|
||||
Leader: raftServer.Leader(),
|
||||
Address: u.Host,
|
||||
}
|
||||
|
||||
mainTempl = template.Must(template.New("index.html").Parse(index_html))
|
||||
|
||||
go h.run()
|
||||
webMux.HandleFunc("/", mainHandler)
|
||||
webMux.Handle("/ws", websocket.Handler(wsHandler))
|
||||
|
||||
fmt.Printf("etcd web server [%s] listening on %s\n", raftServer.Name(), u)
|
||||
|
||||
server.ListenAndServe()
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user