-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
113 lines (94 loc) · 2.4 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"fmt"
"io"
"net/http"
"os"
"github.com/gorilla/websocket"
)
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
fmt.Printf("Echo server listening on port %s.\n", port)
err := http.ListenAndServe(":"+port, http.HandlerFunc(handler))
if err != nil {
panic(err)
}
}
var upgrader = websocket.Upgrader{
CheckOrigin: func(*http.Request) bool {
return true
},
}
func handler(wr http.ResponseWriter, req *http.Request) {
fmt.Printf("%s | %s %s\n", req.RemoteAddr, req.Method, req.URL)
if websocket.IsWebSocketUpgrade(req) {
serveWebSocket(wr, req)
} else if req.URL.Path == "/.ws" {
wr.Header().Add("Content-Type", "text/html")
wr.WriteHeader(200)
io.WriteString(wr, websocketHTML)
} else {
serveHTTP(wr, req)
}
}
func serveWebSocket(wr http.ResponseWriter, req *http.Request) {
connection, err := upgrader.Upgrade(wr, req, nil)
if err != nil {
fmt.Printf("%s | %s\n", req.RemoteAddr, err)
return
}
defer connection.Close()
fmt.Printf("%s | upgraded to websocket\n", req.RemoteAddr)
var message []byte
host, err := os.Hostname()
if err == nil {
message = []byte(fmt.Sprintf("Request served by %s", host))
} else {
message = []byte(fmt.Sprintf("Server hostname unknown: %s", err.Error()))
}
err = connection.WriteMessage(websocket.TextMessage, message)
if err == nil {
var messageType int
for {
messageType, message, err = connection.ReadMessage()
if err != nil {
break
}
if messageType == websocket.TextMessage {
fmt.Printf("%s | txt | %s\n", req.RemoteAddr, message)
} else {
fmt.Printf("%s | bin | %d byte(s)\n", req.RemoteAddr, len(message))
}
err = connection.WriteMessage(messageType, message)
if err != nil {
break
}
}
}
if err != nil {
fmt.Printf("%s | %s\n", req.RemoteAddr, err)
}
}
func serveHTTP(wr http.ResponseWriter, req *http.Request) {
wr.Header().Add("Content-Type", "text/plain")
wr.WriteHeader(200)
host, err := os.Hostname()
if err == nil {
fmt.Fprintf(wr, "Request served by %s\n\n", host)
} else {
fmt.Fprintf(wr, "Server hostname unknown: %s\n\n", err.Error())
}
fmt.Fprintf(wr, "%s %s %s\n", req.Proto, req.Method, req.URL)
fmt.Fprintln(wr, "")
fmt.Fprintf(wr, "Host: %s\n", req.Host)
for key, values := range req.Header {
for _, value := range values {
fmt.Fprintf(wr, "%s: %s\n", key, value)
}
}
fmt.Fprintln(wr, "")
io.Copy(wr, req.Body)
}