Permalink
Browse files

Move toxics and io_chan to their own packages

  • Loading branch information...
xthexder committed Aug 4, 2015
1 parent 61d8bf4 commit 4b437c5296a468f7dd7e97faf1e0f1bd65c293b7
View
@@ -1,7 +1,7 @@
-FROM golang:1.4
+FROM golang:1.4
-ADD . /app
-RUN cd /app && GOPATH=/app/Godeps/_workspace go build -o toxiproxy
+ADD . /app/src/github.com/Shopify/toxiproxy
+RUN cd /app/src/github.com/Shopify/toxiproxy && GOPATH=/app/src/github.com/Shopify/toxiproxy/Godeps/_workspace:/app go build -o /app/toxiproxy ./cmd
EXPOSE 8474
ENTRYPOINT ["/app/toxiproxy"]
View
@@ -9,31 +9,31 @@ COMBINED_GOPATH=$(GODEP_PATH):$(ORIGINAL_PATH)
all: version deb linux darwin windows docker
deb: $(DEB)
-darwin: tmp/build/toxiproxy-darwin-amd64
+darwin: tmp/build/toxiproxy-darwin-amd64
linux: tmp/build/toxiproxy-linux-amd64
windows: tmp/build/toxiproxy-windows-amd64.exe
build:
- GOPATH=$(COMBINED_GOPATH) go build -o toxiproxy
+ GOPATH=$(COMBINED_GOPATH) go build -o toxiproxy ./cmd
clean:
rm tmp/build/*
rm *.deb
test:
- GOMAXPROCS=4 GOPATH=$(COMBINED_GOPATH) go test -v -race
+ GOMAXPROCS=4 GOPATH=$(COMBINED_GOPATH) go test -v -race ./...
version:
sed -i "s/Version = \"[^\"]*\"/Version = \"$(VERSION)\"/" version.go
tmp/build/toxiproxy-linux-amd64:
- GOOS=linux GOARCH=amd64 GOPATH=$(COMBINED_GOPATH) go build -o $(@)
+ GOOS=linux GOARCH=amd64 GOPATH=$(COMBINED_GOPATH) go build -o $(@) ./cmd
tmp/build/toxiproxy-darwin-amd64:
- GOOS=darwin GOARCH=amd64 GOPATH=$(COMBINED_GOPATH) go build -o $(@)
+ GOOS=darwin GOARCH=amd64 GOPATH=$(COMBINED_GOPATH) go build -o $(@) ./cmd
tmp/build/toxiproxy-windows-amd64.exe:
- GOOS=windows GOARCH=amd64 GOPATH=$(COMBINED_GOPATH) go build -o $(@)
+ GOOS=windows GOARCH=amd64 GOPATH=$(COMBINED_GOPATH) go build -o $(@) ./cmd
docker:
docker build --tag="shopify/toxiproxy:$(VERSION)" .
View
135 api.go
@@ -1,4 +1,4 @@
-package main
+package toxiproxy
import (
"encoding/json"
@@ -7,21 +7,22 @@ import (
"net"
"net/http"
+ "github.com/Shopify/toxiproxy/toxics"
"github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
)
-type server struct {
- collection *ProxyCollection
+type ApiServer struct {
+ Collection *ProxyCollection
}
-func NewServer() *server {
- return &server{
- collection: NewProxyCollection(),
+func NewServer() *ApiServer {
+ return &ApiServer{
+ Collection: NewProxyCollection(),
}
}
-func (server *server) Listen(host string, port string) {
+func (server *ApiServer) Listen(host string, port string) {
r := mux.NewRouter()
r.HandleFunc("/reset", server.ResetState).Methods("GET")
r.HandleFunc("/proxies", server.ProxyIndex).Methods("GET")
@@ -50,8 +51,8 @@ func (server *server) Listen(host string, port string) {
}
}
-func (server *server) ProxyIndex(response http.ResponseWriter, request *http.Request) {
- proxies := server.collection.Proxies()
+func (server *ApiServer) ProxyIndex(response http.ResponseWriter, request *http.Request) {
+ proxies := server.Collection.Proxies()
marshalData := make(map[string]interface{}, len(proxies))
for name, proxy := range proxies {
@@ -70,17 +71,17 @@ func (server *server) ProxyIndex(response http.ResponseWriter, request *http.Req
}
}
-func (server *server) ResetState(response http.ResponseWriter, request *http.Request) {
- proxies := server.collection.Proxies()
+func (server *ApiServer) ResetState(response http.ResponseWriter, request *http.Request) {
+ proxies := server.Collection.Proxies()
for _, proxy := range proxies {
err := proxy.Start()
if err != ErrProxyAlreadyStarted && apiError(response, err) {
return
}
- proxy.upToxics.ResetToxics()
- proxy.downToxics.ResetToxics()
+ proxy.UpToxics.ResetToxics()
+ proxy.DownToxics.ResetToxics()
}
response.WriteHeader(http.StatusNoContent)
@@ -90,7 +91,7 @@ func (server *server) ResetState(response http.ResponseWriter, request *http.Req
}
}
-func (server *server) ProxyCreate(response http.ResponseWriter, request *http.Request) {
+func (server *ApiServer) ProxyCreate(response http.ResponseWriter, request *http.Request) {
// Default fields to enable the proxy right away
input := Proxy{Enabled: true}
err := json.NewDecoder(request.Body).Decode(&input)
@@ -112,7 +113,7 @@ func (server *server) ProxyCreate(response http.ResponseWriter, request *http.Re
proxy.Listen = input.Listen
proxy.Upstream = input.Upstream
- err = server.collection.Add(proxy, input.Enabled)
+ err = server.Collection.Add(proxy, input.Enabled)
if apiError(response, err) {
return
}
@@ -130,10 +131,30 @@ func (server *server) ProxyCreate(response http.ResponseWriter, request *http.Re
}
}
-func (server *server) ProxyUpdate(response http.ResponseWriter, request *http.Request) {
+func (server *ApiServer) ProxyShow(response http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
- proxy, err := server.collection.Get(vars["proxy"])
+ proxy, err := server.Collection.Get(vars["proxy"])
+ if apiError(response, err) {
+ return
+ }
+
+ data, err := json.Marshal(proxyWithToxics(proxy))
+ if apiError(response, err) {
+ return
+ }
+
+ response.Header().Set("Content-Type", "application/json")
+ _, err = response.Write(data)
+ if err != nil {
+ logrus.Warn("ProxyShow: Failed to write response to client", err)
+ }
+}
+
+func (server *ApiServer) ProxyUpdate(response http.ResponseWriter, request *http.Request) {
+ vars := mux.Vars(request)
+
+ proxy, err := server.Collection.Get(vars["proxy"])
if apiError(response, err) {
return
}
@@ -162,10 +183,10 @@ func (server *server) ProxyUpdate(response http.ResponseWriter, request *http.Re
}
}
-func (server *server) ProxyDelete(response http.ResponseWriter, request *http.Request) {
+func (server *ApiServer) ProxyDelete(response http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
- err := server.collection.Remove(vars["proxy"])
+ err := server.Collection.Remove(vars["proxy"])
if apiError(response, err) {
return
}
@@ -177,40 +198,20 @@ func (server *server) ProxyDelete(response http.ResponseWriter, request *http.Re
}
}
-func (server *server) ProxyShow(response http.ResponseWriter, request *http.Request) {
- vars := mux.Vars(request)
-
- proxy, err := server.collection.Get(vars["proxy"])
- if apiError(response, err) {
- return
- }
-
- data, err := json.Marshal(proxyWithToxics(proxy))
- if apiError(response, err) {
- return
- }
-
- response.Header().Set("Content-Type", "application/json")
- _, err = response.Write(data)
- if err != nil {
- logrus.Warn("ProxyShow: Failed to write response to client", err)
- }
-}
-
-func (server *server) ToxicIndex(response http.ResponseWriter, request *http.Request) {
+func (server *ApiServer) ToxicIndex(response http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
- proxy, err := server.collection.Get(vars["proxy"])
+ proxy, err := server.Collection.Get(vars["proxy"])
if apiError(response, err) {
return
}
var data []byte
switch vars["stream"] {
case "upstream":
- data, err = json.Marshal(proxy.upToxics.GetToxicMap())
+ data, err = json.Marshal(proxy.UpToxics.GetToxicMap())
case "downstream":
- data, err = json.Marshal(proxy.downToxics.GetToxicMap())
+ data, err = json.Marshal(proxy.DownToxics.GetToxicMap())
default:
apiError(response, ErrInvalidStream)
return
@@ -226,20 +227,20 @@ func (server *server) ToxicIndex(response http.ResponseWriter, request *http.Req
}
}
-func (server *server) ToxicCreate(response http.ResponseWriter, request *http.Request) {
+func (server *ApiServer) ToxicCreate(response http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
- proxy, err := server.collection.Get(vars["proxy"])
+ proxy, err := server.Collection.Get(vars["proxy"])
if apiError(response, err) {
return
}
- var toxic Toxic
+ var toxic toxics.Toxic
switch vars["stream"] {
case "upstream":
- toxic, err = proxy.upToxics.AddToxicJson(request.Body)
+ toxic, err = proxy.UpToxics.AddToxicJson(request.Body)
case "downstream":
- toxic, err = proxy.downToxics.AddToxicJson(request.Body)
+ toxic, err = proxy.DownToxics.AddToxicJson(request.Body)
default:
apiError(response, ErrInvalidStream)
return
@@ -260,20 +261,20 @@ func (server *server) ToxicCreate(response http.ResponseWriter, request *http.Re
}
}
-func (server *server) ToxicShow(response http.ResponseWriter, request *http.Request) {
+func (server *ApiServer) ToxicShow(response http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
- proxy, err := server.collection.Get(vars["proxy"])
+ proxy, err := server.Collection.Get(vars["proxy"])
if apiError(response, err) {
return
}
- var toxic Toxic
+ var toxic toxics.Toxic
switch vars["stream"] {
case "upstream":
- toxic = proxy.upToxics.GetToxic(vars["toxic"])
+ toxic = proxy.UpToxics.GetToxic(vars["toxic"])
case "downstream":
- toxic = proxy.downToxics.GetToxic(vars["toxic"])
+ toxic = proxy.DownToxics.GetToxic(vars["toxic"])
default:
apiError(response, ErrInvalidStream)
return
@@ -295,20 +296,20 @@ func (server *server) ToxicShow(response http.ResponseWriter, request *http.Requ
}
}
-func (server *server) ToxicUpdate(response http.ResponseWriter, request *http.Request) {
+func (server *ApiServer) ToxicUpdate(response http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
- proxy, err := server.collection.Get(vars["proxy"])
+ proxy, err := server.Collection.Get(vars["proxy"])
if apiError(response, err) {
return
}
- var toxic Toxic
+ var toxic toxics.Toxic
switch vars["stream"] {
case "upstream":
- toxic, err = proxy.upToxics.UpdateToxicJson(vars["toxic"], request.Body)
+ toxic, err = proxy.UpToxics.UpdateToxicJson(vars["toxic"], request.Body)
case "downstream":
- toxic, err = proxy.downToxics.UpdateToxicJson(vars["toxic"], request.Body)
+ toxic, err = proxy.DownToxics.UpdateToxicJson(vars["toxic"], request.Body)
default:
apiError(response, ErrInvalidStream)
return
@@ -329,19 +330,19 @@ func (server *server) ToxicUpdate(response http.ResponseWriter, request *http.Re
}
}
-func (server *server) ToxicDelete(response http.ResponseWriter, request *http.Request) {
+func (server *ApiServer) ToxicDelete(response http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
- proxy, err := server.collection.Get(vars["proxy"])
+ proxy, err := server.Collection.Get(vars["proxy"])
if apiError(response, err) {
return
}
switch vars["stream"] {
case "upstream":
- err = proxy.upToxics.RemoveToxic(vars["toxic"])
+ err = proxy.UpToxics.RemoveToxic(vars["toxic"])
case "downstream":
- err = proxy.downToxics.RemoveToxic(vars["toxic"])
+ err = proxy.DownToxics.RemoveToxic(vars["toxic"])
default:
apiError(response, ErrInvalidStream)
return
@@ -357,7 +358,7 @@ func (server *server) ToxicDelete(response http.ResponseWriter, request *http.Re
}
}
-func (server *server) Version(response http.ResponseWriter, request *http.Request) {
+func (server *ApiServer) Version(response http.ResponseWriter, request *http.Request) {
response.Header().Set("Content-Type", "text/plain")
_, err := response.Write([]byte(Version))
if err != nil {
@@ -419,11 +420,11 @@ func apiError(resp http.ResponseWriter, err error) bool {
func proxyWithToxics(proxy *Proxy) (result struct {
*Proxy
- UpstreamToxics map[string]Toxic `json:"upstream_toxics"`
- DownstreamToxics map[string]Toxic `json:"downstream_toxics"`
+ UpstreamToxics map[string]toxics.Toxic `json:"upstream_toxics"`
+ DownstreamToxics map[string]toxics.Toxic `json:"downstream_toxics"`
}) {
result.Proxy = proxy
- result.UpstreamToxics = proxy.upToxics.GetToxicMap()
- result.DownstreamToxics = proxy.downToxics.GetToxicMap()
+ result.UpstreamToxics = proxy.UpToxics.GetToxicMap()
+ result.DownstreamToxics = proxy.DownToxics.GetToxicMap()
return
}
View
@@ -1,15 +1,16 @@
-package main
+package toxiproxy_test
import (
"io/ioutil"
"net/http"
"testing"
"time"
+ "github.com/Shopify/toxiproxy"
tclient "github.com/Shopify/toxiproxy/client"
)
-var testServer *server
+var testServer *toxiproxy.ApiServer
var client = tclient.NewClient("http://127.0.0.1:8475")
var testProxy = client.NewProxy(&tclient.Proxy{
@@ -23,15 +24,15 @@ func WithServer(t *testing.T, f func(string)) {
// Make sure only one server is running at a time. Apparently there's no clean
// way to shut it down between each test run.
if testServer == nil {
- testServer = NewServer()
+ testServer = toxiproxy.NewServer()
go testServer.Listen("localhost", "8475")
// Allow server to start. There's no clean way to know when it listens.
time.Sleep(50 * time.Millisecond)
}
defer func() {
- err := testServer.collection.Clear()
+ err := testServer.Collection.Clear()
if err != nil {
t.Error("Failed to clear collection", err)
}
@@ -539,7 +540,7 @@ func TestVersionEndpointReturnsVersion(t *testing.T) {
t.Fatal("Unable to read body from response")
}
- if string(body) != Version {
+ if string(body) != toxiproxy.Version {
t.Fatal("Expected to return Version from /version, got:", string(body))
}
})
Oops, something went wrong.

0 comments on commit 4b437c5

Please sign in to comment.