capture/capture.go

123 lines
2.5 KiB
Go
Raw Normal View History

2017-11-21 22:36:40 +01:00
package main
2018-11-22 22:45:20 +01:00
import (
2018-11-24 15:24:16 +01:00
"net/http"
2018-11-22 22:45:20 +01:00
"strconv"
"sync"
)
var captureID int
var captures CaptureList
2018-12-01 23:59:33 +01:00
// CaptureList stores all captures
2018-11-22 22:45:20 +01:00
type CaptureList struct {
items []Capture
2018-12-03 23:42:53 +01:00
mu sync.RWMutex
2018-11-22 22:45:20 +01:00
maxItems int
updated chan struct{} // signals any change in "items"
2018-11-22 22:45:20 +01:00
}
2018-12-01 23:59:33 +01:00
// Capture saves our traffic data
2017-11-21 22:36:40 +01:00
type Capture struct {
2018-11-24 15:24:16 +01:00
ID int
Req *http.Request
Res *http.Response
2017-11-21 22:36:40 +01:00
}
2018-12-01 23:59:33 +01:00
// CaptureMetadata is the data for each list item in the dashboard
2018-08-04 01:53:54 +02:00
type CaptureMetadata struct {
2018-09-16 16:18:39 +02:00
ID int `json:"id"`
Path string `json:"path"`
Method string `json:"method"`
Status int `json:"status"`
2018-07-21 19:50:53 +02:00
}
2018-12-01 23:59:33 +01:00
// CaptureDump saves all the dumps shown in the dashboard
2018-11-24 15:24:16 +01:00
type CaptureDump struct {
Request string `json:"request"`
Response string `json:"response"`
2018-11-24 18:51:33 +01:00
Curl string `json:"curl"`
2018-11-24 15:24:16 +01:00
}
2018-12-01 23:59:33 +01:00
// Metadata returns the metadada of a capture
2018-11-22 22:45:20 +01:00
func (c *Capture) Metadata() CaptureMetadata {
return CaptureMetadata{
ID: c.ID,
2018-11-24 15:24:16 +01:00
Path: c.Req.URL.Path,
Method: c.Req.Method,
Status: c.Res.StatusCode,
2018-11-22 22:45:20 +01:00
}
}
2018-07-21 19:50:53 +02:00
2018-12-01 23:59:33 +01:00
// NewCaptureList creates a new list of captures
2018-11-25 18:10:10 +01:00
func NewCaptureList(maxItems int) *CaptureList {
2018-11-22 22:45:20 +01:00
return &CaptureList{
maxItems: maxItems,
updated: make(chan struct{}),
2018-11-22 22:45:20 +01:00
}
2018-08-04 01:53:54 +02:00
}
2018-12-01 23:59:33 +01:00
// Insert adds a new capture
2018-11-22 22:45:20 +01:00
func (c *CaptureList) Insert(capture Capture) {
2018-12-03 23:42:53 +01:00
c.mu.Lock()
defer c.mu.Unlock()
captureID++
capture.ID = captureID
2018-11-22 22:45:20 +01:00
c.items = append(c.items, capture)
if len(c.items) > c.maxItems {
c.items = c.items[1:]
2018-07-21 19:50:53 +02:00
}
c.signalsChange()
2017-11-21 22:36:40 +01:00
}
2018-12-01 23:59:33 +01:00
// Find finds a capture by its id
2018-11-22 22:45:20 +01:00
func (c *CaptureList) Find(captureID string) *Capture {
2018-12-03 23:42:53 +01:00
c.mu.RLock()
defer c.mu.RUnlock()
2018-11-22 22:45:20 +01:00
idInt, _ := strconv.Atoi(captureID)
for _, c := range c.items {
if c.ID == idInt {
return &c
2018-07-21 19:50:53 +02:00
}
2017-11-21 22:36:40 +01:00
}
2018-11-22 22:45:20 +01:00
return nil
}
2018-12-01 23:59:33 +01:00
// RemoveAll removes all the captures
2018-11-22 22:45:20 +01:00
func (c *CaptureList) RemoveAll() {
2018-12-03 23:42:53 +01:00
c.mu.Lock()
defer c.mu.Unlock()
2018-11-22 22:45:20 +01:00
c.items = nil
c.signalsChange()
2018-11-22 22:45:20 +01:00
}
2018-12-01 23:59:33 +01:00
// Items returns all the captures
2018-11-25 18:10:10 +01:00
func (c *CaptureList) Items() []Capture {
2018-12-03 23:42:53 +01:00
c.mu.RLock()
defer c.mu.RUnlock()
2018-11-22 22:45:20 +01:00
return c.items
}
2018-12-01 23:59:33 +01:00
// ItemsAsMetadata returns all the captures as metadata
2018-11-25 18:10:10 +01:00
func (c *CaptureList) ItemsAsMetadata() []CaptureMetadata {
2018-12-03 23:42:53 +01:00
c.mu.RLock()
defer c.mu.RUnlock()
2018-11-25 18:10:10 +01:00
metadatas := make([]CaptureMetadata, len(c.items))
for i, capture := range c.items {
metadatas[i] = capture.Metadata()
}
return metadatas
}
func (c *CaptureList) signalsChange() {
close(c.updated)
c.updated = make(chan struct{})
}
// Updated signals any change in the list
func (c *CaptureList) Updated() <-chan struct{} {
2018-12-03 23:42:53 +01:00
c.mu.RLock()
defer c.mu.RUnlock()
return c.updated
2018-11-25 18:10:10 +01:00
}