break out ws.go
parent
0db285d4d4
commit
0b1900a7e0
|
|
@ -2,13 +2,10 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/coder/websocket"
|
|
||||||
"golang.org/x/time/rate"
|
"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)
|
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 {
|
func (s *S) serveStatic(w http.ResponseWriter, r *http.Request) error {
|
||||||
http.FS(http.Dir(s.config.Root)).ServeHTTP(w, r)
|
http.FS(http.Dir(s.config.Root)).ServeHTTP(w, r)
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -84,23 +73,3 @@ func (s *S) Session(ctx context.Context) Session {
|
||||||
v, _ := ctx.Value("session").(Session)
|
v, _ := ctx.Value("session").(Session)
|
||||||
return v
|
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")
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -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")
|
||||||
|
}
|
||||||
|
|
@ -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")
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue