diff --git a/gopoc/poc1/.gitignore b/gopoc/poc1/.gitignore deleted file mode 100644 index 735e9fe..0000000 --- a/gopoc/poc1/.gitignore +++ /dev/null @@ -1 +0,0 @@ -kapow diff --git a/gopoc/poc1/Makefile b/gopoc/poc1/Makefile deleted file mode 100644 index 7747f5f..0000000 --- a/gopoc/poc1/Makefile +++ /dev/null @@ -1,2 +0,0 @@ -gopoc: kapow.go - CGO_ENABLED=0 go build diff --git a/gopoc/poc1/go.mod b/gopoc/poc1/go.mod deleted file mode 100644 index e04a14c..0000000 --- a/gopoc/poc1/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module github.com/BBVA/kapow - -go 1.12 diff --git a/gopoc/poc1/kapow.go b/gopoc/poc1/kapow.go deleted file mode 100644 index 9e6855c..0000000 --- a/gopoc/poc1/kapow.go +++ /dev/null @@ -1,37 +0,0 @@ -package main - -import ( - "fmt" - "net/http" - "os/exec" -) - -func main() { - go func() { - fmt.Println("Listening on port 8080") - http.ListenAndServe(":8080", &userServerHandler{}) - - }() - - http.ListenAndServe(":8081", &controlAPIHandler{}) - -} - -type userServerHandler struct { -} - -func (m *userServerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - out, err := exec.Command("date").Output() - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - } else { - w.Write(out) - } -} - -type controlAPIHandler struct { -} - -func (m *controlAPIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("Welcome to the control API!")) -} diff --git a/gopoc/poc2/.gitignore b/gopoc/poc2/.gitignore deleted file mode 100644 index 735e9fe..0000000 --- a/gopoc/poc2/.gitignore +++ /dev/null @@ -1 +0,0 @@ -kapow diff --git a/gopoc/poc2/kapow.go b/gopoc/poc2/kapow.go deleted file mode 100644 index bba1839..0000000 --- a/gopoc/poc2/kapow.go +++ /dev/null @@ -1,82 +0,0 @@ -package main - -import ( - "fmt" - "log" - "net/http" - "os/exec" -) - -const defaultMessage = "Hello Mr. %v\n" -const commandErrorMessage = "Error executing command: %v\n" -const commandExecutionMessage = "Command executed: %v\n" - -type serverSpec struct { - id, listenAddr string - routes *http.ServeMux -} - -var servers = []serverSpec{{id: "commandServer", listenAddr: ":8090"}, {id: "defaultServer", listenAddr: ":8080"}} - -func startServer(spec serverSpec) { - err := http.ListenAndServe(spec.listenAddr, spec.routes) - if err != nil { - log.Fatal("Error serving ", err) - } -} - -func main() { - - pipe := make(chan string) - - // Create default route handler - servers[1].routes = http.NewServeMux() - servers[1].routes.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "text/speech; charset=utf-8") - w.WriteHeader(http.StatusOK) - w.Write([]byte(fmt.Sprintf(defaultMessage, "Unknown"))) - - pipe <- fmt.Sprintf("Received request on server %v", servers[1].id) - servers[0].routes.HandleFunc("/testNewRoute", func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "text/speech; charset=utf-8") - - cmd := exec.Command("ls", "-la", "../") - output, err := cmd.CombinedOutput() - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(fmt.Sprintf(commandErrorMessage, err))) - } else { - w.WriteHeader(http.StatusOK) - w.Write([]byte(fmt.Sprintf(commandExecutionMessage, string(output)))) - } - - pipe <- fmt.Sprintf("Received request \"/\" on server %v", servers[0].id) - }) - }) - - // Create commands route handler - servers[0].routes = http.NewServeMux() - servers[0].routes.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "text/speech; charset=utf-8") - - cmd := exec.Command("ls", "-la", "./") - output, err := cmd.CombinedOutput() - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(fmt.Sprintf(commandErrorMessage, err))) - } else { - w.WriteHeader(http.StatusOK) - w.Write([]byte(fmt.Sprintf(commandExecutionMessage, string(output)))) - } - - pipe <- fmt.Sprintf("Received request \"/testNewRoute\" on server %v", servers[0].id) - }) - - for i := 0; i < len(servers); i++ { - go startServer(servers[i]) - } - - for { - fmt.Println("Tracing: ", <-pipe) - } -} diff --git a/main.go b/main.go index 4e3db80..872e7f1 100644 --- a/main.go +++ b/main.go @@ -1,12 +1,38 @@ package main import ( - "fmt" - - b "github.com/BBVA/kapow/pkg/banner" + "fmt" + b "github.com/BBVA/kapow/pkg/banner" + "net/http" + "os/exec" ) func main() { - ban := b.Banner("0.1.0") - fmt.Println(ban) + ban := b.Banner("0.1.0") + fmt.Println(ban) + + go func() { + http.ListenAndServe(":8080", &userServerHandler{}) + }() + http.ListenAndServe(":8081", &controlAPIHandler{}) + +} + +type userServerHandler struct { +} + +func (m *userServerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + out, err := exec.Command("date").Output() + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + } else { + w.Write(out) + } +} + +type controlAPIHandler struct { +} + +func (m *controlAPIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("Welcome to the control API!")) }