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
|
|
|
|
|
|
|
|
type CaptureRepository interface {
|
|
|
|
Insert(capture Capture)
|
|
|
|
RemoveAll()
|
|
|
|
Find(captureID string) *Capture
|
|
|
|
FindAll() []Capture
|
|
|
|
}
|
|
|
|
|
|
|
|
type CaptureList struct {
|
|
|
|
items []Capture
|
|
|
|
mux sync.Mutex
|
|
|
|
maxItems int
|
|
|
|
}
|
|
|
|
|
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-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-11-24 15:24:16 +01:00
|
|
|
type CaptureDump struct {
|
|
|
|
Request string `json:"request"`
|
|
|
|
Response string `json:"response"`
|
|
|
|
}
|
|
|
|
|
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-11-22 22:45:20 +01:00
|
|
|
func NewCapturesRepository(maxItems int) CaptureRepository {
|
|
|
|
return &CaptureList{
|
|
|
|
maxItems: maxItems,
|
|
|
|
}
|
2018-08-04 01:53:54 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2017-11-21 22:36:40 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CaptureList) RemoveAll() {
|
|
|
|
c.mux.Lock()
|
|
|
|
defer c.mux.Unlock()
|
|
|
|
c.items = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CaptureList) FindAll() []Capture {
|
|
|
|
return c.items
|
|
|
|
}
|
|
|
|
|
|
|
|
func newID() int {
|
|
|
|
captureID++
|
|
|
|
return captureID
|
2017-11-21 22:36:40 +01:00
|
|
|
}
|