Skip to content

private NewAutoFlushWriter #129

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

Merged
merged 2 commits into from
Mar 13, 2025
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
4 changes: 4 additions & 0 deletions classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,7 @@
func (p *Context) Stream__2(read io.Reader, buf []byte) {
p.STREAM(200, "", read, buf)
}

func (p *Context) Stream__3(read io.Reader) {
p.STREAM(200, "", read, nil)

Check warning on line 211 in classfile.go

View check run for this annotation

Codecov / codecov/patch

classfile.go#L210-L211

Added lines #L210 - L211 were not covered by tests
}
23 changes: 10 additions & 13 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,39 +164,36 @@

// Auto flush if the buffer is small
if buf != nil && cap(buf) < 32*1024 {
if _, ok := w.(http.Flusher); ok {
w = NewAutoFlushWriter(w)
if f, ok := w.(http.Flusher); ok {
w = newAutoFlushWriter(w, f)

Check warning on line 168 in context.go

View check run for this annotation

Codecov / codecov/patch

context.go#L167-L168

Added lines #L167 - L168 were not covered by tests
}
}

io.CopyBuffer(w, read, buf)
}

// AutoFlushWriter wraps http.ResponseWriter and implements automatic flushing
type AutoFlushWriter struct {
// autoFlushWriter wraps http.ResponseWriter and implements automatic flushing
type autoFlushWriter struct {
http.ResponseWriter // Embeds the original interface
flusher http.Flusher
}

// NewAutoFlushWriter creates a new AutoFlushWriter instance
func NewAutoFlushWriter(w http.ResponseWriter) *AutoFlushWriter {
return &AutoFlushWriter{
// newAutoFlushWriter creates a new AutoFlushWriter instance
func newAutoFlushWriter(w http.ResponseWriter, f http.Flusher) *autoFlushWriter {
return &autoFlushWriter{

Check warning on line 182 in context.go

View check run for this annotation

Codecov / codecov/patch

context.go#L181-L182

Added lines #L181 - L182 were not covered by tests
ResponseWriter: w,
flusher: w.(http.Flusher), // Type assertion must be done at creation time
flusher: f,

Check warning on line 184 in context.go

View check run for this annotation

Codecov / codecov/patch

context.go#L184

Added line #L184 was not covered by tests
}
}

// Write implements the write method with automatic flushing
func (w *AutoFlushWriter) Write(p []byte) (n int, err error) {
func (w *autoFlushWriter) Write(p []byte) (n int, err error) {

Check warning on line 189 in context.go

View check run for this annotation

Codecov / codecov/patch

context.go#L189

Added line #L189 was not covered by tests
// Call the original write method
n, err = w.ResponseWriter.Write(p)
if err != nil {
return
}

// Immediately flush the buffer
if w.flusher != nil {
w.flusher.Flush()
}
w.flusher.Flush()

Check warning on line 197 in context.go

View check run for this annotation

Codecov / codecov/patch

context.go#L197

Added line #L197 was not covered by tests
return
}
42 changes: 42 additions & 0 deletions yap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2023 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package yap_test

import (
"net/http"
"os"
"testing"

"github.com/goplus/yap"
)

func TestBasic(t *testing.T) {
y := yap.New(os.DirFS("."))

y.GET("/", func(ctx *yap.Context) {
ctx.TEXT(200, "text/html", `<html><body>Hello, <a href="/p/123">YAP</a>!</body></html>`)
})
y.GET("/p/:id", func(ctx *yap.Context) {
ctx.YAP(200, "article", yap.H{
"id": ctx.Param("id"),
})
})
y.SetLAS(func(addr string, h http.Handler) error {
return nil
})
y.Run(":8888")
}