From 1521e1e2998be4ab5ba2ab2abf7b04b18dced1f6 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Mon, 22 Jul 2024 22:23:25 +0100 Subject: [PATCH] Fix lint issues, rename `/h` to `/health`, set HTTP server base context, formatting tweaks --- src/core/link_ws.go | 21 ++++++++------------- src/core/link_wss.go | 6 ++---- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/src/core/link_ws.go b/src/core/link_ws.go index 0544614e..57ff6dea 100644 --- a/src/core/link_ws.go +++ b/src/core/link_ws.go @@ -48,20 +48,19 @@ func (l *linkWSListener) Close() error { if err := l.httpServer.Shutdown(l.ctx); err != nil { return err } - return l.listener.Close() } func (s *wsServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { - if r.URL.Path == "/h" { + if r.URL.Path == "/health" { w.WriteHeader(http.StatusOK) - w.Write([]byte("OK")) + _, _ = w.Write([]byte("OK")) return } + c, err := websocket.Accept(w, r, &websocket.AcceptOptions{ Subprotocols: []string{"ygg-ws"}, }) - if err != nil { return } @@ -71,11 +70,8 @@ func (s *wsServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - netconn := websocket.NetConn(s.ctx, c, websocket.MessageBinary) - - ch := s.ch - ch <- &linkWSConn{ - Conn: netconn, + s.ch <- &linkWSConn{ + Conn: websocket.NetConn(s.ctx, c, websocket.MessageBinary), } } @@ -83,7 +79,6 @@ func (l *links) newLinkWS() *linkWS { lt := &linkWS{ links: l, } - return lt } @@ -94,9 +89,8 @@ func (l *linkWS) dial(ctx context.Context, url *url.URL, info linkInfo, options if err != nil { return nil, err } - netconn := websocket.NetConn(ctx, wsconn, websocket.MessageBinary) return &linkWSConn{ - Conn: netconn, + Conn: websocket.NetConn(ctx, wsconn, websocket.MessageBinary), }, nil } @@ -113,6 +107,7 @@ func (l *linkWS) listen(ctx context.Context, url *url.URL, _ string) (net.Listen ch: ch, ctx: ctx, }, + BaseContext: func(_ net.Listener) context.Context { return ctx }, ReadTimeout: time.Second * 10, WriteTimeout: time.Second * 10, } @@ -123,6 +118,6 @@ func (l *linkWS) listen(ctx context.Context, url *url.URL, _ string) (net.Listen httpServer: httpServer, listener: nl, } - go lwl.httpServer.Serve(nl) + go lwl.httpServer.Serve(nl) // nolint:errcheck return lwl, nil } diff --git a/src/core/link_wss.go b/src/core/link_wss.go index 2007d1e3..a9a8df24 100644 --- a/src/core/link_wss.go +++ b/src/core/link_wss.go @@ -21,9 +21,8 @@ type linkWSSConn struct { func (l *links) newLinkWSS() *linkWSS { lwss := &linkWSS{ - links: l, + links: l, } - return lwss } @@ -34,9 +33,8 @@ func (l *linkWSS) dial(ctx context.Context, url *url.URL, info linkInfo, options if err != nil { return nil, err } - netconn := websocket.NetConn(ctx, wsconn, websocket.MessageBinary) return &linkWSSConn{ - Conn: netconn, + Conn: websocket.NetConn(ctx, wsconn, websocket.MessageBinary), }, nil }