Documentation ¶
Index ¶
- Constants
- Variables
- func GetReqID(ctx context.Context) string
- func MustWriteJSON(l *zap.SugaredLogger, w http.ResponseWriter, r *http.Request, data interface{}, ...)
- func RequestIDMiddleware(next http.Handler) http.Handler
- func SwaggerHandler(l *zap.SugaredLogger) func(w http.ResponseWriter, r *http.Request)
- func VersionMiddleware(next http.Handler) http.Handler
- func WriteErrJSON(l *zap.SugaredLogger, w http.ResponseWriter, r *http.Request, err error, ...)
- func WriteJSON(w http.ResponseWriter, data interface{}, httpCode int) error
- type ApiHandler
- type HTTPError
- type HealthResp
- type LoggingMiddleware
- type MetricsMiddleware
- type VersionResp
Constants ¶
const RequestIDKey int = 0
RequestIDKey is the key that holds the unique request ID in a request context.
Variables ¶
var APIVersion = "unknown"
APIVersion - variable injected by -X flag during build
var AppName = "unknown"
AppName - variable injected by -X flag during build
var AppVersion = "unknown"
AppVersion - variable injected by -X flag during build
var BuildTime = "unknown"
BuildTime - variable injected by -X flag during build
var LastCommitHash = "unknown"
LastCommitHash - variable injected by -X flag during build
var LastCommitTime = "unknown"
LastCommitTime - variable injected by -X flag during build
var LastCommitUser = "unknown"
LastCommitUser - variable injected by -X flag during build
var StartTime = time.Now().UTC()
StartTime - time(int UTC) when application starts
Functions ¶
func GetReqID ¶
GetReqID returns a request ID from the given context if one is present. Returns the empty string if a request ID cannot be found.
func MustWriteJSON ¶
func MustWriteJSON(l *zap.SugaredLogger, w http.ResponseWriter, r *http.Request, data interface{}, httpCode int)
MustWriteJSON writes response to client, response is a struct defining JSON reply.
func RequestIDMiddleware ¶
RequestIDMiddleware is a middleware that injects a request ID into the context of each request. A request ID is a string generated by Google lib: github.com/google/uuid
func SwaggerHandler ¶
func SwaggerHandler(l *zap.SugaredLogger) func(w http.ResponseWriter, r *http.Request)
func WriteErrJSON ¶
func WriteErrJSON(l *zap.SugaredLogger, w http.ResponseWriter, r *http.Request, err error, httpCode int)
WriteErrJSON wraps error in JSON structure.
Types ¶
type ApiHandler ¶
type ApiHandler interface { Versionz(http.ResponseWriter, *http.Request) Readyz(http.ResponseWriter, *http.Request) Healthz(http.ResponseWriter, *http.Request) }
ApiHandler general handler responsible for providing information about app itself. Available handlers: Versionz, Readyz, Healthz
func NewAPIHandler ¶
func NewAPIHandler(ctx context.Context, l *zap.Logger, repo app.Repository) ApiHandler
type HTTPError ¶
type HTTPError struct { HTTPStatusCode int `json:"httpStatusCode"` Msg string `json:"msg"` InternalErrCode int `json:"internalErrCode"` }
HTTPError - general error response for api.
type HealthResp ¶
type HealthResp struct { Msg string `json:"msg,omitempty"` Uptime string `json:"uptime,omitempty"` DBStatus string `json:"dbStatus,omitempty"` DBError string `json:"dbError,omitempty"` }
HealthResp - struct represents response for /health endpoint.
type LoggingMiddleware ¶
type LoggingMiddleware struct {
// contains filtered or unexported fields
}
func NewLoggingMiddleware ¶
func NewLoggingMiddleware(logger *zap.Logger) *LoggingMiddleware
type MetricsMiddleware ¶
type MetricsMiddleware struct { Histogram *prometheus.HistogramVec Counter *prometheus.CounterVec }
func NewMetricsMiddleware ¶
func NewMetricsMiddleware() *MetricsMiddleware
func (*MetricsMiddleware) Handler ¶
func (p *MetricsMiddleware) Handler(next http.Handler) http.Handler
Metrics godoc @Summary Prometheus metrics @Description returns HTTP requests duration and Go runtime metrics @Tags Prometheus @Produce plain @Router /metrics [get] @Success 200 {string} string "OK"
type VersionResp ¶
type VersionResp struct { AppName string `json:"appName"` APIVersion string `json:"apiVersion"` AppVersion string `json:"version"` BuildTime string `json:"buildTime"` LastCommitHash string `json:"gitCommitHash"` LastCommitUser string `json:"gitCommitUser"` LastCommitTime string `json:"gitCommitTime"` }
VersionResp - struct represents response for /version endpoint.