| 1 |
package server |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/http" |
| 5 |
"net/url" |
| 6 |
"time" |
| 7 |
) |
| 8 |
|
| 9 |
type Call struct { |
| 10 |
URL *url.URL |
| 11 |
Method string |
| 12 |
Status int |
| 13 |
|
| 14 |
Time time.Time |
| 15 |
Duration time.Duration |
| 16 |
|
| 17 |
RequestHeader http.Header |
| 18 |
RequestBody []byte |
| 19 |
|
| 20 |
ResponseHeader http.Header |
| 21 |
ResponseBody []byte |
| 22 |
} |
| 23 |
|
| 24 |
type callWatcher struct { |
| 25 |
paths map[string]struct{} |
| 26 |
callFn func(Call) |
| 27 |
} |
| 28 |
|
| 29 |
func newCallWatcher(fn func(Call), paths ...string) callWatcher { |
| 30 |
pathMap := make(map[string]struct{}, len(paths)) |
| 31 |
|
| 32 |
for _, path := range paths { |
| 33 |
pathMap[path] = struct{}{} |
| 34 |
} |
| 35 |
|
| 36 |
return callWatcher{ |
| 37 |
paths: pathMap, |
| 38 |
callFn: fn, |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
func (watcher *callWatcher) isWatching(path string) bool { |
| 43 |
if len(watcher.paths) == 0 { |
| 44 |
return true |
| 45 |
} |
| 46 |
|
| 47 |
_, ok := watcher.paths[path] |
| 48 |
|
| 49 |
return ok |
| 50 |
} |
| 51 |
|
| 52 |
func (watcher *callWatcher) publish(call Call) { |
| 53 |
watcher.callFn(call) |
| 54 |
} |