dummy cli

main
Bel LaPointe 2024-12-14 19:29:54 -07:00
parent 7531918cbe
commit b6a104526c
4 changed files with 68 additions and 9 deletions

View File

@ -13,6 +13,7 @@ import (
"syscall" "syscall"
"time" "time"
"github.com/coder/websocket"
"golang.org/x/time/rate" "golang.org/x/time/rate"
) )
@ -64,20 +65,28 @@ type S struct {
func (s *S) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (s *S) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err := s.serveHTTP(w, r); err != nil { if err := s.serveHTTP(w, r); err != nil {
log.Println(r.URL.Path, "//", err.Error(), r.Header)
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
} }
} }
func (s *S) serveHTTP(w http.ResponseWriter, r *http.Request) error { func (s *S) serveHTTP(w http.ResponseWriter, r *http.Request) error {
switch strings.Split(r.URL.Path[1:], "/")[0] { if isV1(r) || isWS(r) {
case "ws", "v1":
return s.serveAPI(w, r) return s.serveAPI(w, r)
} }
return s.serveStatic(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 { 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 { 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 return err
} }
switch strings.Split(r.URL.Path[1:], "/")[0] { if isWS(r) {
case "ws":
return s.serveWS(w, r) return s.serveWS(w, r)
case "v1": } else if isV1(r) {
return s.serveV1(w, r) return s.serveV1(w, r)
} }
http.NotFound(w, r) http.NotFound(w, r)
return nil return nil
} }
@ -115,10 +124,22 @@ func (s *S) injectContext(w http.ResponseWriter, r *http.Request) error {
return nil return nil
} }
func (s *S) serveWS(w http.ResponseWriter, r *http.Request) error { func (s *S) serveWS(httpw http.ResponseWriter, httpr *http.Request) error {
return io.EOF 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 { func (s *S) serveV1(w http.ResponseWriter, r *http.Request) error {
return io.EOF return fmt.Errorf("not impl: v1")
} }

34
cmd/testws/main.go Normal file
View File

@ -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)
}
}

2
go.mod
View File

@ -3,3 +3,5 @@ module gitea.inhome.blapointe.com/bel/out
go 1.22.3 go 1.22.3
require golang.org/x/time v0.8.0 require golang.org/x/time v0.8.0
require github.com/coder/websocket v1.8.12 // indirect

2
go.sum
View File

@ -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 h1:9i3RxcPv3PZnitoVGMPDKZSq1xW1gK1Xy3ArNOGZfEg=
golang.org/x/time v0.8.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= golang.org/x/time v0.8.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=