Skip to content

Controller

Johnwii.Chang edited this page Jun 28, 2019 · 1 revision

Before start using it, you may want to understand the related concepts and build a technical map combined with existing skills in your mind to reduce the sense of alienation. WebAPI is a library similar to ASP.NET Core, if you are a related developer, or if you are a technology-like user, this article will be very simple and easy to understand.

Controller

The Controller is responsible for processing the request distributed by the HTTP Server. It processes the data requested by the user through the business processing layer and encapsulates it into a Model, and then returns the Model to the corresponding View for display.

Definition

The definition of the Controller is simple and easy. Just define a new structure and set the webapi.Controller interface to its field. In addition, the controller can have its own private field, just the storage shared data between the package-level controllers. For example, we create a controller called passage for the CMS (Content Management System):

type passage struct {
    webapi.Controller

    //shared private fields
    logged    *models.User
    article   *models.Article
}

In the definition, the controller passage has two private fields in addition to its own controller, one of which is the currently logged in user and the other is the entity of the article. All controller methods have unconditional access to these fields and this is the best way to pass data between methods of controller.

Precondition

In the process of using, we often need to deal with some preconditions before entering the specific business method, to confirm the legality of the operation or to identify the target object of the current operation. The simple method that is generally used is to use Middleware to solve the problem, but the data of the middleware can only be attached to the context, and there is no specific type. If there is a type mismatch, or some data is unexpected, it may lead to unpredictable consequences. In order to solve this problem, we propose the concept of Precondition. Through preconditioners, developers can initialize controller objects before entering specific methods, so that the business can return to the business.

In the above example, we can't directly use the internal private fields. It can be seen that the logged field is obviously to identify the currently logged in user, but if you do not initialize it, you will definitely get a panic error no doubt. To solve this problem, you can give a preconditioner to Controller, the definition of precondition is a function which named Init and return an error only:

func (controller *passage) Init(id string) (err error) {
    controller.logged, err = getUserFromHeader(controller.Context().GetRequest().Header)
    if err == nil {
        controller.article, err = service.Articles.Find(id)
    }
    return
}

This precondition is called before all other methods, meaning that all methods of the controller can directly access the logged in user entity and the currently processed target object.

Tips:

  1. Precondition may affect the URL of the registered node. For example, the Init(string) function above will have a prefix of /article/{string}/ at all its method addresses. For more placeholders in uri, you need jump to related content after this section.
  2. If the function is not defined by Init and returned with an error, the function will be registered as a normal node instead of a precondition.
Clone this wiki locally