Files
hetty/pkg/reqlog/reqlog.go

52 lines
930 B
Go
Raw Normal View History

2019-12-01 14:07:12 +01:00
package reqlog
import (
"net/http"
"sync"
)
type Request struct {
Request http.Request
Body []byte
2019-12-01 14:07:12 +01:00
}
type response struct {
res http.Response
body []byte
}
type RequestLogStore struct {
reqStore []Request
2019-12-01 14:07:12 +01:00
resStore []response
reqMu sync.Mutex
resMu sync.Mutex
}
func NewRequestLogStore() RequestLogStore {
return RequestLogStore{
reqStore: make([]Request, 0),
2019-12-01 14:07:12 +01:00
resStore: make([]response, 0),
}
}
func (store *RequestLogStore) AddRequest(req http.Request, body []byte) {
store.reqMu.Lock()
defer store.reqMu.Unlock()
store.reqStore = append(store.reqStore, Request{req, body})
}
func (store *RequestLogStore) Requests() []Request {
store.reqMu.Lock()
defer store.reqMu.Unlock()
2019-12-01 14:07:12 +01:00
return store.reqStore
2019-12-01 14:07:12 +01:00
}
func (store *RequestLogStore) AddResponse(res http.Response, body []byte) {
store.resMu.Lock()
defer store.resMu.Unlock()
2019-12-01 14:07:12 +01:00
store.resStore = append(store.resStore, response{res, body})
2019-12-01 14:07:12 +01:00
}