diff --git a/config.go b/config.go index d7e1386..4198288 100644 --- a/config.go +++ b/config.go @@ -11,6 +11,7 @@ type Config struct { MaxCaptures int `json:"maxCaptures"` Dashboard string `json:"dashboard"` DashboardPath string `json:"dashboardPath"` + DashboardConnPath string `json:"dashboardConnPath"` DashboardClearPath string `json:"dashboardClearPath"` DashboardItemInfoPath string `json:"dashboardItemInfoPath"` } @@ -22,6 +23,7 @@ func ReadConfig() Config { maxCaptures := flag.Int("max-captures", 16, "Set the max number of captures to show in the dashboard") flag.Parse() + dashboardConnPath := "/socket.io/" dashboardPath := fmt.Sprintf("/%s/", *dashboard) dashboardClearPath := fmt.Sprintf("/%s/clear/", *dashboard) dashboardItemInfoPath := fmt.Sprintf("/%s/items/", *dashboard) @@ -32,6 +34,7 @@ func ReadConfig() Config { MaxCaptures: *maxCaptures, Dashboard: *dashboard, DashboardPath: dashboardPath, + DashboardConnPath: dashboardConnPath, DashboardClearPath: dashboardClearPath, DashboardItemInfoPath: dashboardItemInfoPath, } diff --git a/main.go b/main.go index 2bd6dce..4c4ec9e 100644 --- a/main.go +++ b/main.go @@ -27,11 +27,11 @@ func startCapture(config Config) { repo := NewCapturesRepository(config.MaxCaptures) - http.Handle("/", NewRecorder(repo, proxyHandler(config.TargetURL))) - http.Handle("/socket.io/", dashboardSocketHandler(repo, config)) - http.Handle(config.DashboardPath, dashboardHandler()) - http.Handle(config.DashboardClearPath, dashboardClearHandler(repo)) - http.Handle(config.DashboardItemInfoPath, dashboardItemInfoHandler(repo)) + http.Handle("/", NewRecorder(repo, NewProxyHandler(config.TargetURL))) + http.Handle(config.DashboardPath, NewDashboardHtmlHandler()) + http.Handle(config.DashboardClearPath, NewDashboardClearHandler(repo)) + http.Handle(config.DashboardItemInfoPath, NewDashboardItemInfoHandler(repo)) + http.Handle(config.DashboardConnPath, NewDashboardSocketHandler(repo, config)) captureHost := fmt.Sprintf("http://localhost:%s", config.ProxyPort) @@ -41,7 +41,7 @@ func startCapture(config Config) { fmt.Println(http.ListenAndServe(":"+config.ProxyPort, nil)) } -func dashboardSocketHandler(repo CaptureRepository, config Config) http.Handler { +func NewDashboardSocketHandler(repo CaptureRepository, config Config) http.Handler { server, err := socketio.NewServer(nil) if err != nil { fmt.Printf("socket server error: %v\n", err) @@ -57,7 +57,7 @@ func dashboardSocketHandler(repo CaptureRepository, config Config) http.Handler return server } -func dashboardClearHandler(repo CaptureRepository) http.Handler { +func NewDashboardClearHandler(repo CaptureRepository) http.Handler { return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { repo.RemoveAll() emitToDashboard(nil) @@ -65,14 +65,14 @@ func dashboardClearHandler(repo CaptureRepository) http.Handler { }) } -func dashboardHandler() http.Handler { +func NewDashboardHtmlHandler() http.Handler { return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { rw.Header().Add("Content-Type", "text/html") fmt.Fprint(rw, dashboardHTML) }) } -func dashboardItemInfoHandler(repo CaptureRepository) http.Handler { +func NewDashboardItemInfoHandler(repo CaptureRepository) http.Handler { return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { id := req.URL.Path[strings.LastIndex(req.URL.Path, "/")+1:] capture := repo.Find(id) @@ -119,7 +119,7 @@ func NewRecorder(repo CaptureRepository, next http.Handler) http.Handler { }) } -func proxyHandler(URL string) http.Handler { +func NewProxyHandler(URL string) http.Handler { url, _ := url.Parse(URL) proxy := httputil.NewSingleHostReverseProxy(url) proxy.ErrorHandler = func(rw http.ResponseWriter, req *http.Request, err error) { diff --git a/main_test.go b/main_test.go index 43b0403..7db317c 100644 --- a/main_test.go +++ b/main_test.go @@ -26,7 +26,7 @@ func TestProxyHandler(t *testing.T) { for _, tc := range tt { t.Run(tc.name, func(t *testing.T) { service := httptest.NewServer(http.HandlerFunc(tc.service)) - capture := httptest.NewServer(proxyHandler(service.URL)) + capture := httptest.NewServer(NewProxyHandler(service.URL)) // when resp := tc.request(capture.URL) @@ -182,13 +182,13 @@ func TestCaptureIDConcurrence(t *testing.T) { rw.WriteHeader(http.StatusOK) })) repo := NewCapturesRepository(interactions) - capture := httptest.NewServer(NewRecorder(repo, proxyHandler(service.URL))) + capture := httptest.NewServer(NewRecorder(repo, NewProxyHandler(service.URL))) defer service.Close() defer capture.Close() // when - // Starts go routines so that captureID is incremented concurrently within proxyHandler() + // Starts go routines so that captureID is incremented concurrently within NewProxyHandler() wg := &sync.WaitGroup{} wg.Add(interactions) for i := 0; i < interactions; i++ {