capture/capture.go

121 lines
2.3 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
mux sync.Mutex
maxItems int
2018-11-25 18:10:10 +01:00
// signals any change in "items"
Updated chan struct{}
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,
2018-11-25 18:10:10 +01:00
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) {
c.mux.Lock()
defer c.mux.Unlock()
capture.ID = newID()
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
}
2018-11-25 18:10:10 +01:00
c.signalsItemsChange()
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 {
c.mux.Lock()
defer c.mux.Unlock()
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() {
c.mux.Lock()
defer c.mux.Unlock()
c.items = nil
2018-11-25 18:10:10 +01:00
c.signalsItemsChange()
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-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 {
c.mux.Lock()
defer c.mux.Unlock()
metadatas := make([]CaptureMetadata, len(c.items))
for i, capture := range c.items {
metadatas[i] = capture.Metadata()
}
return metadatas
}
2018-11-22 22:45:20 +01:00
func newID() int {
captureID++
return captureID
2017-11-21 22:36:40 +01:00
}
2018-11-25 18:10:10 +01:00
func (c *CaptureList) signalsItemsChange() {
select {
case c.Updated <- struct{}{}:
default:
}
}