capture/capture.go

91 lines
1.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 (
"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-07-27 00:49:12 +02:00
ID int `json:"id"`
2018-07-21 19:50:53 +02:00
Path string `json:"path"`
2017-11-21 22:36:40 +01:00
Method string `json:"method"`
Status int `json:"status"`
Request string `json:"request"`
Response string `json:"response"`
}
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-22 22:45:20 +01:00
func (c *Capture) Metadata() CaptureMetadata {
return CaptureMetadata{
ID: c.ID,
Path: c.Path,
Method: c.Method,
Status: c.Status,
}
}
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
}