Skip to content

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.

go
package controllers

import (
    "github.com/sev-2/raiden"
)

type HelloWorldRequest struct {
}

type HelloWorldResponse struct {
    Message string `json:"message"`
}

type HelloWorldController struct {
    raiden.ControllerBase
    Http    string `path:"/hello/{name}" type:"custom"`
    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. You can define a routing on a controller. Here is the example of hello controller.

go
import "github.com/sev-2/raiden"

package controllers

type HelloWorldController struct {
    raiden.ControllerBase
    Http    string `path:"/hello/{name}" type:"custom"`
    Payload *HelloWorldRequest
    Result  HelloWorldResponse
}

As you can see, the routing are defined on path:"/hello/{name}". By runing raiden generate, that route will auto-generate to router directory.

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.

go
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 of type: custom, rest, rpc — that you can define on controller's struct.

go
type HelloWorldController struct {
    raiden.ControllerBase
    Http    string `path:"/hello/{name}" type:"custom"`
    Payload *HelloWorldRequest
    Result  HelloWorldResponse
}

INFO

The rpc type only available for Post verb.

Released under the MIT License.