Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support sse writer #4

Merged
merged 3 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go: ['1.22', '1.23']
go: ['1.23']
name: Go ${{ matrix.go }}
steps:
- uses: actions/checkout@v3
Expand Down
37 changes: 26 additions & 11 deletions arpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,13 @@ func (m *Manager) Decode(r *http.Request, v any) error {
p.AdaptRequest(r)
}

if r.Method == http.MethodPost {
switch r.Method {
case http.MethodGet:
if v, ok := v.(FormUnmarshaler); ok {
return WrapError(v.UnmarshalForm(r.Form))
}
return nil
case http.MethodPost:
mt, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type"))
switch mt {
case "application/json":
Expand Down Expand Up @@ -177,19 +183,21 @@ func (m *Manager) NotFoundHandler() http.Handler {
type mapIndex int

const (
_ mapIndex = iota
miContext // context.Context
miRequest // *http.Request
miResponseWriter // http.ResponseWriter
miAny // any
miError // error
_ mapIndex = iota
miContext // context.Context
miRequest // *http.Request
miResponseWriter // http.ResponseWriter
miSSEResponseWriter // SSEResponseWriter
miAny // any
miError // error
)

const (
strContext = "context.Context"
strRequest = "*http.Request"
strResponseWriter = "http.ResponseWriter"
strError = "error"
strContext = "context.Context"
strRequest = "*http.Request"
strResponseWriter = "http.ResponseWriter"
strSSEResponseWriter = "arpc.SSEResponseWriter"
strError = "error"
)

func setOrPanic(m map[mapIndex]int, k mapIndex, v int) {
Expand Down Expand Up @@ -238,6 +246,9 @@ func (m *Manager) Handler(f any) http.Handler {
case strResponseWriter:
setOrPanic(mapIn, miResponseWriter, i)
hasWriter = true
case strSSEResponseWriter:
setOrPanic(mapIn, miSSEResponseWriter, i)
hasWriter = true
default:
setOrPanic(mapIn, miAny, i)
}
Expand Down Expand Up @@ -314,6 +325,10 @@ func (m *Manager) Handler(f any) http.Handler {
if i, ok := mapIn[miResponseWriter]; ok {
vIn[i] = reflect.ValueOf(w)
}
// inject sse response writer
if i, ok := mapIn[miSSEResponseWriter]; ok {
vIn[i] = reflect.ValueOf(newSSEResponseWriter(w))
}

vOut := fv.Call(vIn)
// check error
Expand Down
27 changes: 27 additions & 0 deletions arpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ func f1(r *request) int {
func f2() {
}

func f3(ctx context.Context, w arpc.SSEResponseWriter) error {
w.Write([]byte("data: 1\n\n"))
<-ctx.Done()
return nil
}

func TestSuccess(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -276,3 +282,24 @@ func TestMiddleware(t *testing.T) {
assert.JSONEq(t, `{"ok":true,"result":{}}`, w.Body.String())
})
}

func TestSSE(t *testing.T) {
t.Parallel()

m := arpc.New()
h := m.Handler(f3)
ctx, cancel := context.WithCancel(context.Background())
w := httptest.NewRecorder()
r := httptest.NewRequestWithContext(ctx, "GET", "/", nil)
waitExit := make(chan struct{})
go func() {
h.ServeHTTP(w, r)
waitExit <- struct{}{}
}()
cancel()
<-waitExit

assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "text/event-stream", w.Header().Get("Content-Type"))
assert.Equal(t, "data: 1\n\n", w.Body.String())
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/acoshift/arpc/v2

go 1.22
go 1.23

require github.com/stretchr/testify v1.10.0

Expand Down
48 changes: 48 additions & 0 deletions sse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package arpc

import "net/http"

type SSEResponseWriter interface {
http.ResponseWriter
http.Flusher
}

var _ SSEResponseWriter = (*sseResponseWriter)(nil)

type sseResponseWriter struct {
wrote bool
w http.ResponseWriter
}

func newSSEResponseWriter(w http.ResponseWriter) SSEResponseWriter {
return &sseResponseWriter{w: w}
}

func (w *sseResponseWriter) writeHeader() {
if w.wrote {
return
}
w.WriteHeader(http.StatusOK)
}

func (w *sseResponseWriter) Header() http.Header {
return w.w.Header()
}

func (w *sseResponseWriter) WriteHeader(statusCode int) {
if w.wrote {
return
}
w.wrote = true
w.w.Header().Set("Content-Type", "text/event-stream")
w.w.WriteHeader(statusCode)
}

func (w *sseResponseWriter) Write(b []byte) (int, error) {
w.writeHeader()
return w.w.Write(b)
}

func (w *sseResponseWriter) Flush() {
w.w.(http.Flusher).Flush()
}
Loading