improve the look a lot, add webpage addition

This commit is contained in:
Barak Michener 2013-09-08 17:47:11 -04:00
parent 4f623d4897
commit 3986a3f9ad
2 changed files with 62 additions and 7 deletions

View file

@ -62,7 +62,7 @@ func CopyBidir(conn1 io.ReadWriteCloser, rw1 *bufio.ReadWriter, conn2 io.ReadWri
} }
func (h *RequestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *RequestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Printf("incoming request: %#v\n", *r) //fmt.Printf("incoming request: %#v\n", *r)
r.RequestURI = "" r.RequestURI = ""
r.URL.Scheme = "http" r.URL.Scheme = "http"
@ -139,7 +139,7 @@ func (h *RequestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
resp, err := h.Transport.RoundTrip(r) resp, err := h.Transport.RoundTrip(r)
if err != nil { if err != nil {
w.WriteHeader(http.StatusServiceUnavailable) w.WriteHeader(http.StatusServiceUnavailable)
fmt.Fprintf(w, "Error: %v", err) fmt.Fprintf(w, "Error: %v\n", err)
return return
} }
@ -198,11 +198,18 @@ type RootHandler struct {
Routes chan *ForwardSpec Routes chan *ForwardSpec
} }
var add_templ = `
<tr>
<td> {{.Hostname}} </td>
<td> {{.Target}} </td>
</tr>
`
func (h *RootHandler) HandleAdd(w http.ResponseWriter, req *http.Request) { func (h *RootHandler) HandleAdd(w http.ResponseWriter, req *http.Request) {
host := req.URL.Query().Get("host") host := req.URL.Query().Get("host")
target := req.URL.Query().Get("target") target := req.URL.Query().Get("target")
if host != "" && target != "" { if host != "" && target != "" {
h.AddForward(host, target) h.AddForward(host, target, w)
} }
} }
@ -218,10 +225,15 @@ func (h *RootHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
} }
func (h *RootHandler) AddForward(host, target string) { func (h *RootHandler) AddForward(host, target string, w http.ResponseWriter) {
fwd := &ForwardSpec{Hostname: host, Target: target} fwd := &ForwardSpec{Hostname: host, Target: target}
h.Forwards = append(h.Forwards, fwd) h.Forwards = append(h.Forwards, fwd)
h.Routes <- fwd h.Routes <- fwd
if w != nil {
t := template.New("Add template")
t, _ = t.Parse(add_templ)
t.Execute(w, fwd)
}
} }
func ServeCfg(routes chan *ForwardSpec) { func ServeCfg(routes chan *ForwardSpec) {
@ -232,7 +244,7 @@ func ServeCfg(routes chan *ForwardSpec) {
Forwards: make([]*ForwardSpec, 0, 20), Forwards: make([]*ForwardSpec, 0, 20),
Routes: routes, Routes: routes,
} }
handler.AddForward("switchyard.app.barakmich.com", "10.42.0.2:8889") handler.AddForward("switchyard.app.barakmich.com", "10.42.0.2:8889", nil)
mux.Handle("/", handler) mux.Handle("/", handler)
addr := fmt.Sprintf(":%d", *cfg_port) addr := fmt.Sprintf(":%d", *cfg_port)
srv := &http.Server{Handler: mux, Addr: addr} srv := &http.Server{Handler: mux, Addr: addr}

View file

@ -1,12 +1,32 @@
<html> <html>
<head> <head>
<title>Switchyard Server</title> <title>Switchyard Server</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
<!-- Optional theme -->
<!--<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css">-->
<!-- Latest compiled and minified JavaScript -->
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<style type="text/css">
[class*="col-"] {
padding-top: 15px;
padding-bottom: 15px;
background-color: #eee;
border: 1px solid #ddd;
background-color: rgba(86,86,124,.15);
border: 1px solid rgba(86,86,124,.2);
}
</style>
</head> </head>
<body> <body>
<div class="container">
<h1>Switchyard</h1> <h1>Switchyard</h1>
<h2>Current Routes:</h2> <h2>Current Routes:</h2>
<table> <table class="table" id="hosttable">
<thead> <thead>
<td>Hostname</td> <td>Hostname</td>
<td>Target</td> <td>Target</td>
</thead> </thead>
@ -17,6 +37,29 @@
</tr> </tr>
{{end}} {{end}}
</table> </table>
<h2>Add a route:</h2>
<table class="table">
<tr>
<td><input type="text" name="newhost"/></td>
<td><input type="text" name="newpath"/></td>
<td><button type="button" id="add_button" class="btn btn-primary">Add Route</button></td>
</tr>
</table>
</div>
</body> </body>
<script type="text/javascript">
$(function() {
$("#add_button").click(function() {
var data = {
"host": $("[name=newhost]").val(),
"target": $("[name=newpath]").val()
}
$.get("/add", data, function(return_data) {
$("#hosttable > tbody").append(return_data);
});
});
});
</script>
</html> </html>