servletrest

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2021 License: MIT Imports: 17 Imported by: 1

README

servlet-rest

Opinionated RESTful application scaffolding over servlet/gin-gonic frameworks

Documentation

Index

Constants

View Source
const (
	// ContainerGinEngineID defines the default id used to register the
	// application gin engine instance in the application container.
	ContainerGinEngineID = "servlet.engine"

	// EnvContainerGinEngineID defines the environment variable used to
	// override the default value for the container gin engine id
	EnvContainerGinEngineID = "SERVLET_CONTAINER_GIN_ENGINE_ID"
)
View Source
const (
	// LogMiddlewareChannel defines the channel id to be used when the log
	// middleware sends the logging signal to the logger instance.
	LogMiddlewareChannel = "transport"

	// EnvLogMiddlewareChannel defines the name of the environment
	// variable to be checked for a overriding value for the channel id used
	// on the logging signal call.
	EnvLogMiddlewareChannel = "SERVLET_LOG_MIDDLEWARE_CHANNEL"

	// LogMiddlewareLevel defines the logging level to be used when the log
	// middleware sends the logging signal to the logger instance.
	LogMiddlewareLevel = servlet.INFO

	// EnvLogMiddlewareLevel defines the name of the environment
	// variable to be checked for a overriding value for the logging level
	// used on the logging signal call.
	EnvLogMiddlewareLevel = "SERVLET_LOG_MIDDLEWARE_LEVEL"

	// LogMiddlewareRequestMessage defines the request event logging message to
	// be used when the log middleware sends the logging signal to the logger
	// instance.
	LogMiddlewareRequestMessage = "Request"

	// EnvLogMiddlewareRequestMessage defines the name of the environment
	// variable to be checked for a overriding value for the request event
	// logging message used on the logging signal call
	EnvLogMiddlewareRequestMessage = "SERVLET_LOG_MIDDLEWARE_REQUEST_MESSAGE"

	// LogMiddlewareResponseMessage defines the response event logging message
	// to be used when the log middleware sends the logging signal to the
	// logger instance.
	LogMiddlewareResponseMessage = "Response"

	// EnvLogMiddlewareResponseMessage defines the name of the environment
	// variable to be checked for a overriding value for the response event
	// logging message used on the logging signal call
	EnvLogMiddlewareResponseMessage = "SERVLET_LOG_MIDDLEWARE_RESPONSE_MESSAGE"
)

Variables

This section is empty.

Functions

func NewLogMiddleware

func NewLogMiddleware(p *LogMiddlewareParams) (func(ctx *gin.Context), error)

NewLogMiddleware will instantiate a new middleware that will emit logging signals on a request event and on a response event.

Types

type GinApp

type GinApp struct {
	servlet.App
	// contains filtered or unexported fields
}

GinApp interface used to define the methods of a servlet application.

func NewGinApp

func NewGinApp(params *GinAppParams) *GinApp

NewGinApp used to instantiate a new application.

func (GinApp) Engine

func (a GinApp) Engine() GinEngine

Engine will retrieve the application underlying gin engine.

func (*GinApp) Run

func (a *GinApp) Run(addr ...string) error

Run method will boot the application, if not yet, and the start the underlying gin server.

type GinAppParams

type GinAppParams struct {
	EngineID string
}

GinAppParams defines the application parameters storing structure that will be needed when instantiating a new application

func NewGinAppParams

func NewGinAppParams() *GinAppParams

NewGinAppParams instantiate a new application parameters object.

type GinContext

type GinContext interface {
	Abort()
	AbortWithError(code int, err error) *gin.Error
	AbortWithStatus(code int)
	AbortWithStatusJSON(code int, jsonObj interface{})
	AsciiJSON(code int, obj interface{})
	Bind(obj interface{}) error
	BindHeader(obj interface{}) error
	BindJSON(obj interface{}) error
	BindQuery(obj interface{}) error
	BindUri(obj interface{}) error
	BindWith(obj interface{}, b binding.Binding) error
	BindXML(obj interface{}) error
	BindYAML(obj interface{}) error
	ClientIP() string
	ContentType() string
	Cookie(name string) (string, error)
	Copy() *gin.Context
	Data(code int, contentType string, data []byte)
	DataFromReader(code int, contentLength int64, contentType string, reader io.Reader, extraHeaders map[string]string)
	Deadline() (deadline time.Time, ok bool)
	DefaultPostForm(key, defaultValue string) string
	DefaultQuery(key, defaultValue string) string
	Done() <-chan struct{}
	Err() error
	Error(err error) *gin.Error
	File(filepath string)
	FileAttachment(filepath, filename string)
	FileFromFS(filepath string, fs http.FileSystem)
	FormFile(name string) (*multipart.FileHeader, error)
	FullPath() string
	Get(key string) (value interface{}, exists bool)
	GetBool(key string) (b bool)
	GetDuration(key string) (d time.Duration)
	GetFloat64(key string) (f64 float64)
	GetHeader(key string) string
	GetInt(key string) (i int)
	GetInt64(key string) (i64 int64)
	GetPostForm(key string) (string, bool)
	GetPostFormArray(key string) ([]string, bool)
	GetPostFormMap(key string) (map[string]string, bool)
	GetQuery(key string) (string, bool)
	GetQueryArray(key string) ([]string, bool)
	GetQueryMap(key string) (map[string]string, bool)
	GetRawData() ([]byte, error)
	GetString(key string) (s string)
	GetStringMap(key string) (sm map[string]interface{})
	GetStringMapString(key string) (sms map[string]string)
	GetStringMapStringSlice(key string) (smss map[string][]string)
	GetStringSlice(key string) (ss []string)
	GetTime(key string) (t time.Time)
	HTML(code int, name string, obj interface{})
	Handler() gin.HandlerFunc
	HandlerName() string
	HandlerNames() []string
	Header(key, value string)
	IndentedJSON(code int, obj interface{})
	IsAborted() bool
	IsWebsocket() bool
	JSON(code int, obj interface{})
	JSONP(code int, obj interface{})
	MultipartForm() (*multipart.Form, error)
	MustBindWith(obj interface{}, b binding.Binding) error
	MustGet(key string) interface{}
	Negotiate(code int, config gin.Negotiate)
	NegotiateFormat(offered ...string) string
	Next()
	Param(key string) string
	PostForm(key string) string
	PostFormArray(key string) []string
	PostFormMap(key string) map[string]string
	ProtoBuf(code int, obj interface{})
	PureJSON(code int, obj interface{})
	Query(key string) string
	QueryArray(key string) []string
	QueryMap(key string) map[string]string
	Redirect(code int, location string)
	Render(code int, r render.Render)
	SSEvent(name string, message interface{})
	SaveUploadedFile(file *multipart.FileHeader, dst string) error
	SecureJSON(code int, obj interface{})
	Set(key string, value interface{})
	SetAccepted(formats ...string)
	SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool)
	SetSameSite(samesite http.SameSite)
	ShouldBind(obj interface{}) error
	ShouldBindBodyWith(obj interface{}, bb binding.BindingBody) (err error)
	ShouldBindHeader(obj interface{}) error
	ShouldBindJSON(obj interface{}) error
	ShouldBindQuery(obj interface{}) error
	ShouldBindUri(obj interface{}) error
	ShouldBindWith(obj interface{}, b binding.Binding) error
	ShouldBindXML(obj interface{}) error
	ShouldBindYAML(obj interface{}) error
	Status(code int)
	Stream(step func(w io.Writer) bool) bool
	String(code int, format string, values ...interface{})
	Value(key interface{}) interface{}
	XML(code int, obj interface{})
	YAML(code int, obj interface{})
}

GinContext interface for the gin-gonic context object.

type GinEngine

type GinEngine interface {
	gin.IRoutes
	Delims(left, right string) *gin.Engine
	HandleContext(c *gin.Context)
	LoadHTMLFiles(files ...string)
	LoadHTMLGlob(pattern string)
	NoMethod(handlers ...gin.HandlerFunc)
	NoRoute(handlers ...gin.HandlerFunc)
	Routes() (routes gin.RoutesInfo)
	Run(addr ...string) (err error)
	RunFd(fd int) (err error)
	RunListener(listener net.Listener) (err error)
	RunTLS(addr, certFile, keyFile string) (err error)
	RunUnix(file string) (err error)
	SecureJsonPrefix(prefix string) *gin.Engine
	ServeHTTP(w http.ResponseWriter, req *http.Request)
	SetFuncMap(funcMap template.FuncMap)
	SetHTMLTemplate(templ *template.Template)
}

GinEngine interface for the gin-gonic engine object.

type LogMiddlewareParams

type LogMiddlewareParams struct {
	RequestReader      LogMiddlewareRequestReader
	ResponseReader     LogMiddlewareResponseReader
	Next               gin.HandlerFunc
	Logger             *servlet.Log
	LogChannel         string
	LogLevel           servlet.LogLevel
	LogRequestMessage  string
	LogResponseMessage string
}

LogMiddlewareParams defines the storing structure of the parameters used to configure the logging middleware.

func NewLogMiddlewareParams

func NewLogMiddlewareParams(next gin.HandlerFunc, logger *servlet.Log) (*LogMiddlewareParams, error)

NewLogMiddlewareParams will instantiate a new log middleware parameters instance used to configure a log middleware. If environment variables have been set for the log environment, the returned parameters structure will reflect those values.

type LogMiddlewareRequestReader

type LogMiddlewareRequestReader interface {
	Get(context GinContext) map[string]interface{}
}

LogMiddlewareRequestReader defines the interface of a request reader used in the log middleware.

type LogMiddlewareRequestReaderBase

type LogMiddlewareRequestReaderBase struct{}

LogMiddlewareRequestReaderBase defines the methods of a request context reader used to compose the data to be sent to the logger on a request event.

func NewLogMiddlewareRequestReaderBase

func NewLogMiddlewareRequestReaderBase() *LogMiddlewareRequestReaderBase

NewLogMiddlewareRequestReaderBase will instantiate a new basic request context reader.

func (LogMiddlewareRequestReaderBase) Get

func (r LogMiddlewareRequestReaderBase) Get(context GinContext) map[string]interface{}

Get process the context request and return the data to be signaled to the logger.

type LogMiddlewareRequestReaderJSON

type LogMiddlewareRequestReaderJSON struct {
	// contains filtered or unexported fields
}

LogMiddlewareRequestReaderJSON defines a request reader used to parse a JSON body content.

func NewLogMiddlewareRequestReaderJSON

func NewLogMiddlewareRequestReaderJSON(reader LogMiddlewareRequestReader, model interface{}) (*LogMiddlewareRequestReaderJSON, error)

NewLogMiddlewareRequestReaderJSON will instantiate a new request event context reader JSON decorator used to parse the request body as a JSON and add the parsed content into the logging data.

func (LogMiddlewareRequestReaderJSON) Get

func (r LogMiddlewareRequestReaderJSON) Get(context GinContext) map[string]interface{}

Get process the context request and add the extra bodyJson if the body content can be parsed as JSON.

type LogMiddlewareRequestReaderXML

type LogMiddlewareRequestReaderXML struct {
	// contains filtered or unexported fields
}

LogMiddlewareRequestReaderXML defines a request reader used to parse a XML body content.

func NewLogMiddlewareRequestReaderXML

func NewLogMiddlewareRequestReaderXML(reader LogMiddlewareRequestReader, model interface{}) (*LogMiddlewareRequestReaderXML, error)

NewLogMiddlewareRequestReaderXML will instantiate a new request event context reader XML decorator used to parse the request body as a XML and add the parsed content into the logging data.

func (LogMiddlewareRequestReaderXML) Get

func (r LogMiddlewareRequestReaderXML) Get(context GinContext) map[string]interface{}

Get process the context request and add the extra bodyJson if the body content can be parsed as XML.

type LogMiddlewareResponseReader

type LogMiddlewareResponseReader interface {
	Get(context GinContext, writer LogMiddlewareResponseWriter) map[string]interface{}
}

LogMiddlewareResponseReader defines the interface methods of a response context reader used to compose the data to be sent to the logger on a response event.

type LogMiddlewareResponseReaderBase

type LogMiddlewareResponseReaderBase struct{}

LogMiddlewareResponseReaderBase defines the methods of a response context reader used to compose the data to be sent to the logger on a response event.

func NewLogMiddlewareResponseReaderBase

func NewLogMiddlewareResponseReaderBase() *LogMiddlewareResponseReaderBase

NewLogMiddlewareResponseReaderBase will instantiate a new basic response context reader.

func (LogMiddlewareResponseReaderBase) Get

func (r LogMiddlewareResponseReaderBase) Get(context GinContext, writer LogMiddlewareResponseWriter) map[string]interface{}

Get process the context response and return the data to be signaled to the logger.

type LogMiddlewareResponseReaderJSON

type LogMiddlewareResponseReaderJSON struct {
	// contains filtered or unexported fields
}

LogMiddlewareResponseReaderJSON defines a response reader used to parse a JSON body content.

func NewLogMiddlewareResponseReaderJSON

func NewLogMiddlewareResponseReaderJSON(reader LogMiddlewareResponseReader, model interface{}) (*LogMiddlewareResponseReaderJSON, error)

NewLogMiddlewareResponseReaderJSON will instantiate a new response event context reader JSON decorator used to parse the response body as a JSON and add the parsed content into the logging data.

func (LogMiddlewareResponseReaderJSON) Get

func (r LogMiddlewareResponseReaderJSON) Get(context GinContext, writer LogMiddlewareResponseWriter) map[string]interface{}

Get process the context response and add the extra bodyJson if the body content can be parsed as JSON.

type LogMiddlewareResponseReaderXML

type LogMiddlewareResponseReaderXML struct {
	// contains filtered or unexported fields
}

LogMiddlewareResponseReaderXML defines a response reader used to parse a XML body content.

func NewLogMiddlewareResponseReaderXML

func NewLogMiddlewareResponseReaderXML(reader LogMiddlewareResponseReader, model interface{}) (*LogMiddlewareResponseReaderXML, error)

NewLogMiddlewareResponseReaderXML will instantiate a new response event context reader XML decorator used to parse the response body as a XML and add the parsed content into the logging data.

func (LogMiddlewareResponseReaderXML) Get

func (r LogMiddlewareResponseReaderXML) Get(context GinContext, writer LogMiddlewareResponseWriter) map[string]interface{}

Get process the context response and add the extra bodyJson if the body content can be parsed as XML.

type LogMiddlewareResponseWriter

type LogMiddlewareResponseWriter interface {
	gin.ResponseWriter
	Body() []byte
}

LogMiddlewareResponseWriter defines the interface of a response writer used in the log middleware.

func NewLogMiddlewareResponseWriter

func NewLogMiddlewareResponseWriter(writer gin.ResponseWriter) (LogMiddlewareResponseWriter, error)

NewLogMiddlewareResponseWriter instantiate a new response writer proxy.

type LogMiddlewareResponseWriterBase added in v1.2.0

type LogMiddlewareResponseWriterBase struct {
	gin.ResponseWriter
	// contains filtered or unexported fields
}

LogMiddlewareResponseWriterBase defines the interface of a response writer used in the log middleware.

func (LogMiddlewareResponseWriterBase) Body added in v1.2.0

Body will retrieve the stored bytes given on the previous calls to the Write method.

func (LogMiddlewareResponseWriterBase) Write added in v1.2.0

Write executes the writing the desired bytes into the underlying writer and storing them in the internal buffer.

Jump to

Keyboard shortcuts

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