Skip to main content

Customization

tip

Below, property refers to Slim instance property.

Debug

The Slim instance property Slim#Debug can be used to enable or disable debug mode.

ValueDescription
trueEnable debug mode
falseDisable debug mode

Filesystem

The property Slim#Filesystem is used for static file serving, defaulting to the working directory of our program.

Custom filesystem
s.Filesystem = os.DirFS("/path/to/entry")

Validator

The property Slim#Validator can be used to register a validator to perform data validation based on request payload.

Custom validator
func newCustomValidator() slim.Validator {
// ...
}

s.Validator = newCustomValidator()

Custom Binder

The property Slim#Binder can be used to register custom data binding logic to parse data submitted during requests.

Custom data binding
func newCustomBinder() slim.Binder {
// ...
}

s.Binder = newCustomBinder()

Custom Codec

We can customize codec logic through the following two properties:

  • Property Slim#JSONCodec for encoding and decoding JSON data
  • Property Slim#XMLCodec for encoding and decoding XML data
Custom codec logic
func newJSONCodec() slim.Codec {}
func newXMLCodec() slim.Codec {}

s.JSONCodec = newJSONCodec()
s.XMLCodec = newXMLCodec()

Renderer

The property Slim#Renderer can be used to register a renderer for template rendering.

Custom template renderer
func newRenderer() slim.Renderer {
// ...
}

s.Renderer = newRenderer()

Error Handler

The property Slim#ErrorHandler can be used to register a custom HTTP error handler.

Custom error handler
s.ErrorHandler = func(c slim.Context, err error) {
// handling the error
c.JSON(http.StatusInternalServerError, slim.Map{
"message": err.Error(),
"stack": stackFor(err), // for debug
})
}