This commit is contained in:
Fabricio 2018-08-01 19:36:23 -03:00
commit ce77c1b497
2 changed files with 33 additions and 37 deletions

View file

@ -1,16 +1,5 @@
package main
import (
"net/http"
)
func getDashboardHandler() http.Handler {
return http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
response.Header().Add("Content-Type", "text/html")
response.Write([]byte(dashboardHTML))
})
}
const dashboardHTML = `
<!DOCTYPE html>
<html ng-app="app">

73
main.go
View file

@ -8,7 +8,6 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"strconv"
@ -17,7 +16,7 @@ import (
"github.com/googollee/go-socket.io"
)
type Transport struct {
type transport struct {
http.RoundTripper
}
@ -28,20 +27,17 @@ var socket socketio.Socket
var host string
var dashboardPath string
var dashboardItemsPath string
var dashboardItemPath string
func main() {
parseArgs()
proxy := httputil.NewSingleHostReverseProxy(args.url)
proxy.Transport = Transport{http.DefaultTransport}
dashboardPath = "/" + args.dashboard + "/"
dashboardItemsPath = dashboardPath + "items/"
dashboardItemPath = dashboardPath + "items/"
http.Handle("/", getProxyHandler(proxy))
http.Handle("/", getProxyHandler())
http.Handle("/socket.io/", getSocketHandler())
http.Handle(dashboardItemsPath, getCapturesHandler())
http.Handle(dashboardItemPath, getDashboardItemHandler())
http.Handle(dashboardPath, getDashboardHandler())
host = "http://localhost:" + args.port
@ -49,12 +45,38 @@ func main() {
fmt.Printf("\nListening on %s", host)
fmt.Printf("\n %s/%s\n\n", host, args.dashboard)
http.ListenAndServe(":"+args.port, nil)
if err := http.ListenAndServe(":"+args.port, nil); err != nil {
fmt.Println(err)
}
}
func getCapturesHandler() http.Handler {
func getProxyHandler() http.Handler {
proxy := httputil.NewSingleHostReverseProxy(args.url)
proxy.Transport = transport{http.DefaultTransport}
return http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
request.Host = request.URL.Host
proxy.ServeHTTP(response, request)
})
}
func getSocketHandler() http.Handler {
server, err := socketio.NewServer(nil)
if err != nil {
panic(err)
}
server.On("connection", func(so socketio.Socket) {
socket = so
emit()
})
server.On("error", func(so socketio.Socket, err error) {
fmt.Println("socket error:", err)
})
return server
}
func getDashboardItemHandler() http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
id := strings.TrimPrefix(req.URL.Path, dashboardItemsPath)
id := strings.TrimPrefix(req.URL.Path, dashboardItemPath)
i, _ := strconv.Atoi(id)
json, _ := json.Marshal(captures[i])
res.Header().Add("Content-Type", "application/json")
@ -62,29 +84,14 @@ func getCapturesHandler() http.Handler {
})
}
func getProxyHandler(handler http.Handler) http.Handler {
return http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
request.Host = request.URL.Host
handler.ServeHTTP(response, request)
func getDashboardHandler() http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.Header().Add("Content-Type", "text/html")
res.Write([]byte(dashboardHTML))
})
}
func getSocketHandler() http.Handler {
server, err := socketio.NewServer(nil)
if err != nil {
log.Fatal(err)
}
server.On("connection", func(so socketio.Socket) {
socket = so
emit()
})
server.On("error", func(so socketio.Socket, err error) {
log.Println("socket error:", err)
})
return server
}
func (t Transport) RoundTrip(req *http.Request) (*http.Response, error) {
func (t transport) RoundTrip(req *http.Request) (*http.Response, error) {
reqDump, err := DumpRequest(req)
if err != nil {
@ -137,5 +144,5 @@ func emit() {
if socket == nil {
return
}
socket.Emit("captures", captures.ToReferences(host+dashboardItemsPath))
socket.Emit("captures", captures.ToReferences(host+dashboardItemPath))
}