@@ -13,36 +13,35 @@ func main() {
13
13
if port == "" {
14
14
log .Fatal ("$PORT must be set" )
15
15
}
16
- chat := os .Getenv ("CHAT " )
16
+ chat := os .Getenv ("CHATPORT " )
17
17
if chat == "" {
18
- log .Fatal ("$CHAT must be set" )
18
+ log .Fatal ("$CHATPORT must be set" )
19
19
}
20
-
21
20
wd , err := os .Getwd ()
22
21
if err != nil {
23
- log .Fatal (err )
24
- }
25
- index , err := indexHandler (wd )
26
- if err != nil {
27
- log .Fatal (err )
22
+ log .Fatalf ("can not get os working directory: %v" , err )
28
23
}
24
+
29
25
web := http .FileServer (http .Dir (wd + "/web" ))
30
26
31
- http .Handle ("/" , index )
27
+ http .Handle ("/" , web )
32
28
http .Handle ("/web/" , http .StripPrefix ("/web/" , web ))
33
- http .Handle ("/ws" , wsHandler (chat ))
29
+ http .Handle ("/ws" , wsHandler (":" + chat ))
34
30
31
+ log .Printf ("proxy is listening on localhost:%v" , port )
35
32
log .Fatal (http .ListenAndServe (":" + port , nil ))
36
33
}
37
34
38
35
func wsHandler (upstream string ) http.Handler {
39
36
return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
40
37
peer , err := net .Dial ("tcp" , upstream )
41
38
if err != nil {
39
+ log .Printf ("dial upstream error: %v" , err )
42
40
w .WriteHeader (502 )
43
41
return
44
42
}
45
43
if err := r .Write (peer ); err != nil {
44
+ log .Printf ("write request to upstream error: %v" , err )
46
45
w .WriteHeader (502 )
47
46
return
48
47
}
0 commit comments