Remove gopoc dir and make the main.go listen to 8080 and 8081 to further check the pipeline.

This commit is contained in:
Roberto Abdelkader Martínez Pérez
2019-09-04 16:35:03 +02:00
parent 0d13531773
commit 6affed99c8
7 changed files with 31 additions and 131 deletions
-1
View File
@@ -1 +0,0 @@
kapow
-2
View File
@@ -1,2 +0,0 @@
gopoc: kapow.go
CGO_ENABLED=0 go build
-3
View File
@@ -1,3 +0,0 @@
module github.com/BBVA/kapow
go 1.12
-37
View File
@@ -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!"))
}
-1
View File
@@ -1 +0,0 @@
kapow
-82
View File
@@ -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)
}
}
+31 -5
View File
@@ -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!"))
}