api

package
v0.0.0-...-383fca9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 7, 2020 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const RequestIDKey int = 0

RequestIDKey is the key that holds the unique request ID in a request context.

Variables

View Source
var APIVersion = "unknown"

APIVersion - variable injected by -X flag during build

View Source
var AppName = "unknown"

AppName - variable injected by -X flag during build

View Source
var AppVersion = "unknown"

AppVersion - variable injected by -X flag during build

View Source
var BuildTime = "unknown"

BuildTime - variable injected by -X flag during build

View Source
var LastCommitHash = "unknown"

LastCommitHash - variable injected by -X flag during build

View Source
var LastCommitTime = "unknown"

LastCommitTime - variable injected by -X flag during build

View Source
var LastCommitUser = "unknown"

LastCommitUser - variable injected by -X flag during build

View Source
var StartTime = time.Now().UTC()

StartTime - time(int UTC) when application starts

Functions

func GetReqID

func GetReqID(ctx context.Context) string

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

func RequestIDMiddleware(next http.Handler) http.Handler

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 VersionMiddleware

func VersionMiddleware(next http.Handler) http.Handler

func WriteErrJSON

func WriteErrJSON(l *zap.SugaredLogger, w http.ResponseWriter, r *http.Request, err error, httpCode int)

WriteErrJSON wraps error in JSON structure.

func WriteJSON

func WriteJSON(w http.ResponseWriter, data interface{}, httpCode int) error

WriteJSON writes response to client, response is a struct defining JSON reply.

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

func (*LoggingMiddleware) Handler

func (m *LoggingMiddleware) Handler(next http.Handler) http.Handler

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL