Moved to internal

This commit is contained in:
Héctor Hurtado
2019-10-08 08:54:07 +02:00
parent 9266f48c7e
commit 56db1ce2ac
4 changed files with 95 additions and 35 deletions
+1 -1
View File
@@ -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
)
+84
View File
@@ -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 }()
}
@@ -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)
}
}
-31
View File
@@ -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) {
}