move socket code to the main file
This commit is contained in:
parent
477af61528
commit
e6c9e34f85
21
capture.go
Normal file
21
capture.go
Normal file
|
@ -0,0 +1,21 @@
|
|||
package main
|
||||
|
||||
type Capture struct {
|
||||
Url string `json:"url"`
|
||||
Method string `json:"method"`
|
||||
Status int `json:"status"`
|
||||
Request string `json:"request"`
|
||||
Response string `json:"response"`
|
||||
}
|
||||
|
||||
type Captures struct {
|
||||
items []Capture
|
||||
max int
|
||||
}
|
||||
|
||||
func (c *Captures) Add(capture Capture) {
|
||||
c.items = append([]Capture{capture}, c.items...)
|
||||
if len(c.items) > c.max {
|
||||
c.items = c.items[:len(c.items)-1]
|
||||
}
|
||||
}
|
49
main.go
49
main.go
|
@ -7,22 +7,23 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
|
||||
"github.com/googollee/go-socket.io"
|
||||
)
|
||||
|
||||
type Capture map[string]interface{}
|
||||
|
||||
var captures []Capture
|
||||
var maxCaptures int
|
||||
var captures Captures
|
||||
var socket socketio.Socket
|
||||
|
||||
type Transport struct {
|
||||
http.RoundTripper
|
||||
}
|
||||
|
||||
func main() {
|
||||
targetURL, proxyPort, dashboard, maxCaptrs := parseFlags()
|
||||
maxCaptures = maxCaptrs
|
||||
targetURL, proxyPort, dashboard, maxCaptures := parseFlags()
|
||||
captures.max = maxCaptures
|
||||
|
||||
proxy := httputil.NewSingleHostReverseProxy(targetURL)
|
||||
proxy.Transport = Transport{http.DefaultTransport}
|
||||
|
@ -43,6 +44,21 @@ func getProxyHandler(handler http.Handler) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func getSocketHandler() http.Handler {
|
||||
server, err := socketio.NewServer(nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
server.On("connection", func(so socketio.Socket) {
|
||||
socket = so
|
||||
emit()
|
||||
})
|
||||
server.On("error", func(so socketio.Socket, err error) {
|
||||
log.Println("socket error:", err)
|
||||
})
|
||||
return server
|
||||
}
|
||||
|
||||
func (t Transport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
reqDump, err := httputil.DumpRequest(req, true)
|
||||
if err != nil {
|
||||
|
@ -59,15 +75,13 @@ func (t Transport) RoundTrip(req *http.Request) (*http.Response, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
capture := Capture{
|
||||
"url": req.URL.Path,
|
||||
"method": req.Method,
|
||||
"status": res.StatusCode,
|
||||
"request": string(reqDump),
|
||||
"response": string(resDump),
|
||||
capture := Capture{req.URL.Path, req.Method, res.StatusCode,
|
||||
string(reqDump),
|
||||
string(resDump),
|
||||
}
|
||||
|
||||
save(capture)
|
||||
captures.Add(capture)
|
||||
emit()
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
@ -83,10 +97,9 @@ func DumpResponse(res *http.Response) ([]byte, error) {
|
|||
return resDump, err
|
||||
}
|
||||
|
||||
func save(capture Capture) {
|
||||
captures = append([]Capture{capture}, captures...)
|
||||
if len(captures) > maxCaptures {
|
||||
captures = captures[:len(captures)-1]
|
||||
func emit() {
|
||||
if socket == nil {
|
||||
return
|
||||
}
|
||||
emit(captures)
|
||||
socket.Emit("captures", captures.items)
|
||||
}
|
||||
|
|
32
socket.go
32
socket.go
|
@ -1,32 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/googollee/go-socket.io"
|
||||
)
|
||||
|
||||
var socket socketio.Socket
|
||||
|
||||
func getSocketHandler() http.Handler {
|
||||
server, err := socketio.NewServer(nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
server.On("connection", func(so socketio.Socket) {
|
||||
socket = so
|
||||
emit(captures)
|
||||
})
|
||||
server.On("error", func(so socketio.Socket, err error) {
|
||||
log.Println("socket error:", err)
|
||||
})
|
||||
return server
|
||||
}
|
||||
|
||||
func emit(captures []Capture) {
|
||||
if socket == nil {
|
||||
return
|
||||
}
|
||||
socket.Emit("captures", captures)
|
||||
}
|
Loading…
Reference in a new issue