Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
573086694b | |||
|
3992a2bfb2 |
@@ -1,5 +1,5 @@
|
||||
# HTTP Server
|
||||

|
||||

|
||||
|
||||
The HTTP Server is a go-micro.Server. It's a partial implementation which strips out codecs, transports, etc but enables you
|
||||
to create a HTTP Server that could potentially be used for REST based API services.
|
||||
|
@@ -5,6 +5,8 @@ import (
|
||||
"compress/gzip"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
http "net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
@@ -85,6 +87,33 @@ func NewHandler(opts ...Option) *Handler {
|
||||
return &Handler{Options: options}
|
||||
}
|
||||
|
||||
func (h *Handler) HTTPHandlerFunc(w http.ResponseWriter, r *http.Request) {
|
||||
var wr io.Writer
|
||||
if v := r.Header.Get(acceptEncodingHeader); strings.Contains(v, "gzip") && !h.Options.DisableCompress {
|
||||
w.Header().Add(contentEncodingHeader, "gzip")
|
||||
|
||||
gz := gzipPool.Get().(*gzip.Writer)
|
||||
defer gzipPool.Put(gz)
|
||||
|
||||
gz.Reset(w)
|
||||
defer gz.Close()
|
||||
|
||||
wr = gz
|
||||
} else {
|
||||
wr = w
|
||||
}
|
||||
|
||||
if err := h.Options.Meter.Write(wr, h.Options.MeterOptions...); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// gz.Flush() must be called after writing metrics to ensure buffered data is written to the underlying writer.
|
||||
if gz, ok := wr.(*gzip.Writer); ok {
|
||||
gz.Flush()
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) Metrics(ctx context.Context, req *codecpb.Frame, rsp *codecpb.Frame) error {
|
||||
log, ok := logger.FromContext(ctx)
|
||||
if !ok {
|
||||
|
Reference in New Issue
Block a user