updates
This commit is contained in:
32
app.go
32
app.go
@ -2,8 +2,10 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/gorilla/websocket"
|
||||
// "github.com/pkg/profile"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
@ -12,27 +14,41 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var port string
|
||||
|
||||
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)
|
||||
fmt.Println("Starting up on 80")
|
||||
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:")
|
||||
for n := 0; n < len(s); n++ {
|
||||
fmt.Printf("%d,", s[n])
|
||||
}
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
|
||||
func benchHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// body := "Hello World\n"
|
||||
w.Header().Set("Connection", "keep-alive")
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
// fmt.Fprint(w, body)
|
||||
}
|
||||
func echoHandler(w http.ResponseWriter, r *http.Request) {
|
||||
conn, err := upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
@ -44,7 +60,7 @@ func echoHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
print_binary(p)
|
||||
printBinary(p)
|
||||
err = conn.WriteMessage(messageType, p)
|
||||
if err != nil {
|
||||
return
|
||||
@ -85,11 +101,13 @@ func whoamI(w http.ResponseWriter, req *http.Request) {
|
||||
func api(w http.ResponseWriter, req *http.Request) {
|
||||
hostname, _ := os.Hostname()
|
||||
data := struct {
|
||||
Hostname string `json:"hostname,omitempty"`
|
||||
IP []string `json:"ip,omitempty"`
|
||||
Hostname string `json:"hostname,omitempty"`
|
||||
IP []string `json:"ip,omitempty"`
|
||||
Headers http.Header `json:"headers,omitempty"`
|
||||
}{
|
||||
hostname,
|
||||
[]string{},
|
||||
req.Header,
|
||||
}
|
||||
|
||||
ifaces, _ := net.Interfaces()
|
||||
|
Reference in New Issue
Block a user