Skip to content

Endpoint

Johnwii edited this page Jun 23, 2020 · 3 revisions

Endpoint is where the actual business logic is located. Used to focus on specific business logic. The definition of an endpoint is inextricably linked to the access address of an endpoint. Through this convention, we can easily define an endpoint and automatically expose it, or locate the relevant that one by address.

Endpoint is a public method under the controller, that is, all public methods specified by Golang are legal endpoints. An endpoint allows multiple parameters, but only one structure and one structure reference are allowed in all parameters. The other basic type parameters will be presented in the address and sent to the method in actual work in order.

Body & Query

The controller provides automatic serialization and deserialization, and the structure in the node will be serialized according to the query processing. The structure reference will be read from the body.

If you set a body param, the body of request will be read by default if Content-Type indicated an existed deserialiser. You can access Controller.Context().Body() to get it(not for manual reading).

In general, the built-in JSON serializer will be called without special customization serialization and deserialization. The node with the body set at the same time is registered as the POST method by default, and vice versa. If you need to set it manually, you need to write all the private fields that are expected to support RESTful methods starting with method to the parameters of the body or query.

If you need to manually set up a private serialiser and deserialiser, see the Hook#Serialiser.

URI

In general, the controller's registered domain is its name itself. The general rules are:

/controller_name/controller_preconditioner_parameters/method_name/method_parameters

The controller precondition parameters and method parameters are placeholders. For example, the preceding preconditions have a string, so the {string} placeholder will appear. The more parameters there are, the more placeholders there are. Here is a list of placeholders:

Type Placeholder
Integer {digits}
Floating {float}
Boolean {bool}
String {string}

the string is the fall of all methods. If the URI address can be directly matched by the original word, the placeholder lookup logic will not be entered.

Default

If the root access is not defined in the Endpoint in the routing table, the Index method to be registered will become the root access. However, it's worth noting that if the Index method contains parameters, it may not be able to register properly.

Careful! If you are trying to make a complex route table, it's better to reduce ambiguity definitions and equivalent definitions. When registering a controller, important controllers or rule avoidance class controllers need to be registered in advance. As a developer, I don't recommend using this type of behavior that may induce unexpected access on a large scale.

Reply

The response is in the form of an explicit response and an implicit response:

  • The developer can directly call the Controller.Reply method to reply the data to the requesting client. You must specify a status code when replying but you can have no body returned.
  • It is also possible to provide a return value when the endpoint is declared. The first value of the return value(s) will be replied to the requesting client by the WebAPI. If the return value type is non-Replyable type(you might find some useful information about these interface definition from hook page, webapi.Reply is default implementation), the status code will be 200, the body is the serialized value of the value object (except for the string and []byte). Otherwise, Reply.StatusCode() will be used as the status code, and Reply.Data() will reply as the body.

Demo

All these demo is registered with lowercase uri configuration.

Fragment 1

func (controller *passage) Index(query struct {
    RequestSource string `json:"rs"`
}) *webapi.Reply {
    return &webapi.Reply {
        Status: http.StatusOK,
        Body: controller.article,
    }
}

This method will be registered as /passage/{digits}/ and /passage/{digits}/index. And client will receive a controller.Article object with silence.

Fragment 2

func (controller *passage) Save(operation string, body *struct {
    controller.Article `options:"get,put"`
}) {
    //db operation
    controller.Reply(http.StatusAccepted)
}

Although there is no return value, the client will receive a response that the request is accepted when the operation is successful. Note that this method will be registered as /passage/{digits}/save/{string}, methods are POST and PUT.

Clone this wiki locally