{"modulePath":"github.com/DoNewsCode/core","version":"v0.12.3","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package srvhttp\n\nPackage srvhttp groups helpers for server side http use cases. Components in the Package srvhttp must stay align with net/http implementation, rather than third party vendors such as gin.\n\n## Variables\n\n```go\nvar Trace = nethttp.Middleware\n```\nTrace is an alias of nethttp.Middleware. It is recommended to use the trace implementation in github.com/opentracing-contrib/go-stdlib. This alias serves as a pointer to it.\n\n## Functions\n\n```go\nfunc MakeApacheLogMiddleware(logger log.Logger) func(handler http.Handler) http.Handler\n```\nMakeApacheLogMiddleware creates a standard HTTP middleware responsible for access logging.\n\n```go\nfunc Metrics(metrics *RequestDurationSeconds) func(handler http.Handler) http.Handler\n```\nMetrics is a middleware for standard library http package. It records the request duration in a histogram.\n\n## Types\n\n```go\ntype ApacheLogAdapter struct {\n\tlog.Logger\n}\n```\nApacheLogAdapter logs HTTP requests in the Apache Common Log Format.\n\nSee [http://httpd.apache.org/docs/2.2/logs.html#common](http://httpd.apache.org/docs/2.2/logs.html#common)\n\n```go\nfunc (a ApacheLogAdapter) Write(p []byte) (n int, err error)\n```\nWrite redirect the data stream to the underlying log.Logger\n\n```go\ntype DebugModule struct{}\n```\nDebugModule defines a http provider for container.Container. It calls pprof underneath. For instance, \\`/debug/pprof/cmdline\\` invokes pprof.Cmdline\n\n```go\nfunc (d DebugModule) ProvideHTTP(router *mux.Router)\n```\nProvideHTTP implements container.HTTPProvider\n\n```go\ntype DocsModule struct{}\n```\nDocsModule defines a http provider for container.Container. It serves static files under \\`./docs\\`. \\`./docs\\` is supposed to contain doc serving engines such as Swagger, Docify or plain markdowns.\n\n```go\nfunc (d DocsModule) ProvideHTTP(router *mux.Router)\n```\nProvideHTTP implements container.HTTPProvider\n\n```go\ntype Headerer interface {\n\t// Headers provides the header map that will be sent by http.ResponseWriter WriteHeader.\n\tHeaders() http.Header\n}\n```\n\n```go\ntype HealthCheckModule struct{}\n```\nHealthCheckModule defines a http provider for container.Container. It uses github.com/heptiolabs/healthcheck underneath. It doesn't do much out of box other than providing liveness check at “/live“ and readiness check at “/ready“. End user should add health checking functionality by themself, e.g. probe if database connection pool has exhausted at readiness check.\n\n```go\nfunc (h HealthCheckModule) ProvideHTTP(router *mux.Router)\n```\nProvideHTTP implements container.HTTPProvider\n\n```go\ntype MetricsModule struct{}\n```\nMetricsModule exposes prometheus metrics to \\`/metrics\\`. This is the standard route for prometheus metrics scrappers.\n\n```go\nfunc (m MetricsModule) ProvideHTTP(router *mux.Router)\n```\nProvideHTTP implements container.HTTPProvider\n\n```go\ntype RequestDurationSeconds struct {\n\t// contains filtered or unexported fields\n}\n```\nRequestDurationSeconds is a wrapper around a histogram that measures the request latency. The RequestDurationSeconds exposes label setters such as module, service and route. If a label is set more than once, the one set last will take precedence.\n\n```go\nfunc NewRequestDurationSeconds(histogram metrics.Histogram) *RequestDurationSeconds\n```\nNewRequestDurationSeconds returns a new RequestDurationSeconds. The default labels are set to \"unknown\".\n\n```go\nfunc (r *RequestDurationSeconds) Module(module string) *RequestDurationSeconds\n```\nModule specifies the module label for RequestDurationSeconds.\n\n```go\nfunc (r *RequestDurationSeconds) Observe(duration time.Duration)\n```\nObserve records the time taken to process the request.\n\n```go\nfunc (r *RequestDurationSeconds) Route(route string) *RequestDurationSeconds\n```\nRoute specifies the method label for RequestDurationSeconds.\n\n```go\nfunc (r *RequestDurationSeconds) Service(service string) *RequestDurationSeconds\n```\nService specifies the service label for RequestDurationSeconds.\n\n```go\nfunc (r *RequestDurationSeconds) Status(status int) *RequestDurationSeconds\n```\nStatus specifies the status label for RequestDurationSeconds.\n\n```go\ntype ResponseEncoder struct {\n\t// contains filtered or unexported fields\n}\n```\nResponseEncoder encodes either a successful http response or errors to the JSON format, and pipe the serialized json data to the http.ResponseWriter.\n\nIt asserts the type of input in following order and figures out the matching encoder:\n\n\tjson.Marshaler: use encoding/json encoder.\n\tproto.Message: use the jsonpb encoder\n\terror: {\"message\": err.Error()}\n\tby default: encoding/json encoder.\n\nIt also populates http status code and headers if necessary.\n\n```go\nfunc NewResponseEncoder(w http.ResponseWriter) *ResponseEncoder\n```\nNewResponseEncoder wraps the http.ResponseWriter and returns a reference to ResponseEncoder\n\n```go\nfunc (s *ResponseEncoder) Encode(response interface{}, err error)\n```\nEncode serialize response and error to the corresponding json format and write then to the output buffer.\n\nSee ResponseEncoder for details.\n\n```go\nfunc (s *ResponseEncoder) EncodeError(err error)\n```\nEncodeError encodes an Error. If the error is not a StatusCoder, the http.StatusInternalServerError will be used.\n\n```go\nfunc (s *ResponseEncoder) EncodeResponse(response interface{})\n```\nEncodeResponse encodes an response value. If the response is not a StatusCoder, the http.StatusInternalServerError will be used.\n\n```go\ntype StatusCoder interface {\n\t// StatusCode provides the status code for the http response.\n\tStatusCode() int\n}\n```\n\n","path":"github.com/DoNewsCode/core/srvhttp","name":"srvhttp","synopsis":"Package srvhttp groups helpers for server side http use cases.","isRedistributable":true}
