REST API
Corteza uses standard Go’s HTTP servers and handlers with chi to handle request routing.
Request handling
-
request handlers convert the request into a proper structure,
-
the service initializes the action parameters (used for the action log),
-
requested resources are loaded and access permissions are checked,
-
the appropriate operation is performed,
-
the request handler prepares the response, most commonly in the JSON format.
API endpoints
Endpoints are defined with */rest.yaml files (/(compose|system|federation|automation)/rest.yaml).
The code generation tool uses these rest.yaml files to generate the boilerplate request and response handling logic.
The rest.yaml files are used to generate the boilerplate code to handle request and response formatting (rest/request/.go and rest/handlers/.go).
You can run the code generation tool with make codegen CLI command.
Handlers read and normalize request data into request structures and pass them on to controllers. Controllers are functions that handle a request and route it forward to internal service, and format the final output.
|
Controllers were initially developed to solve CRUD operations.
Further development of Corteza platform outgrew that, so you may notice some peculiar patterns (returning |
API documentation
API documentation is provided by Swagger using the open API. The open API format is generated using the openapi3-converter tool.
The converter tool generates a series of {resource}.yaml files in the /swagger directory.
If corteza-server is in the same directory as the converter tool, the files are moved to the corteza-server/docs directory and the /swagger directory.
To generate the static HTML API documentation served by the corteza-server, run the make docs command within the corteza-server repository.
The static HTML files are generated in the corteza-server/docs/ directory.
Extending the tool
The entirety of the converting process is done in the openapi3-converter/tools/convert.js.
Adding new definitions
Adding a new definition adds a new entry to the const namespaces = […] array.
Rebuild the open API definitions using the yarn convert:yaml command.
const namespaces = [
{
path: `${path}/system/rest.yaml`,
namespace: 'system',
className: 'System',
},
{
path: `${path}/compose/rest.yaml`,
namespace: 'compose',
className: 'Compose',
},
{
path: `${path}/federation/rest.yaml`,
namespace: 'federation',
className: 'Federation',
},
{
path: `${path}/foo/rest.yaml`,
namespace: 'foo',
className: 'Foo',
},
]