Skip to content

Easy chunked responses and buffering for the net/http package

Notifications You must be signed in to change notification settings

widemeshio/httpflusher

Repository files navigation

httpflusher

Test

Here is httpflusher, to help you with your chunked responses and buffering of the HTTP protocol endeavors.

http.ResponseWriter + automatic http.Flusher.

Sample

package main

import (
	"net/http"
	"time"

	"github.com/widemeshio/httpflusher"
)

func main() {
	var chunkedEndpoint = func(w http.ResponseWriter, req *http.Request) {
		w = httpflusher.NewResponseWriter(w)
		w.WriteHeader(200)
		w.Write([]byte("log-line-1\n"))
		time.Sleep(time.Second)
		w.Write([]byte("log-line-2\n"))
	}
	http.HandleFunc("/stream-logs", chunkedEndpoint)
	http.ListenAndServe(":8090", nil)
}

Run:

$ curl --no-buffer localhost:8090/stream-logs