dashboard on different port, closes #1
This commit is contained in:
parent
b49eba8fe1
commit
760639465e
10
README.md
10
README.md
|
@ -16,13 +16,13 @@ For ready-to-use executables for *Windows*, *Linux* and *Mac*, see [Releases](ht
|
||||||
./capture -url=https://example.com/
|
./capture -url=https://example.com/
|
||||||
|
|
||||||
|
|
||||||
### Configurations
|
### Settings
|
||||||
|
|
||||||
| param | description |
|
| param | description |
|
||||||
|-----------------|-------------|
|
|-----------------|-------------|
|
||||||
| `-url` | **Required.** Set the url you want to proxy |
|
| `-url` | **Required.** Set the url you want to proxy |
|
||||||
| `-port` | Set the proxy port. Default: *9000* |
|
| `-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* |
|
| `-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
|
## Dashboard
|
||||||
|
|
||||||
To access the dashboard go to `http://localhost:9000/dashboard`
|
To access the dashboard go to `http://localhost:9001/`
|
||||||
|
|
||||||
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`
|
|
||||||
|
|
||||||
|
|
||||||
##### Preview
|
##### Preview
|
||||||
|
|
||||||
|
|
30
config.go
30
config.go
|
@ -2,39 +2,27 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config has all the configuration parsed from the command line
|
// Config has all the configuration parsed from the command line
|
||||||
type Config struct {
|
type Config struct {
|
||||||
TargetURL string `json:"targetURL"`
|
TargetURL string
|
||||||
ProxyPort string `json:"proxyPort"`
|
ProxyPort string
|
||||||
MaxCaptures int `json:"maxCaptures"`
|
DashboardPort string
|
||||||
|
MaxCaptures int
|
||||||
DashboardPath string `json:"dashboardPath"`
|
|
||||||
DashboardConnPath string `json:"dashboardConnPath"`
|
|
||||||
DashboardInfoPath string `json:"dashboardInfoPath"`
|
|
||||||
DashboardClearPath string `json:"dashboardClearPath"`
|
|
||||||
DashboardRetryPath string `json:"dashboardRetryPath"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadConfig reads the arguments from the command line
|
// ReadConfig reads the arguments from the command line
|
||||||
func ReadConfig() Config {
|
func ReadConfig() Config {
|
||||||
targetURL := flag.String("url", "https://jsonplaceholder.typicode.com", "Required. Set the base url you want to capture")
|
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")
|
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")
|
maxCaptures := flag.Int("max-captures", 16, "Set the max number of captures to show in the dashboard")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
return Config{
|
return Config{
|
||||||
TargetURL: *targetURL,
|
TargetURL: *targetURL,
|
||||||
ProxyPort: *proxyPort,
|
ProxyPort: *proxyPort,
|
||||||
MaxCaptures: *maxCaptures,
|
MaxCaptures: *maxCaptures,
|
||||||
|
DashboardPort: *dashboardPort,
|
||||||
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),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -233,7 +233,7 @@ const dashboardHTML = `
|
||||||
$scope.show = item => {
|
$scope.show = item => {
|
||||||
$scope.selectedItem.id = item.id;
|
$scope.selectedItem.id = item.id;
|
||||||
$scope.selectedItem.status = item.status;
|
$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.request = r.data.request;
|
||||||
$scope.selectedItem.response = r.data.response;
|
$scope.selectedItem.response = r.data.response;
|
||||||
$scope.selectedItem.curl = r.data.curl;
|
$scope.selectedItem.curl = r.data.curl;
|
||||||
|
@ -247,7 +247,7 @@ const dashboardHTML = `
|
||||||
}
|
}
|
||||||
|
|
||||||
$scope.clearDashboard = () => {
|
$scope.clearDashboard = () => {
|
||||||
$http.get(<<.DashboardClearPath>>)
|
$http.get(window.location.href + '/clear/')
|
||||||
.then(() => $scope.selectedItem = {});
|
.then(() => $scope.selectedItem = {});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -266,7 +266,7 @@ const dashboardHTML = `
|
||||||
}
|
}
|
||||||
|
|
||||||
$scope.retry = () => {
|
$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]));
|
.then(() => $scope.show($scope.items[$scope.items.length - 1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -279,7 +279,7 @@ const dashboardHTML = `
|
||||||
$scope.selectedItem[key] = data.replace(body, prettyBody);
|
$scope.selectedItem[key] = data.replace(body, prettyBody);
|
||||||
}
|
}
|
||||||
|
|
||||||
const evt = new EventSource(<<.DashboardConnPath>>);
|
const evt = new EventSource(window.location.href + '/conn/');
|
||||||
evt.addEventListener('captures', e => {
|
evt.addEventListener('captures', e => {
|
||||||
$scope.items = JSON.parse(e.data);
|
$scope.items = JSON.parse(e.data);
|
||||||
if (!$scope.items.find(i => i.id == $scope.selectedItem.id)) {
|
if (!$scope.items.find(i => i.id == $scope.selectedItem.id)) {
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -1,3 +1,5 @@
|
||||||
module github.com/ofabricio/capture
|
module github.com/ofabricio/capture
|
||||||
|
|
||||||
require github.com/ofabricio/curl v0.1.0
|
require github.com/ofabricio/curl v0.1.0
|
||||||
|
|
||||||
|
go 1.13
|
||||||
|
|
38
main.go
38
main.go
|
@ -27,27 +27,33 @@ const StatusInternalProxyError = 999
|
||||||
func main() {
|
func main() {
|
||||||
config := ReadConfig()
|
config := ReadConfig()
|
||||||
|
|
||||||
proxyURL := "http://localhost:" + config.ProxyPort
|
fmt.Printf("\nListening on http://localhost:%s", config.ProxyPort)
|
||||||
|
fmt.Printf("\nDashboard on http://localhost:%s", config.DashboardPort)
|
||||||
fmt.Printf("\nListening on %s", proxyURL)
|
fmt.Println()
|
||||||
fmt.Printf("\n %s%s\n\n", proxyURL, config.DashboardPath)
|
|
||||||
|
|
||||||
fmt.Println(http.ListenAndServe(":"+config.ProxyPort, NewCaptureHandler(config)))
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewCaptureHandler(config Config) http.Handler {
|
|
||||||
|
|
||||||
srv := NewCaptureService(config.MaxCaptures)
|
srv := NewCaptureService(config.MaxCaptures)
|
||||||
|
|
||||||
handler := NewRecorderHandler(srv, NewPluginHandler(NewProxyHandler(config.TargetURL)))
|
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 := http.NewServeMux()
|
||||||
router.HandleFunc(config.DashboardPath, NewDashboardHTMLHandler(config))
|
router.HandleFunc("/", h)
|
||||||
router.HandleFunc(config.DashboardConnPath, NewDashboardConnHandler(srv))
|
return router
|
||||||
router.HandleFunc(config.DashboardInfoPath, NewDashboardInfoHandler(srv))
|
}
|
||||||
router.HandleFunc(config.DashboardClearPath, NewDashboardClearHandler(srv))
|
|
||||||
router.HandleFunc(config.DashboardRetryPath, NewDashboardRetryHandler(srv, handler))
|
func NewDashboardHandler(h http.HandlerFunc, srv *CaptureService, config Config) http.Handler {
|
||||||
router.HandleFunc("/", 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
|
return router
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue