diff --git a/go.mod b/go.mod index 299196f..74eb86e 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.12 require ( github.com/google/uuid v1.1.1 // indirect - github.com/gorilla/mux v1.7.3 // indirect + github.com/gorilla/mux v1.7.3 github.com/spf13/cobra v0.0.5 gopkg.in/h2non/gock.v1 v1.0.15 ) diff --git a/internal/server/control/control.go b/internal/server/control/control.go new file mode 100644 index 0000000..a9fd6ea --- /dev/null +++ b/internal/server/control/control.go @@ -0,0 +1,84 @@ +package control + +import ( + "errors" + "fmt" + "net/http" + + "github.com/gorilla/mux" +) + +type ControlServer struct { + bindAddr string + ctrlMux *mux.Router + traceChannel chan string + useTLS bool + certfile string + keyfile string +} + +var server *ControlServer + +func NewControlServer(bindAddr string, listenPort int, certfile, keyfile string) (*ControlServer, error) { + + if server == nil { + var useTLS bool + + if certfile == "" && keyfile != "" { + return nil, errors.New("No keyfile provided") + } else if certfile != "" && keyfile == "" { + return nil, errors.New("No certfile provided") + } else if certfile != "" && keyfile != "" { + useTLS = true + } else { + useTLS = false + } + + server = &ControlServer{ + bindAddr: fmt.Sprintf("%s:%d", bindAddr, listenPort), + useTLS: useTLS, + certfile: certfile, + keyfile: keyfile, + ctrlMux: mux.NewRouter(), + } + + server.ctrlMux.HandleFunc("/routes/{id}", server.removeRoute).Methods("DELETE") + server.ctrlMux.HandleFunc("/routes", server.listRoutes).Methods("GET") + server.ctrlMux.HandleFunc("/routes", server.addRoute).Methods("POST") + } + + return server, nil +} + +func (srv *ControlServer) Start(traceChannel chan string) { + + srv.traceChannel = traceChannel + + // Start the server + var err error + if srv.useTLS { + err = http.ListenAndServeTLS(srv.bindAddr, srv.certfile, srv.keyfile, srv.ctrlMux) + } else { + err = http.ListenAndServe(srv.bindAddr, srv.ctrlMux) + } + + srv.traceChannel <- err.Error() +} + +func (srv *ControlServer) removeRoute(http.ResponseWriter, *http.Request) { + var logRecord string + defer func() { srv.traceChannel <- logRecord }() + +} + +func (srv *ControlServer) listRoutes(http.ResponseWriter, *http.Request) { + var logRecord string + defer func() { srv.traceChannel <- logRecord }() + +} + +func (srv *ControlServer) addRoute(http.ResponseWriter, *http.Request) { + var logRecord string + defer func() { srv.traceChannel <- logRecord }() + +} diff --git a/server/control/control_test.go b/internal/server/control/control_test.go similarity index 70% rename from server/control/control_test.go rename to internal/server/control/control_test.go index dbd265b..e83b15f 100644 --- a/server/control/control_test.go +++ b/internal/server/control/control_test.go @@ -1,10 +1,16 @@ package control -import "testing" +import ( + "testing" +) -func TestNewControlServerFillsTheStruct(t *testing.T) { +func TestNewControlServerFillsTheStructCorrectly(t *testing.T) { - server := NewControlServer("0.0.0.0", 8080, "/certfile.pem", "/keyfile.pem") + server, err := NewControlServer("0.0.0.0", 8080, "/certfile.pem", "/keyfile.pem") + + if err != nil { + + } if server.bindAddr != "0.0.0.0:8080" { t.Errorf("BindAddress incorrectly composed. Expected: %s, got: %s", "0.0.0.0:8080", server.bindAddr) @@ -17,4 +23,5 @@ func TestNewControlServerFillsTheStruct(t *testing.T) { if server.keyfile != "/keyfile.pem" { t.Errorf("BindAddress incorrectly composed. Expected: %s, got: %s", "/keyfile.pem", server.keyfile) } + } diff --git a/server/control/control.go b/server/control/control.go deleted file mode 100644 index 7a12f27..0000000 --- a/server/control/control.go +++ /dev/null @@ -1,31 +0,0 @@ -package control - -import ( - "fmt" - "net/http" -) - -type ControlServer struct { - bindAddr string - mux *http.ServeMux - traceChannel chan string - certfile string - keyfile string -} - -var server *ControlServer - -func NewControlServer(bindAddr string, listenPort int, certfile, keyfile string) *ControlServer { - - if server == nil { - server = &ControlServer{bindAddr: fmt.Sprintf("%s:%d", bindAddr, listenPort), - certfile: certfile, - keyfile: keyfile} - } - - return server -} - -func (*ControlServer) Start(traceChannel chan string) { - -}