From 665b4579cb29694915ba4419267bafba43cf3c0e Mon Sep 17 00:00:00 2001 From: Fabricio Date: Fri, 3 Aug 2018 20:53:54 -0300 Subject: [PATCH] better responsibilities --- args.go | 10 ++-- capture.go | 37 ++++++++------- dashboard.go | 8 ++-- main.go | 127 ++++++++++++++++++++++++++------------------------- 4 files changed, 93 insertions(+), 89 deletions(-) diff --git a/args.go b/args.go index a519a81..2e9d684 100644 --- a/args.go +++ b/args.go @@ -6,18 +6,18 @@ import ( ) type Args struct { - url *url.URL - port string + targetURL *url.URL + proxyPort string dashboard string maxCaptures int } -func (a *Args) Parse() Args { - proxyURL := flag.String("url", "https://jsonplaceholder.typicode.com", "Required. Set the base url you want to capture") +func ParseArgs() Args { + 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") maxCaptures := flag.Int("max-captures", 16, "Set the max number of captures to show in the dashboard") flag.Parse() - url, _ := url.Parse(*proxyURL) + url, _ := url.Parse(*targetURL) return Args{url, *proxyPort, *dashboard, *maxCaptures} } diff --git a/capture.go b/capture.go index 3653b37..a72d380 100644 --- a/capture.go +++ b/capture.go @@ -1,43 +1,46 @@ package main -import "strconv" +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 CaptureRef struct { - ID int `json:"id"` - Path string `json:"path"` - Method string `json:"method"` - Status int `json:"status"` - ItemUrl string `json:"itemUrl"` +type CaptureMetadata struct { + ID int `json:"id"` + Path string `json:"path"` + Method string `json:"method"` + Status int `json:"status"` + InfoPath string `json:"infoPath"` } type Captures []Capture func (items *Captures) Add(capture Capture) { *items = append(*items, capture) - size := len(*items) - if size > args.maxCaptures { +} + +func (items *Captures) RemoveLastAfterReaching(maxItems int) { + if len(*items) > maxItems { *items = (*items)[1:] } } -func (items *Captures) ToReferences(itemBaseUrl string) []CaptureRef { - refs := make([]CaptureRef, len(*items)) +func (items *Captures) MetadataOnly() []CaptureMetadata { + refs := make([]CaptureMetadata, len(*items)) for i, item := range *items { - refs[i] = CaptureRef{ - ID: item.ID, - Path: item.Path, - Method: item.Method, - Status: item.Status, - ItemUrl: itemBaseUrl + strconv.Itoa(i), + refs[i] = CaptureMetadata{ + ID: item.ID, + Path: item.Path, + Method: item.Method, + Status: item.Status, + InfoPath: fmt.Sprintf("%s%d", item.InfoPath, i), } } return refs diff --git a/dashboard.go b/dashboard.go index f84d695..5843fca 100644 --- a/dashboard.go +++ b/dashboard.go @@ -6,8 +6,8 @@ const dashboardHTML = ` - - + + Dashboard