dashboard on different port, closes #1

This commit is contained in:
Fabricio 2019-11-08 20:27:02 -03:00
parent b49eba8fe1
commit 760639465e
5 changed files with 40 additions and 48 deletions

View file

@ -16,13 +16,13 @@ For ready-to-use executables for *Windows*, *Linux* and *Mac*, see [Releases](ht
./capture -url=https://example.com/
### Configurations
### Settings
| param | description |
|-----------------|-------------|
| `-url` | **Required.** Set the url you want to proxy |
| `-port` | Set the proxy port. Default: *9000* |
| `-dashboard` | Set the dashboard's name. Default: *dashboard* |
| `-dashboard` | Set the dashboard port. Default: *9001* |
| `-max-captures` | Set the max number of captures to show in the dashboard. Default: *16* |
@ -36,11 +36,7 @@ address. Hence, calling `http://localhost:9000/users/1` is like calling `http://
## Dashboard
To access the dashboard go to `http://localhost:9000/dashboard`
The path `/dashboard/**` is reserved, that means if your api has a path like that it will be ignored
in favor of the dashboard. However, you can change the dashboard's name with `-dashboard`
To access the dashboard go to `http://localhost:9001/`
##### Preview

View file

@ -2,39 +2,27 @@ package main
import (
"flag"
"fmt"
)
// Config has all the configuration parsed from the command line
type Config struct {
TargetURL string `json:"targetURL"`
ProxyPort string `json:"proxyPort"`
MaxCaptures int `json:"maxCaptures"`
DashboardPath string `json:"dashboardPath"`
DashboardConnPath string `json:"dashboardConnPath"`
DashboardInfoPath string `json:"dashboardInfoPath"`
DashboardClearPath string `json:"dashboardClearPath"`
DashboardRetryPath string `json:"dashboardRetryPath"`
TargetURL string
ProxyPort string
DashboardPort string
MaxCaptures int
}
// ReadConfig reads the arguments from the command line
func ReadConfig() Config {
targetURL := flag.String("url", "https://jsonplaceholder.typicode.com", "Required. Set the base url you want to capture")
proxyPort := flag.String("port", "9000", "Set the proxy port")
dashboard := flag.String("dashboard", "dashboard", "Set the dashboard name")
dashboardPort := flag.String("dashboard", "9001", "Set the dashboard port")
maxCaptures := flag.Int("max-captures", 16, "Set the max number of captures to show in the dashboard")
flag.Parse()
return Config{
TargetURL: *targetURL,
ProxyPort: *proxyPort,
MaxCaptures: *maxCaptures,
DashboardPath: fmt.Sprintf("/%s/", *dashboard),
DashboardConnPath: fmt.Sprintf("/%s/conn/", *dashboard),
DashboardInfoPath: fmt.Sprintf("/%s/info/", *dashboard),
DashboardClearPath: fmt.Sprintf("/%s/clear/", *dashboard),
DashboardRetryPath: fmt.Sprintf("/%s/retry/", *dashboard),
TargetURL: *targetURL,
ProxyPort: *proxyPort,
MaxCaptures: *maxCaptures,
DashboardPort: *dashboardPort,
}
}

View file

@ -233,7 +233,7 @@ const dashboardHTML = `
$scope.show = item => {
$scope.selectedItem.id = item.id;
$scope.selectedItem.status = item.status;
$http.get(<<.DashboardInfoPath>> + item.id).then(r => {
$http.get(window.location.href + '/info/' + item.id).then(r => {
$scope.selectedItem.request = r.data.request;
$scope.selectedItem.response = r.data.response;
$scope.selectedItem.curl = r.data.curl;
@ -247,7 +247,7 @@ const dashboardHTML = `
}
$scope.clearDashboard = () => {
$http.get(<<.DashboardClearPath>>)
$http.get(window.location.href + '/clear/')
.then(() => $scope.selectedItem = {});
}
@ -266,7 +266,7 @@ const dashboardHTML = `
}
$scope.retry = () => {
$http.get(<<.DashboardRetryPath>> + $scope.selectedItem.id)
$http.get(window.location.href + '/retry/' + $scope.selectedItem.id)
.then(() => $scope.show($scope.items[$scope.items.length - 1]));
}
@ -279,7 +279,7 @@ const dashboardHTML = `
$scope.selectedItem[key] = data.replace(body, prettyBody);
}
const evt = new EventSource(<<.DashboardConnPath>>);
const evt = new EventSource(window.location.href + '/conn/');
evt.addEventListener('captures', e => {
$scope.items = JSON.parse(e.data);
if (!$scope.items.find(i => i.id == $scope.selectedItem.id)) {

2
go.mod
View file

@ -1,3 +1,5 @@
module github.com/ofabricio/capture
require github.com/ofabricio/curl v0.1.0
go 1.13

38
main.go
View file

@ -27,27 +27,33 @@ const StatusInternalProxyError = 999
func main() {
config := ReadConfig()
proxyURL := "http://localhost:" + config.ProxyPort
fmt.Printf("\nListening on %s", proxyURL)
fmt.Printf("\n %s%s\n\n", proxyURL, config.DashboardPath)
fmt.Println(http.ListenAndServe(":"+config.ProxyPort, NewCaptureHandler(config)))
}
func NewCaptureHandler(config Config) http.Handler {
fmt.Printf("\nListening on http://localhost:%s", config.ProxyPort)
fmt.Printf("\nDashboard on http://localhost:%s", config.DashboardPort)
fmt.Println()
srv := NewCaptureService(config.MaxCaptures)
handler := NewRecorderHandler(srv, NewPluginHandler(NewProxyHandler(config.TargetURL)))
go func() {
fmt.Println(http.ListenAndServe(":"+config.DashboardPort, NewDashboardHandler(handler, srv, config)))
os.Exit(1)
}()
fmt.Println(http.ListenAndServe(":"+config.ProxyPort, NewCaptureHandler(handler)))
}
func NewCaptureHandler(h http.HandlerFunc) http.Handler {
router := http.NewServeMux()
router.HandleFunc(config.DashboardPath, NewDashboardHTMLHandler(config))
router.HandleFunc(config.DashboardConnPath, NewDashboardConnHandler(srv))
router.HandleFunc(config.DashboardInfoPath, NewDashboardInfoHandler(srv))
router.HandleFunc(config.DashboardClearPath, NewDashboardClearHandler(srv))
router.HandleFunc(config.DashboardRetryPath, NewDashboardRetryHandler(srv, handler))
router.HandleFunc("/", handler)
router.HandleFunc("/", h)
return router
}
func NewDashboardHandler(h http.HandlerFunc, srv *CaptureService, config Config) http.Handler {
router := http.NewServeMux()
router.HandleFunc("/", NewDashboardHTMLHandler(config))
router.HandleFunc("/conn/", NewDashboardConnHandler(srv))
router.HandleFunc("/info/", NewDashboardInfoHandler(srv))
router.HandleFunc("/clear/", NewDashboardClearHandler(srv))
router.HandleFunc("/retry/", NewDashboardRetryHandler(srv, h))
return router
}