From b6a104526cf6a30fc1ed2ab7edf508f12460f85e Mon Sep 17 00:00:00 2001 From: Bel LaPointe <153096461+breel-render@users.noreply.github.com> Date: Sat, 14 Dec 2024 19:29:54 -0700 Subject: [PATCH] dummy cli --- cmd/testapi/main.go | 39 ++++++++++++++++++++++++++++++--------- cmd/testws/main.go | 34 ++++++++++++++++++++++++++++++++++ go.mod | 2 ++ go.sum | 2 ++ 4 files changed, 68 insertions(+), 9 deletions(-) create mode 100644 cmd/testws/main.go diff --git a/cmd/testapi/main.go b/cmd/testapi/main.go index 9416bc8..cacae48 100644 --- a/cmd/testapi/main.go +++ b/cmd/testapi/main.go @@ -13,6 +13,7 @@ import ( "syscall" "time" + "github.com/coder/websocket" "golang.org/x/time/rate" ) @@ -64,20 +65,28 @@ type S struct { func (s *S) ServeHTTP(w http.ResponseWriter, r *http.Request) { if err := s.serveHTTP(w, r); err != nil { + log.Println(r.URL.Path, "//", err.Error(), r.Header) http.Error(w, err.Error(), http.StatusInternalServerError) } } func (s *S) serveHTTP(w http.ResponseWriter, r *http.Request) error { - switch strings.Split(r.URL.Path[1:], "/")[0] { - case "ws", "v1": + if isV1(r) || isWS(r) { return s.serveAPI(w, r) } 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" +} + func (s *S) serveStatic(w http.ResponseWriter, r *http.Request) error { - return io.EOF + return fmt.Errorf("not impl static") } func (s *S) serveAPI(w http.ResponseWriter, r *http.Request) error { @@ -88,12 +97,12 @@ func (s *S) serveAPI(w http.ResponseWriter, r *http.Request) error { return err } - switch strings.Split(r.URL.Path[1:], "/")[0] { - case "ws": + if isWS(r) { return s.serveWS(w, r) - case "v1": + } else if isV1(r) { return s.serveV1(w, r) } + http.NotFound(w, r) return nil } @@ -115,10 +124,22 @@ func (s *S) injectContext(w http.ResponseWriter, r *http.Request) error { return nil } -func (s *S) serveWS(w http.ResponseWriter, r *http.Request) error { - return io.EOF +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 nil } func (s *S) serveV1(w http.ResponseWriter, r *http.Request) error { - return io.EOF + return fmt.Errorf("not impl: v1") } diff --git a/cmd/testws/main.go b/cmd/testws/main.go new file mode 100644 index 0000000..bf4ac77 --- /dev/null +++ b/cmd/testws/main.go @@ -0,0 +1,34 @@ +package main + +import ( + "context" + "log" + "os" + "os/signal" + "syscall" + + "github.com/coder/websocket" +) + +func main() { + ctx, can := signal.NotifyContext(context.Background(), syscall.SIGINT) + defer can() + + c, _, err := websocket.Dial(ctx, os.Args[1], &websocket.DialOptions{ + HTTPHeader: map[string][]string{ + "Cookie": []string{"uuid=x"}, + }, + }) + if err != nil { + panic(err) + } + defer c.CloseNow() + + for { + mt, b, err := c.Read(ctx) + if err != nil { + panic(err) + } + log.Printf("[%s] %s", mt, b) + } +} diff --git a/go.mod b/go.mod index 6ec80f4..6239923 100644 --- a/go.mod +++ b/go.mod @@ -3,3 +3,5 @@ module gitea.inhome.blapointe.com/bel/out go 1.22.3 require golang.org/x/time v0.8.0 + +require github.com/coder/websocket v1.8.12 // indirect diff --git a/go.sum b/go.sum index d06eb41..ec4e17a 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ +github.com/coder/websocket v1.8.12 h1:5bUXkEPPIbewrnkU8LTCLVaxi4N4J8ahufH2vlo4NAo= +github.com/coder/websocket v1.8.12/go.mod h1:LNVeNrXQZfe5qhS9ALED3uA+l5pPqvwXg3CKoDBB2gs= golang.org/x/time v0.8.0 h1:9i3RxcPv3PZnitoVGMPDKZSq1xW1gK1Xy3ArNOGZfEg= golang.org/x/time v0.8.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=