Handlers - lucasmenendez/shgf GitHub Wiki

Handlers are functions that are executed when some client makes a request to the server. Handlers generally respond under conditions such as URL or method matching those determined by the developer.

Main Handler

Main handler function represents the main entry point to the route assigned to it. It must be a shgf.Handler. You can check out the documentation about the shgf.Handler in its GoDoc page.

func req(ctx *shgf.Context) (res *shgf.Response) {
	var err error
	if res, err = shgf.NewResponse(200, "Hello world!"); err != nil {
		res, _ = shgf.NewResponse(500, err)
	}

	return res
}

Middleware Handler

Middleware function is executed before the main handler function but provides entire access to request data and functions. Commonly used for validating request data before process the main function, for example in a process of authentication. In that case, the middleware is used for parsing the route params calling ctx.ParseParams() function. Like the Main Handler, it must be a shgf.Handler.

func mid(ctx *shgf.Context) (res *shgf.Response) {
	if err := ctx.ParseParams(); err != nil {
		res, _ := shgf.NewResponse(500, err)
		return res
	}

	return ctx.Next()
}