From 77821a12474d8e9584d3e051ce405ea7b2d02976 Mon Sep 17 00:00:00 2001 From: Fabricio Date: Sat, 21 Jul 2018 14:50:53 -0300 Subject: [PATCH] two step item fetch --- args.go | 20 ++++++++--- capture.go | 38 ++++++++++++++++----- dashboard.go | 94 ++++++++++++++++++++++++++++++++++++---------------- main.go | 72 ++++++++++++++++++++++++++++------------ 4 files changed, 162 insertions(+), 62 deletions(-) diff --git a/args.go b/args.go index 503631d..f6342fb 100644 --- a/args.go +++ b/args.go @@ -6,12 +6,24 @@ import ( "strconv" ) -func parseFlags() (*url.URL, string, string, int) { - target := flag.String("url", "https://jsonplaceholder.typicode.com", "Required. Set the base url you want to capture") +type Args struct { + url *url.URL + port string + dashboard string + maxCaptures int +} + +var args Args + +func parseArgs() { + proxyURL := flag.String("url", "https://jsonplaceholder.typicode.com", "Required. Set the base url you want to capture") proxyPort := flag.Int("port", 9000, "Set the proxy port") 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() - targetURL, _ := url.Parse(*target) - return targetURL, strconv.Itoa(*proxyPort), *dashboard, *maxCaptures + args = Args{} + args.url, _ = url.Parse(*proxyURL) + args.port = strconv.Itoa(*proxyPort) + args.dashboard = *dashboard + args.maxCaptures = *maxCaptures } diff --git a/capture.go b/capture.go index 6d887d2..b5bee86 100644 --- a/capture.go +++ b/capture.go @@ -1,21 +1,43 @@ package main +import "strconv" + type Capture struct { - Url string `json:"url"` + Path string `json:"path"` Method string `json:"method"` Status int `json:"status"` Request string `json:"request"` Response string `json:"response"` } -type Captures struct { - items []Capture - max int +type CaptureRef struct { + ID int `json:"id"` + Path string `json:"path"` + Method string `json:"method"` + Status int `json:"status"` + ItemUrl string `json:"itemUrl"` } -func (c *Captures) Add(capture Capture) { - c.items = append([]Capture{capture}, c.items...) - if len(c.items) > c.max { - c.items = c.items[:len(c.items)-1] +type Captures []Capture + +func (items *Captures) Add(capture Capture) { + *items = append(*items, capture) + size := len(*items) + if size > args.maxCaptures { + *items = (*items)[1:] } } + +func (items *Captures) GetRefs(itemBaseUrl string) []CaptureRef { + refs := make([]CaptureRef, len(*items)) + for i, item := range *items { + refs[i] = CaptureRef{ + ID: i, + Path: item.Path, + Method: item.Method, + Status: item.Status, + ItemUrl: itemBaseUrl + strconv.Itoa(i), + } + } + return refs +} diff --git a/dashboard.go b/dashboard.go index 32cba0f..6b20e2c 100644 --- a/dashboard.go +++ b/dashboard.go @@ -21,6 +21,28 @@ const dashboardHTML = ` Dashboard @@ -96,10 +126,10 @@ const dashboardHTML = `
-
+
{{item.method}} - ‎{{item.url}}‎ + ‎{{item.path}}‎ {{item.status}}
@@ -107,7 +137,6 @@ const dashboardHTML = `
-
{{url}}
{{request}}
@@ -122,19 +151,26 @@ const dashboardHTML = `