Documentation ¶
Index ¶
- func Abort(c *gin.Context, code int, message string)
- func CORSHandler() gin.HandlerFunc
- func LoggerHandler(logger logrus.FieldLogger, timeFormat string, utc bool) gin.HandlerFunc
- func New(cfg Config, logger logrus.FieldLogger) *server
- func NewGRPCServer(service interface{}, desc *grpc.ServiceDesc, ...) *grpc.Server
- func NewHTTPServer(cfg Config, handler http.Handler) *http.Server
- func NewLogger(level string, format string) logrus.FieldLogger
- func NoCacheHandler() gin.HandlerFunc
- func NotFoundHandler(c *gin.Context)
- func RequestIDHandler() gin.HandlerFunc
- type Config
- type ErrorResponse
- type Server
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Abort ¶
Abort is a helper function that calls `Abort()` and then `JSON` internally. This method stops the chain, writes the status code and return a JSON body with HTTP status code and error message. It also sets the Content-Type as "application/json".
func CORSHandler ¶
func CORSHandler() gin.HandlerFunc
CORSHandler returns a gin.HandlerFunc (middleware) to enable CORSHandler support to all origins.
func LoggerHandler ¶
func LoggerHandler(logger logrus.FieldLogger, timeFormat string, utc bool) gin.HandlerFunc
LoggerHandler returns a gin.HandlerFunc (middleware) that logs requests using logrus.
Requests with errors are logged using logrus.Error(). Requests without errors are logged using logrus.Info().
It receives:
- A time package format string (e.g. time.RFC3339).
- A boolean stating whether to use UTC time zone or local.
func New ¶
func New(cfg Config, logger logrus.FieldLogger) *server
func NewGRPCServer ¶
func NewGRPCServer(service interface{}, desc *grpc.ServiceDesc, middleware grpc.UnaryServerInterceptor, options []grpc.ServerOption) *grpc.Server
func NewLogger ¶
func NewLogger(level string, format string) logrus.FieldLogger
NewLogger creates a new Logger. Configuration should be set by changing level (eg.: panic, fatal, error, warn, info, debug) format (eg.: text, json).
func NoCacheHandler ¶
func NoCacheHandler() gin.HandlerFunc
NoCacheHandler is a middleware func for setting the Cache-Control to no-cache.
func NotFoundHandler ¶
NotFoundHandler is a helper function that calls Server.Abort.
func RequestIDHandler ¶
func RequestIDHandler() gin.HandlerFunc
RequestIDHandler injects a special header X-Request-Id to response headers that could be used to track incoming requests for monitoring/debugging purposes.
Types ¶
type Config ¶
type Config struct { // MaxHeaderBytes can be used to override the default of 1<<20. MaxHeaderBytes int `json:"max_header_bytes"` // ReadTimeout can be used to override the default http Server timeout of 20s. // The string should be formatted like a time.Duration string. ReadTimeout time.Duration `json:"read_timeout"` // WriteTimeout can be used to override the default http Server timeout of 20s. // The string should be formatted like a time.Duration string. WriteTimeout time.Duration `json:"write_timeout"` // IdleTimeout can be used to override the default http Server timeout of 120s. // The string should be formatted like a time.Duration string. IdleTimeout time.Duration `json:"idle_timeout"` // ShutdownTimeout can be used to override the default http Server Shutdown timeout // of 5m. ShutdownTimeout time.Duration `json:"shutdown_timeout"` // HTTPPort is the port the Server implementation will serve HTTP over. // The default is 8080 HTTPPort int `json:"http_port"` // RPCPort is the port the Server implementation will serve RPC over. // The default is 8081. RPCPort int `json:"rpc_port"` // Enable pprof Profiling. Off by default. EnablePProf bool `json:"enable_pprof"` // LoggerHandler level (eg.: panic, fatal, error, warn, info, debug) LoggerLevel string `json:"logger_level"` // LoggerHandler format (ex.: text, json) LoggerFormat string `json:"logger_format"` }
Config holds info required to configure a Server.Server.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a generic Server configuration.
type ErrorResponse ¶
func (*ErrorResponse) Error ¶
func (er *ErrorResponse) Error() string