remove the need of ItemPath

This commit is contained in:
Fabricio 2018-09-16 11:18:39 -03:00
parent aa70e1b21c
commit cb16e22b8b
4 changed files with 58 additions and 41 deletions

31
args.go
View file

@ -1,12 +1,18 @@
package main
import "flag"
import (
"flag"
"fmt"
)
type Args struct {
TargetURL string `json:"targetURL"`
ProxyPort string `json:"proxyPort"`
Dashboard string `json:"dashboard"`
MaxCaptures int `json:"maxCaptures"`
TargetURL string `json:"targetURL"`
ProxyPort string `json:"proxyPort"`
MaxCaptures int `json:"maxCaptures"`
Dashboard string `json:"dashboard"`
DashboardPath string `json:"dashboardPath"`
DashboardClearPath string `json:"dashboardClearPath"`
DashboardItemInfoPath string `json:"dashboardItemInfoPath"`
}
func ParseArgs() Args {
@ -15,5 +21,18 @@ func ParseArgs() Args {
dashboard := flag.String("dashboard", "dashboard", "Set the dashboard name")
maxCaptures := flag.Int("max-captures", 16, "Set the max number of captures to show in the dashboard")
flag.Parse()
return Args{*targetURL, *proxyPort, *dashboard, *maxCaptures}
dashboardPath := fmt.Sprintf("/%s/", *dashboard)
dashboardClearPath := fmt.Sprintf("/%s/clear/", *dashboard)
dashboardItemInfoPath := fmt.Sprintf("/%s/items/", *dashboard)
return Args{
TargetURL: *targetURL,
ProxyPort: *proxyPort,
MaxCaptures: *maxCaptures,
Dashboard: *dashboard,
DashboardPath: dashboardPath,
DashboardClearPath: dashboardClearPath,
DashboardItemInfoPath: dashboardItemInfoPath,
}
}

View file

@ -1,23 +1,19 @@
package main
import "fmt"
type Capture struct {
ID int `json:"id"`
Path string `json:"path"`
Method string `json:"method"`
Status int `json:"status"`
InfoPath string `json:"infoPath"`
Request string `json:"request"`
Response string `json:"response"`
}
type CaptureMetadata struct {
ID int `json:"id"`
Path string `json:"path"`
Method string `json:"method"`
Status int `json:"status"`
InfoPath string `json:"infoPath"`
ID int `json:"id"`
Path string `json:"path"`
Method string `json:"method"`
Status int `json:"status"`
}
type Captures []Capture
@ -36,11 +32,10 @@ func (items *Captures) MetadataOnly() []CaptureMetadata {
refs := make([]CaptureMetadata, len(*items))
for i, item := range *items {
refs[i] = CaptureMetadata{
ID: item.ID,
Path: item.Path,
Method: item.Method,
Status: item.Status,
InfoPath: fmt.Sprintf("%s%d", item.InfoPath, i),
ID: item.ID,
Path: item.Path,
Method: item.Method,
Status: item.Status,
}
}
return refs

View file

@ -217,7 +217,8 @@ const dashboardHTML = `
$scope.show = item => {
$scope.path = item.path;
$scope.selectedId = item.id;
$http.get(item.infoPath).then(r => {
let path = $scope.config.dashboardItemInfoPath + item.id;
$http.get(path).then(r => {
$scope.request = r.data.request;
$scope.response = r.data.response;
});
@ -233,9 +234,11 @@ const dashboardHTML = `
}
$scope.clearDashboard = () => {
$http.get('/' + $scope.config.dashboard + '/clear').then(() => {
$scope.request = $scope.response = null;
});
$http.get($scope.config.dashboardClearPath).then(clearRequestAndResponse);
}
function clearRequestAndResponse() {
$scope.request = $scope.response = null;
}
$scope.canPrettifyBody = name => {
@ -256,6 +259,7 @@ const dashboardHTML = `
let socket = io();
socket.on('connect', () => {
clearRequestAndResponse();
socket.off('config');
socket.off('captures');
socket.on('config', args => {

35
main.go
View file

@ -18,9 +18,8 @@ import (
type transport struct {
http.RoundTripper
itemInfoPath string
maxItems int
currItemID int
maxItems int
currItemID int
}
var captures Captures
@ -30,23 +29,19 @@ var dashboardSocket socketio.Socket
func main() {
args := ParseArgs()
proxyHost := fmt.Sprintf("http://localhost:%s", args.ProxyPort)
dashboardPath := fmt.Sprintf("/%s/", args.Dashboard)
dashboardClearPath := fmt.Sprintf("/%s/clear/", args.Dashboard)
dashboardItemInfoPath := fmt.Sprintf("/%s/items/", args.Dashboard)
transp := &transport{
RoundTripper: http.DefaultTransport,
itemInfoPath: dashboardItemInfoPath,
maxItems: args.MaxCaptures,
currItemID: 0,
}
http.Handle("/", getProxyHandler(args.TargetURL, transp))
http.Handle("/socket.io/", getDashboardSocketHandler(args))
http.Handle(dashboardPath, getDashboardHandler())
http.Handle(dashboardClearPath, getDashboardClearHandler())
http.Handle(dashboardItemInfoPath, getDashboardItemInfoHandler())
http.Handle(args.DashboardPath, getDashboardHandler())
http.Handle(args.DashboardClearPath, getDashboardClearHandler())
http.Handle(args.DashboardItemInfoPath, getDashboardItemInfoHandler())
proxyHost := fmt.Sprintf("http://localhost:%s", args.ProxyPort)
fmt.Printf("\nListening on %s", proxyHost)
fmt.Printf("\n %s/%s\n\n", proxyHost, args.Dashboard)
@ -87,11 +82,16 @@ func getDashboardHandler() http.Handler {
func getDashboardItemInfoHandler() http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
id := req.URL.Path[strings.LastIndex(req.URL.Path, "/")+1:]
i, _ := strconv.Atoi(id)
json, _ := json.Marshal(captures[i])
res.Header().Add("Content-Type", "application/json")
res.Write([]byte(json))
idStr := req.URL.Path[strings.LastIndex(req.URL.Path, "/")+1:]
idInt, _ := strconv.Atoi(idStr)
for _, c := range captures {
if c.ID == idInt {
json, _ := json.Marshal(c)
res.Header().Add("Content-Type", "application/json")
res.Write([]byte(json))
return
}
}
})
}
@ -127,7 +127,6 @@ func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
Path: req.URL.Path,
Method: req.Method,
Status: res.StatusCode,
InfoPath: t.itemInfoPath,
Request: string(reqDump),
Response: string(resDump),
}