Controller
Controller responsible for directing and coordinating the flow of data and operations within an application. The controller are defined in controllers directory. You can define the payload request and response in a controller.
Here is the default Raiden's controller.
package controllers
import (
"github.com/sev-2/raiden"
)
type HelloWorldRequest struct {
}
type HelloWorldResponse struct {
Message string `json:"message"`
}
type HelloWorldController struct {
raiden.ControllerBase
Payload *HelloWorldRequest
Result HelloWorldResponse
}
func (c *HelloWorldController) Get(ctx raiden.Context) error {
c.Result.Message = "Welcome to raiden"
return ctx.SendJson(c.Result)
}Routing
All routings are automatically generated by controllers based on their location within the controllers directory. The folder structure directly dictates the URL path.
Static Routes
For example, a controller located at controllers/status.go will automatically be accessible at the /status endpoint.
Dynamic Routes
Folders prefixed with an underscore (_) denote dynamic route segments. The name after the underscore becomes the parameter name, which can be accessed within the controller using ctx.GetParam("parameterName").
For example, a controller located at controllers/rest/v1/users/_id/custom.go will be accessible at /rest/v1/users/{id}. Inside the controller, you can retrieve the id like this:
id := ctx.GetParam("id").(string)HTTP Verb
You can use Get, Post, Put, Patch, Delete, Options, or Head on function name to use specific HTTP verb request.
For example, if you want to use Post and Delete request.
func (c *HelloWorldController) Post(ctx raiden.Context) raiden.Presenter {
c.Result.Message = "Data has been inserted."
return ctx.SendJson(c.Result)
}
func (c *HelloWorldController) Delete(ctx raiden.Context) raiden.Presenter {
c.Result.Message = "Data has been deleted."
return ctx.SendJson(c.Result)
}Type
There are several options for the controller type: custom, rest, rpc. These types influence the controller's behavior and generated functionalities, while the route itself is determined by the folder structure.
For example, a custom type controller:
type HelloWorldController struct {
raiden.ControllerBase
Payload *HelloWorldRequest
Result HelloWorldResponse
}INFO
The rpc type only available for Post verb.