server

package
v0.0.0-...-9eee1bd Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2023 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abort

func Abort(c *gin.Context, code int, message string)

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:

  1. A time package format string (e.g. time.RFC3339).
  2. 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 NewHTTPServer

func NewHTTPServer(cfg Config, handler http.Handler) *http.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

func NotFoundHandler(c *gin.Context)

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

type ErrorResponse struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

func (*ErrorResponse) Error

func (er *ErrorResponse) Error() string

type Server

type Server interface {
	Run() error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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