Skip to main content

custom 🔌

You can extend httpin by adding your custom directives.

Firstly you should understand the concepts of Directive and Directive Executor.

To add your custom directive, you need to:

1. Create a Directive Executor

by implementing the httpin.DirectiveExecutor interface:

func toLower(ctx *DirectiveContext) error {
if ctx.ValueType.Kind() != reflect.String {
return errors.New("not a string")
}

currentValue := ctx.Value.Elem().String()
newValue := strings.ToLower(currentValue)
ctx.Value.Elem().SetString(newValue)
return nil
}

// Adapt to the httpin.DirectiveExecutor interface.
var MyLowercaseDirectiveExecutor = httpin.DirectiveExecutorFunc(toLower)

2. Register your Directive to httpin

by calling httpin.RegisterDirectiveExecutor:

func init() {
httpin.RegisterDirectiveExecutor("to_lowercase", MyLowercaseDirectiveExecutor, nil)
}

NB: make sure to register your directive executor before you start the server.

3. Use your Directive

type LoginFormInput struct {
Login string `in:"form=login;required;to_lowercase"`
Pass string `in:"form=login;required"`
}