From 0b1900a7e026acdccbf4d4aaa948407d86eda061 Mon Sep 17 00:00:00 2001 From: Bel LaPointe <153096461+breel-render@users.noreply.github.com> Date: Sat, 14 Dec 2024 20:37:17 -0700 Subject: [PATCH] break out ws.go --- cmd/server/server.go | 31 ------------------------------- cmd/server/v1.go | 15 +++++++++++++++ cmd/server/ws.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 31 deletions(-) create mode 100644 cmd/server/v1.go create mode 100644 cmd/server/ws.go diff --git a/cmd/server/server.go b/cmd/server/server.go index 42ae2f1..e452e2e 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -2,13 +2,10 @@ package main import ( "context" - "fmt" "io" "log" "net/http" - "strings" - "github.com/coder/websocket" "golang.org/x/time/rate" ) @@ -32,14 +29,6 @@ func (s *S) serveHTTP(w http.ResponseWriter, r *http.Request) error { return s.serveStatic(w, r) } -func isV1(r *http.Request) bool { - return strings.HasPrefix(r.URL.Path, "/v1/") -} - -func isWS(r *http.Request) bool { - return r.URL.Path == "/ws" || strings.HasPrefix(r.URL.Path, "/ws/") -} - func (s *S) serveStatic(w http.ResponseWriter, r *http.Request) error { http.FS(http.Dir(s.config.Root)).ServeHTTP(w, r) return nil @@ -84,23 +73,3 @@ func (s *S) Session(ctx context.Context) Session { v, _ := ctx.Value("session").(Session) return v } - -func (s *S) serveWS(httpw http.ResponseWriter, httpr *http.Request) error { - ctx := httpr.Context() - - c, err := websocket.Accept(httpw, httpr, nil) - if err != nil { - return err - } - defer c.CloseNow() - - if err := c.Write(ctx, 1, []byte("hello world")); err != nil { - return err - } - - return fmt.Errorf("not impl") -} - -func (s *S) serveV1(w http.ResponseWriter, r *http.Request) error { - return fmt.Errorf("not impl: v1") -} diff --git a/cmd/server/v1.go b/cmd/server/v1.go new file mode 100644 index 0000000..af9b72c --- /dev/null +++ b/cmd/server/v1.go @@ -0,0 +1,15 @@ +package main + +import ( + "fmt" + "net/http" + "strings" +) + +func isV1(r *http.Request) bool { + return strings.HasPrefix(r.URL.Path, "/v1/") +} + +func (s *S) serveV1(w http.ResponseWriter, r *http.Request) error { + return fmt.Errorf("not impl: v1") +} diff --git a/cmd/server/ws.go b/cmd/server/ws.go new file mode 100644 index 0000000..8a0750b --- /dev/null +++ b/cmd/server/ws.go @@ -0,0 +1,29 @@ +package main + +import ( + "fmt" + "net/http" + "strings" + + "github.com/coder/websocket" +) + +func isWS(r *http.Request) bool { + return r.URL.Path == "/ws" || strings.HasPrefix(r.URL.Path, "/ws/") +} + +func (s *S) serveWS(httpw http.ResponseWriter, httpr *http.Request) error { + ctx := httpr.Context() + + c, err := websocket.Accept(httpw, httpr, nil) + if err != nil { + return err + } + defer c.CloseNow() + + if err := c.Write(ctx, 1, []byte("hello world")); err != nil { + return err + } + + return fmt.Errorf("not impl") +}