Compare commits
8 Commits
v1.0.alpha
...
1.0.1
Author | SHA1 | Date | |
---|---|---|---|
396480734c | |||
f1fe8b7fa0 | |||
c60eef3b94 | |||
5ebf416f43 | |||
531a7e2b13 | |||
1362371fa1 | |||
4a4db8d195 | |||
139ff4dadd |
137
app.go
137
app.go
@ -1,28 +1,88 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/gorilla/mux"
|
||||
"sync"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
// "github.com/pkg/profile"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var port string
|
||||
|
||||
r := mux.NewRouter()
|
||||
r.HandleFunc("/", whoamI)
|
||||
http.Handle("/", r)
|
||||
fmt.Println("Starting up on 80")
|
||||
log.Fatal(http.ListenAndServe(":80", nil))
|
||||
func init() {
|
||||
flag.StringVar(&port, "port", "80", "give me a port number")
|
||||
}
|
||||
|
||||
var upgrader = websocket.Upgrader{
|
||||
ReadBufferSize: 1024,
|
||||
WriteBufferSize: 1024,
|
||||
}
|
||||
|
||||
func main() {
|
||||
// defer profile.Start().Stop()
|
||||
flag.Parse()
|
||||
http.HandleFunc("/echo", echoHandler)
|
||||
http.HandleFunc("/bench", benchHandler)
|
||||
http.HandleFunc("/", whoamI)
|
||||
http.HandleFunc("/api", api)
|
||||
http.HandleFunc("/health", healthHandler)
|
||||
fmt.Println("Starting up on port " + port)
|
||||
log.Fatal(http.ListenAndServe(":"+port, nil))
|
||||
}
|
||||
|
||||
func printBinary(s []byte) {
|
||||
fmt.Printf("Received b:")
|
||||
for n := 0; n < len(s); n++ {
|
||||
fmt.Printf("%d,", s[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) {
|
||||
conn, err := upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
for {
|
||||
messageType, p, err := conn.ReadMessage()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
printBinary(p)
|
||||
err = conn.WriteMessage(messageType, p)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func whoamI(w http.ResponseWriter, req *http.Request) {
|
||||
u, _ := url.Parse(req.URL.String())
|
||||
queryParams := u.Query()
|
||||
wait := queryParams.Get("wait")
|
||||
if len(wait) > 0 {
|
||||
duration, err := time.ParseDuration(wait)
|
||||
if err == nil {
|
||||
time.Sleep(duration)
|
||||
}
|
||||
}
|
||||
hostname, _ := os.Hostname()
|
||||
fmt.Fprintln(w, "Hostname : ", hostname)
|
||||
fmt.Fprintln(w, "Hostname:", hostname)
|
||||
ifaces, _ := net.Interfaces()
|
||||
// handle err
|
||||
for _, i := range ifaces {
|
||||
addrs, _ := i.Addrs()
|
||||
// handle err
|
||||
@ -34,8 +94,65 @@ func whoamI(w http.ResponseWriter, req *http.Request) {
|
||||
case *net.IPAddr:
|
||||
ip = v.IP
|
||||
}
|
||||
fmt.Fprintln(w, "IP : ", ip)
|
||||
fmt.Fprintln(w, "IP:", ip)
|
||||
}
|
||||
}
|
||||
req.Write(w)
|
||||
}
|
||||
|
||||
func api(w http.ResponseWriter, req *http.Request) {
|
||||
hostname, _ := os.Hostname()
|
||||
data := struct {
|
||||
Hostname string `json:"hostname,omitempty"`
|
||||
IP []string `json:"ip,omitempty"`
|
||||
Headers http.Header `json:"headers,omitempty"`
|
||||
}{
|
||||
hostname,
|
||||
[]string{},
|
||||
req.Header,
|
||||
}
|
||||
|
||||
ifaces, _ := net.Interfaces()
|
||||
for _, i := range ifaces {
|
||||
addrs, _ := i.Addrs()
|
||||
// handle err
|
||||
for _, addr := range addrs {
|
||||
var ip net.IP
|
||||
switch v := addr.(type) {
|
||||
case *net.IPNet:
|
||||
ip = v.IP
|
||||
case *net.IPAddr:
|
||||
ip = v.IP
|
||||
}
|
||||
data.IP = append(data.IP, ip.String())
|
||||
}
|
||||
}
|
||||
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