Compare commits

...

2 Commits
v4.1.3 ... v4

Author SHA1 Message Date
573086694b handler/meter: add HTTPHandlerFunc method
Some checks failed
coverage / build (push) Successful in 2m37s
test / test (push) Failing after 16m22s
sync / sync (push) Successful in 8s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2025-08-04 21:49:04 +03:00
vtolstov
3992a2bfb2 Apply Code Coverage Badge 2025-05-19 08:54:04 +00:00
2 changed files with 30 additions and 1 deletions

View File

@@ -1,5 +1,5 @@
# HTTP Server
![Coverage](https://img.shields.io/badge/Coverage-5.2%25-red)
![Coverage](https://img.shields.io/badge/Coverage-5.1%25-red)
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.

View File

@@ -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 {