Example #1
0
func run() int {
	robot, err := hal.NewRobot()
	if err != nil {
		hal.Logger.Error(err)
		return 1
	}

	// Or define them inside another function...
	fooHandler := hal.Respond(`foo`, func(res *hal.Response) error {
		return res.Send("BAR")
	})

	tableFlipHandler := &hal.BasicHandler{
		Method:  hal.HEAR,
		Pattern: `tableflip`,
		Run: func(res *hal.Response) error {
			return res.Send(`(╯°□°)╯︵ ┻━┻`)
		},
	}

	robot.Handle(
		pingHandler,
		fooHandler,
		tableFlipHandler,

		// Or stick them in an entirely different package, and reference them
		// exactly in the way you would expect.
		handler.Ping,

		// Or use a hal.BasicHandler structure complete with usage...
		&hal.BasicHandler{
			Method:  hal.RESPOND,
			Pattern: `SYN`,
			Usage:   `hal syn - replies with "ACK"`,
			Run: func(res *hal.Response) error {
				return res.Reply("ACK")
			},
		},

		// Or even inline!
		hal.Hear(`yo`, func(res *hal.Response) error {
			return res.Send("lo")
		}),
	)

	if err := robot.Run(); err != nil {
		hal.Logger.Error(err)
		return 1
	}
	return 0
}
Example #2
0
File: echo.go Project: thuvh/hal
package handler

import (
	"github.com/danryan/hal"
)

// Echo is an example of a simple handler.
var Echo = hal.Respond(`echo (.+)`, func(res *hal.Response) error {
	return res.Reply(res.Match[1])
})