rename stuff

This commit is contained in:
Fabricio 2018-11-16 19:39:53 -02:00
parent 0d73abcefd
commit b92002c15d

30
main.go
View file

@ -32,10 +32,10 @@ func startCapture(config Config) {
http.Handle(config.DashboardClearPath, dashboardClearHandler()) http.Handle(config.DashboardClearPath, dashboardClearHandler())
http.Handle(config.DashboardItemInfoPath, dashboardItemInfoHandler()) http.Handle(config.DashboardItemInfoPath, dashboardItemInfoHandler())
proxyHost := fmt.Sprintf("http://localhost:%s", config.ProxyPort) captureHost := fmt.Sprintf("http://localhost:%s", config.ProxyPort)
fmt.Printf("\nListening on %s", proxyHost) fmt.Printf("\nListening on %s", captureHost)
fmt.Printf("\n %s/%s\n\n", proxyHost, config.Dashboard) fmt.Printf("\n %s/%s\n\n", captureHost, config.Dashboard)
fmt.Println(http.ListenAndServe(":"+config.ProxyPort, nil)) fmt.Println(http.ListenAndServe(":"+config.ProxyPort, nil))
} }
@ -43,7 +43,7 @@ func startCapture(config Config) {
func dashboardSocketHandler(config Config) http.Handler { func dashboardSocketHandler(config Config) http.Handler {
server, err := socketio.NewServer(nil) server, err := socketio.NewServer(nil)
if err != nil { if err != nil {
fmt.Println("socket server error", err) fmt.Printf("socket server error: %v\n", err)
} }
server.On("connection", func(so socketio.Socket) { server.On("connection", func(so socketio.Socket) {
dashboardSocket = so dashboardSocket = so
@ -51,34 +51,34 @@ func dashboardSocketHandler(config Config) http.Handler {
emitToDashboard(captures) emitToDashboard(captures)
}) })
server.On("error", func(so socketio.Socket, err error) { server.On("error", func(so socketio.Socket, err error) {
fmt.Println("socket error", err) fmt.Printf("socket error: %v\n", err)
}) })
return server return server
} }
func dashboardClearHandler() http.Handler { func dashboardClearHandler() http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
captures = nil captures = nil
emitToDashboard(captures) emitToDashboard(captures)
res.Write([]byte("")) rw.WriteHeader(http.StatusOK)
}) })
} }
func dashboardHandler() http.Handler { func dashboardHandler() http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
res.Header().Add("Content-Type", "text/html") rw.Header().Add("Content-Type", "text/html")
res.Write([]byte(dashboardHTML)) fmt.Fprint(rw, dashboardHTML)
}) })
} }
func dashboardItemInfoHandler() http.Handler { func dashboardItemInfoHandler() http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
idStr := req.URL.Path[strings.LastIndex(req.URL.Path, "/")+1:] idStr := req.URL.Path[strings.LastIndex(req.URL.Path, "/")+1:]
idInt, _ := strconv.Atoi(idStr) idInt, _ := strconv.Atoi(idStr)
for _, c := range captures { for _, c := range captures {
if c.ID == idInt { if c.ID == idInt {
res.Header().Add("Content-Type", "application/json") rw.Header().Add("Content-Type", "application/json")
json.NewEncoder(res).Encode(c) json.NewEncoder(rw).Encode(c)
break break
} }
} }
@ -95,14 +95,14 @@ func proxyHandler(config Config) http.Handler {
reqDump, err := dumpRequest(req) reqDump, err := dumpRequest(req)
if err != nil { if err != nil {
fmt.Printf("Could not dump request: %v\n", err) fmt.Printf("could not dump request: %v\n", err)
} }
proxy := httputil.NewSingleHostReverseProxy(url) proxy := httputil.NewSingleHostReverseProxy(url)
proxy.ModifyResponse = func(res *http.Response) error { proxy.ModifyResponse = func(res *http.Response) error {
resDump, err := dumpResponse(res) resDump, err := dumpResponse(res)
if err != nil { if err != nil {
return fmt.Errorf("Could not dump response: %v", err) return fmt.Errorf("could not dump response: %v", err)
} }
captureID++ captureID++
capture := Capture{ capture := Capture{