Documentation ¶
Index ¶
- Constants
- Variables
- func GetConfig(configFile string, config interface{}) error
- func NewStdLogger(l *Logger) *log.Logger
- func NewStdLoggerC(context interface{}) *log.Logger
- func NewType(orig interface{}) interface{}
- func Pipeline(mws []func(http.HandlerFunc) http.HandlerFunc, endpoint http.HandlerFunc) http.HandlerFunc
- func ReplyWithError(w http.ResponseWriter, r *http.Request, err error)
- func SetDefaultLogLevel(level string)
- func WithLog(next http.HandlerFunc) http.HandlerFunc
- func WithLogContext(ctxt Context) func(http.HandlerFunc) http.HandlerFunc
- func WithMethodNotAllowed(allowedMethods ...string) http.HandlerFunc
- func WithNotFound() http.HandlerFunc
- func WriteJSON(w http.ResponseWriter, r *http.Request, v interface{})
- type Context
- type Error
- type LogContext
- type LoggableResponseWriter
- type Logger
- func (l *Logger) Debug(message string, args ...interface{})
- func (l *Logger) DebugC(context interface{}, message string, args ...interface{})
- func (l *Logger) DebugRequest(message string, r *http.Request)
- func (l *Logger) DebugRequestC(context interface{}, message string, r *http.Request)
- func (l *Logger) DebugRequestOut(message string, r *http.Request)
- func (l *Logger) DebugRequestOutC(context interface{}, message string, r *http.Request)
- func (l *Logger) DebugResponse(message string, r *http.Response)
- func (l *Logger) DebugResponseC(context interface{}, message string, r *http.Response)
- func (l *Logger) Error(message string, args ...interface{})
- func (l *Logger) ErrorC(context interface{}, message string, args ...interface{})
- func (l *Logger) Fatal(message string, args ...interface{})
- func (l *Logger) FatalC(context interface{}, message string, args ...interface{})
- func (l *Logger) GetLevel() string
- func (l *Logger) GetLogContext() interface{}
- func (l *Logger) GetWriter() io.Writer
- func (l *Logger) Info(message string, args ...interface{})
- func (l *Logger) InfoC(context interface{}, message string, args ...interface{})
- func (l *Logger) SetLevel(levelName string)
- func (l *Logger) SetLogContext(context interface{})
- func (l *Logger) SetWriter(o io.Writer)
- func (l *Logger) Warn(message string, args ...interface{})
- func (l *Logger) WarnC(context interface{}, message string, args ...interface{})
- type ReqLogContext
- type RespLogContext
- type Validator
- func (v *Validator) LoadSchemas(schemasDir string) error
- func (v *Validator) ValidateBytes(schemaName string, data []byte, o interface{}) error
- func (v *Validator) ValidateConfig(schemaName string, config interface{}) error
- func (v *Validator) ValidateObject(schemaName string, data interface{}) error
- func (v *Validator) ValidateRequestBody(schemaName string, r *http.Request, o interface{}) error
- func (v *Validator) ValidateSafeRequestBody(schemaName string, r *http.Request, o interface{}) error
Constants ¶
const RFC3339Milli = "2006-01-02T15:04:05.000Z07:00"
RFC3339Milli date layout
Variables ¶
var ( CorrelatorHTTPHeader = "Unica-Correlator" RequestLogMessage = "Request" ResponseLogMessage = "Response" )
CorrelatorHTTPHeader contains the name of the HTTP header that transports the correlator. The correlator enables to match all the HTTP requests and responses for a same web flow.
var LogLevelNames = []string{"DEBUG", "INFO", "WARN", "ERROR", "FATAL"}
LogLevelNames is an array with the valid log levels.
var LoggerContextKey = loggerContextKey("logger")
LoggerContextKey is a unique key to store the logger in the golang context.
var NotFoundError = &Error{ Message: "not found", Status: http.StatusNotFound, Code: "invalid_request", Description: "not found", }
NotFoundError with a not found Error
Functions ¶
func GetConfig ¶
GetConfig prepares the configuration by merging multiple sources: - Default configuration stored in a json file - Environment variables
func NewStdLogger ¶
NewStdLogger returns a standard logger struct but using our custom logger.
func NewStdLoggerC ¶
NewStdLoggerC returns a standard logger struct but using our custom logger with a specific context.
func NewType ¶
func NewType(orig interface{}) interface{}
NewType creates a new object with the same type using reflection. Note that the new object is empty.
func Pipeline ¶
func Pipeline(mws []func(http.HandlerFunc) http.HandlerFunc, endpoint http.HandlerFunc) http.HandlerFunc
Pipeline returns a HandlerFunc resulting of a list of middlewares and a final endpoint.
The following example creates a pipeline of 2 middlewares:
mws := []func(http.HandlerFunc) http.HandlerFunc{ govice.WithLogContext(&logContext), govice.WithLog, } p := govice.Pipeline(mws, next)
func ReplyWithError ¶
func ReplyWithError(w http.ResponseWriter, r *http.Request, err error)
ReplyWithError to send a HTTP response with the error document.
func SetDefaultLogLevel ¶
func SetDefaultLogLevel(level string)
SetDefaultLogLevel sets the default log level. This default can be overridden with SetLevel method.
func WithLog ¶
func WithLog(next http.HandlerFunc) http.HandlerFunc
WithLog is a middleware to log the request and response. Note that WithContext middleware is required to initialize the logger with a context.
func WithLogContext ¶
func WithLogContext(ctxt Context) func(http.HandlerFunc) http.HandlerFunc
WithLogContext is a middleware constructor to initialize the log context with the transactionID and correlator. It also stores the logger in the golang context. Note that the context is initialized with an initial context (see ctxt).
func WithMethodNotAllowed ¶
func WithMethodNotAllowed(allowedMethods ...string) http.HandlerFunc
WithMethodNotAllowed is a middleware to reply with an error when the HTTP method is not supported. The allowedMethods must be a list of HTTP methods.
func WithNotFound ¶
func WithNotFound() http.HandlerFunc
WithNotFound is a middleware to reply with a not found error (status code: 404).
Types ¶
type Context ¶
type Context interface { Clone() Context GetCorrelator() string SetCorrelator(corr string) GetTransactionID() string SetTransactionID(trans string) }
Context to support extending the log context with other parameters.
type Error ¶
type Error struct { Message string `json:"-"` Status int `json:"-"` Alarm string `json:"-"` Code string `json:"error"` Description string `json:"error_description,omitempty"` }
Error is a custom error. This struct stores information to generate an HTTP error response if required.
func NewBadGatewayError ¶
NewBadGatewayError to create a bad gateway error.
func NewInvalidRequestError ¶
NewInvalidRequestError to create an invalid_request Error
func NewServerError ¶
NewServerError to create a server_error Error
func NewUnauthorizedClientError ¶
NewUnauthorizedClientError to create an unauthorized_client Error
func (*Error) GetResponse ¶
GetResponse to get a http.Response object from an Error.
func (*Error) Response ¶
func (e *Error) Response(w http.ResponseWriter)
Response generates a JSON document for an Error. JSON is in the form: {"error": "invalid_request", "error_description": "xxx"}
type LogContext ¶
type LogContext struct { TransactionID string `json:"trans,omitempty"` Correlator string `json:"corr,omitempty"` Operation string `json:"op,omitempty"` Service string `json:"svc,omitempty"` Component string `json:"comp,omitempty"` User string `json:"user,omitempty"` Realm string `json:"realm,omitempty"` Alarm string `json:"alarm,omitempty"` }
LogContext represents the log context for a base service. Note that LogContext implements the Context interface.
func GetLogContext ¶
func GetLogContext(r *http.Request) *LogContext
GetLogContext gets the log context associated to a request.
func (*LogContext) GetCorrelator ¶
func (c *LogContext) GetCorrelator() string
GetCorrelator returns the log context correlator.
func (*LogContext) GetTransactionID ¶
func (c *LogContext) GetTransactionID() string
GetTransactionID returns the log context transactionID (trans).
func (*LogContext) SetCorrelator ¶
func (c *LogContext) SetCorrelator(corr string)
SetCorrelator to set a correlator in the log context.
func (*LogContext) SetTransactionID ¶
func (c *LogContext) SetTransactionID(trans string)
SetTransactionID to set a transactionID in the log context.
type LoggableResponseWriter ¶
type LoggableResponseWriter struct { Status int http.ResponseWriter }
LoggableResponseWriter is a ResponseWriter wrapper to log the response status code.
func (*LoggableResponseWriter) WriteHeader ¶
func (w *LoggableResponseWriter) WriteHeader(statusCode int)
WriteHeader overwrites ResponseWriter's WriteHeader to store the response status code.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger type.
func (*Logger) DebugRequest ¶
DebugRequest to dump the request at debug level.
func (*Logger) DebugRequestC ¶
DebugRequestC to dump the request at debug level.
func (*Logger) DebugRequestOut ¶
DebugRequestOut to dump the output request at debug level.
func (*Logger) DebugRequestOutC ¶
DebugRequestOutC to dump the output request at debug level.
func (*Logger) DebugResponse ¶
DebugResponse to dump the response at debug level.
func (*Logger) DebugResponseC ¶
DebugResponseC to dump the response at debug level.
func (*Logger) GetLogContext ¶
func (l *Logger) GetLogContext() interface{}
GetLogContext to get the global context.
func (*Logger) SetLogContext ¶
func (l *Logger) SetLogContext(context interface{})
SetLogContext to set a global context.
type ReqLogContext ¶
type ReqLogContext struct { Method string `json:"method,omitempty"` Path string `json:"path,omitempty"` RemoteAddr string `json:"remoteaddr,omitempty"` }
ReqLogContext is a complementary LogContext to log information about the request (e.g. path).
type RespLogContext ¶
type RespLogContext struct { Status int `json:"status,omitempty"` Latency int `json:"latency,omitempty"` Location string `json:"location,omitempty"` }
RespLogContext is a complementary LogContext to log information about the response (e.g. status code).
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
Validator type.
func (*Validator) LoadSchemas ¶
LoadSchemas to load all the JSON schemas stored in schemasDir directory (it may be an absolute path or relative to the current working directory)
func (*Validator) ValidateBytes ¶
ValidateBytes reads a byte array, validates it against a JSON schema, and unmarshall it.
func (*Validator) ValidateConfig ¶
ValidateConfig to validate the configuration against config.json schema.
func (*Validator) ValidateObject ¶
ValidateObject to validate an object against a JSON schema.
func (*Validator) ValidateRequestBody ¶
ValidateRequestBody reads the request body, validates it against a JSON schema, and unmarshall it.
func (*Validator) ValidateSafeRequestBody ¶
func (v *Validator) ValidateSafeRequestBody(schemaName string, r *http.Request, o interface{}) error
ValidateSafeRequestBody reads the request body, validates it against a JSON schema, and unmarshall it. Unlike ValidateRequestBody, it maintains the body in the request object (e.g. to be forwarded via proxy).