File tree Expand file tree Collapse file tree 4 files changed +8
-2
lines changed Expand file tree Collapse file tree 4 files changed +8
-2
lines changed Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ FROM alpine:latest
11
11
WORKDIR /opt/
12
12
13
13
COPY --from=builder /github.com/Lapp-coder/websocket-chat/build/bin/chat .
14
+ COPY --from=builder /github.com/Lapp-coder/websocket-chat/index.html .
14
15
15
16
EXPOSE 8080
16
17
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package main
2
2
3
3
import (
4
4
"github.com/gorilla/websocket"
5
+ "html/template"
5
6
"net/http"
6
7
)
7
8
@@ -10,6 +11,8 @@ var upgrader = websocket.Upgrader{
10
11
WriteBufferSize : 1024 ,
11
12
}
12
13
14
+ var indexTemplate * template.Template
15
+
13
16
type handler struct {
14
17
hub * hub
15
18
}
@@ -24,7 +27,7 @@ func (h *handler) initRoutes() {
24
27
}
25
28
26
29
func (h * handler ) index (w http.ResponseWriter , r * http.Request ) {
27
- http . ServeFile (w , r , "index.html" )
30
+ indexTemplate . Execute (w , r . Host )
28
31
}
29
32
30
33
func (h * handler ) chat (w http.ResponseWriter , r * http.Request ) {
Original file line number Diff line number Diff line change 28
28
} ) ;
29
29
30
30
if ( window [ "WebSocket" ] ) {
31
- conn = new WebSocket ( "ws://localhost:8080 /chat" ) ;
31
+ conn = new WebSocket ( "ws://{{$}} /chat" ) ;
32
32
conn . onclose = function ( evt ) {
33
33
appendLog ( $ ( "<div><b>Соединение закрыто.</b></div>" ) )
34
34
}
Original file line number Diff line number Diff line change @@ -3,12 +3,14 @@ package main
3
3
import (
4
4
"context"
5
5
"github.com/sirupsen/logrus"
6
+ "html/template"
6
7
"os"
7
8
"os/signal"
8
9
"syscall"
9
10
)
10
11
11
12
func main () {
13
+ indexTemplate = template .Must (template .ParseFiles ("index.html" ))
12
14
hub := newHub ()
13
15
handler := newHandler (hub )
14
16
handler .initRoutes ()
You can’t perform that action at this time.
0 commit comments