Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
396480734c | |||
f1fe8b7fa0 | |||
c60eef3b94 | |||
5ebf416f43 | |||
531a7e2b13 | |||
1362371fa1 |
57
app.go
57
app.go
@ -2,8 +2,12 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
// "github.com/pkg/profile"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -12,27 +16,41 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var port string
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
flag.StringVar(&port, "port", "80", "give me a port number")
|
||||||
|
}
|
||||||
|
|
||||||
var upgrader = websocket.Upgrader{
|
var upgrader = websocket.Upgrader{
|
||||||
ReadBufferSize: 1024,
|
ReadBufferSize: 1024,
|
||||||
WriteBufferSize: 1024,
|
WriteBufferSize: 1024,
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
// defer profile.Start().Stop()
|
||||||
|
flag.Parse()
|
||||||
http.HandleFunc("/echo", echoHandler)
|
http.HandleFunc("/echo", echoHandler)
|
||||||
|
http.HandleFunc("/bench", benchHandler)
|
||||||
http.HandleFunc("/", whoamI)
|
http.HandleFunc("/", whoamI)
|
||||||
http.HandleFunc("/api", api)
|
http.HandleFunc("/api", api)
|
||||||
fmt.Println("Starting up on 80")
|
http.HandleFunc("/health", healthHandler)
|
||||||
log.Fatal(http.ListenAndServe(":80", nil))
|
fmt.Println("Starting up on port " + port)
|
||||||
|
log.Fatal(http.ListenAndServe(":"+port, nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
func print_binary(s []byte) {
|
func printBinary(s []byte) {
|
||||||
fmt.Printf("Received b:")
|
fmt.Printf("Received b:")
|
||||||
for n := 0; n < len(s); n++ {
|
for n := 0; n < len(s); n++ {
|
||||||
fmt.Printf("%d,", s[n])
|
fmt.Printf("%d,", s[n])
|
||||||
}
|
}
|
||||||
fmt.Printf("\n")
|
fmt.Printf("\n")
|
||||||
}
|
}
|
||||||
|
func benchHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Connection", "keep-alive")
|
||||||
|
w.Header().Set("Content-Type", "text/plain")
|
||||||
|
fmt.Fprint(w, "1")
|
||||||
|
}
|
||||||
func echoHandler(w http.ResponseWriter, r *http.Request) {
|
func echoHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
conn, err := upgrader.Upgrade(w, r, nil)
|
conn, err := upgrader.Upgrade(w, r, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -44,7 +62,7 @@ func echoHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
print_binary(p)
|
printBinary(p)
|
||||||
err = conn.WriteMessage(messageType, p)
|
err = conn.WriteMessage(messageType, p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@ -87,9 +105,11 @@ func api(w http.ResponseWriter, req *http.Request) {
|
|||||||
data := struct {
|
data := struct {
|
||||||
Hostname string `json:"hostname,omitempty"`
|
Hostname string `json:"hostname,omitempty"`
|
||||||
IP []string `json:"ip,omitempty"`
|
IP []string `json:"ip,omitempty"`
|
||||||
|
Headers http.Header `json:"headers,omitempty"`
|
||||||
}{
|
}{
|
||||||
hostname,
|
hostname,
|
||||||
[]string{},
|
[]string{},
|
||||||
|
req.Header,
|
||||||
}
|
}
|
||||||
|
|
||||||
ifaces, _ := net.Interfaces()
|
ifaces, _ := net.Interfaces()
|
||||||
@ -109,3 +129,30 @@ func api(w http.ResponseWriter, req *http.Request) {
|
|||||||
}
|
}
|
||||||
json.NewEncoder(w).Encode(data)
|
json.NewEncoder(w).Encode(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type healthState struct {
|
||||||
|
StatusCode int
|
||||||
|
}
|
||||||
|
|
||||||
|
var currentHealthState = healthState{200}
|
||||||
|
var mutexHealthState = &sync.RWMutex{}
|
||||||
|
|
||||||
|
func healthHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
|
if req.Method == http.MethodPost {
|
||||||
|
var statusCode int
|
||||||
|
err := json.NewDecoder(req.Body).Decode(&statusCode)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
w.Write([]byte(err.Error()))
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Update health check status code [%d]\n", statusCode)
|
||||||
|
mutexHealthState.Lock()
|
||||||
|
defer mutexHealthState.Unlock()
|
||||||
|
currentHealthState.StatusCode = statusCode
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
mutexHealthState.RLock()
|
||||||
|
defer mutexHealthState.RUnlock()
|
||||||
|
w.WriteHeader(currentHealthState.StatusCode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user