Reworking in module definition

This commit is contained in:
Héctor Hurtado
2019-10-08 12:02:46 +02:00
parent 73e05afee3
commit fe7d962581
3 changed files with 25 additions and 19 deletions
+21 -17
View File
@@ -1,35 +1,39 @@
package control package control
import ( import (
"log"
"net/http" "net/http"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/BBVA/kapow/internal/server/user"
) )
type ControlServer struct { func Run(bindAddr string) {
bindAddr string
ctrlMux *mux.Router r := mux.NewRouter()
r.HandleFunc("/routes/{id}", removeRoute).
Methods("DELETE")
r.HandleFunc("/routes", listRoutes).
Methods("GET")
r.HandleFunc("/routes", addRoute).
Methods("POST")
log.Fatal(http.ListenAndServe(bindAddr, r))
} }
var Server *ControlServer func removeRoute(http.ResponseWriter, *http.Request) {
func (srv *ControlServer) Run(bindAddr string) {
Server = &ControlServer{bindAddr, 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")
}
func (srv *ControlServer) removeRoute(http.ResponseWriter, *http.Request) {
} }
func (srv *ControlServer) listRoutes(http.ResponseWriter, *http.Request) { func listRoutes(http.ResponseWriter, *http.Request) {
user.Routes.Snapshot()
} }
func (srv *ControlServer) addRoute(http.ResponseWriter, *http.Request) { func addRoute(http.ResponseWriter, *http.Request) {
user.Routes.Append(routeSpec)
} }
+1 -1
View File
@@ -4,6 +4,6 @@ import (
"testing" "testing"
) )
func TestNewControlServerFillsTheStructCorrectly(t *testing.T) { func Test(t *testing.T) {
} }
+3 -1
View File
@@ -4,6 +4,8 @@ import (
"fmt" "fmt"
"net/url" "net/url"
"strconv" "strconv"
"github.com/BBVA/kapow/internal/server/control"
) )
// StartServer Starts one instance of each server in a goroutine and remains listening on a channel for trace events generated by them // StartServer Starts one instance of each server in a goroutine and remains listening on a channel for trace events generated by them
@@ -24,7 +26,7 @@ func StartServer(bindAddr, certfile, keyfile string, interactive bool) error {
dataBindAddr := fmt.Sprintf("%s:%d", userBindAddr.Host, userPort+2) dataBindAddr := fmt.Sprintf("%s:%d", userBindAddr.Host, userPort+2)
fmt.Printf("User server bind address: %s, Control server bind address: %s, Data server bind address: %s", bindAddr, ctrlBindAddr, dataBindAddr) fmt.Printf("User server bind address: %s, Control server bind address: %s, Data server bind address: %s", bindAddr, ctrlBindAddr, dataBindAddr)
//go ctrlServer.Start(traceChannel) go control.Run(ctrlBindAddr)
//go dataServer.Start(traceChannel) //go dataServer.Start(traceChannel)
//go ctrlServer.Start(traceChannel) //go ctrlServer.Start(traceChannel)