Code Monkey home page Code Monkey logo

Comments (7)

ashertz-dev avatar ashertz-dev commented on August 25, 2024

I resolve it

from go-restful.

maratrixx avatar maratrixx commented on August 25, 2024

I resolve it

How to solve the problem? I also encountered the same problem. thanks.

from go-restful.

ashertz-dev avatar ashertz-dev commented on August 25, 2024

I resolve it

How to solve the problem? I also encountered the same problem. thanks.

Sorry, it has been a long time, and I have already forgotten. I find some code block in my project. Maybe it's useful.

type WriterFlusher interface {
	io.Writer
	http.Flusher
}

// wrapperHTTP used to wrap http request for print
func wrapperHTTP(h http.Handler) http.Handler {
	slogger := slog.New(slog.NewTextHandler(os.Stdout))
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		m := httpsnoop.CaptureMetrics(h, w, r)
		defer func() {
			slogger.Info("",
				"method", r.Method,
				"url", r.URL,
				"proto", r.Proto,
				"user-agent", r.UserAgent(),
				"remote", r.RemoteAddr,
				"referer", r.Referer(),
				"status", m.Code,
				"size", m.Written,
				"lat-ms", m.Duration.Milliseconds(),
			)
		}()
	})
}

func ListenAndServe(addr string, handler http.Handler) error {
	return http.ListenAndServe(addr, wrapperHTTP(handler))
}

from go-restful.

maratrixx avatar maratrixx commented on August 25, 2024

I resolve it

How to solve the problem? I also encountered the same problem. thanks.

Sorry, it has been a long time, and I have already forgotten. I find some code block in my project. Maybe it's useful.

type WriterFlusher interface {
	io.Writer
	http.Flusher
}

// wrapperHTTP used to wrap http request for print
func wrapperHTTP(h http.Handler) http.Handler {
	slogger := slog.New(slog.NewTextHandler(os.Stdout))
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		m := httpsnoop.CaptureMetrics(h, w, r)
		defer func() {
			slogger.Info("",
				"method", r.Method,
				"url", r.URL,
				"proto", r.Proto,
				"user-agent", r.UserAgent(),
				"remote", r.RemoteAddr,
				"referer", r.Referer(),
				"status", m.Code,
				"size", m.Written,
				"lat-ms", m.Duration.Milliseconds(),
			)
		}()
	})
}

func ListenAndServe(addr string, handler http.Handler) error {
	return http.ListenAndServe(addr, wrapperHTTP(handler))
}

Thanks your reply. But this does not appear to use the flush method.

from go-restful.

nullbus avatar nullbus commented on August 25, 2024

I suggest to figure out the actual type of ResponseWriter the server is using (using debbuger or logging). I think your ResponseWriter is not being from Go's default http package.

In this package, there is some path that it can wrap default http.ResponseWriter:

go-restful/container.go

Lines 263 to 272 in c691c18

if contentEncodingEnabled {
doCompress, encoding := wantsCompressedResponse(httpRequest, httpWriter)
if doCompress {
var err error
writer, err = NewCompressingResponseWriter(httpWriter, encoding)
if err != nil {
log.Print("unable to install compressor: ", err)
httpWriter.WriteHeader(http.StatusInternalServerError)
return
}

It replaces writer to CompressingResponseWriter if something compression is needed and CompressingResponseWriter is not implementing Flush method for now. If your case is it, there are some options:

  • disable compression
  • implement Flush method to CompressingResponseWriter
  • use Close() instead of Flush()

from go-restful.

emicklei avatar emicklei commented on August 25, 2024

I think adding a Flush method is an easy step to take

from go-restful.

emicklei avatar emicklei commented on August 25, 2024

#538

from go-restful.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.