Skip to content
Flecs v4.2
rest.h
Go to the documentation of this file.
1/**
2 * @file addons/rest.h
3 * @brief REST API addon.
4 *
5 * A small REST API that uses the HTTP server and JSON serializer to provide
6 * access to application data for remote applications.
7 *
8 * A description of the API can be found in docs/FlecsRemoteApi.md.
9 */
10
11#ifdef FLECS_REST
12
13/**
14 * @defgroup c_addons_rest Rest
15 * @ingroup c_addons
16 * REST API for querying and mutating entities.
17 *
18 * @{
19 */
20
21/* Used for the HTTP server */
22#ifndef FLECS_HTTP
23#define FLECS_HTTP
24#endif
25
26/* Used for building the JSON replies */
27#ifndef FLECS_JSON
28#define FLECS_JSON
29#endif
30
31/* For the REST system */
32#ifndef FLECS_PIPELINE
33#define FLECS_PIPELINE
34#endif
35
36#ifndef FLECS_REST_H
37#define FLECS_REST_H
38
39#ifdef __cplusplus
40extern "C" {
41#endif
42
43/** Default port for the REST API server. */
44#define ECS_REST_DEFAULT_PORT (27750)
45
46/** Component that instantiates the REST API. */
47FLECS_API extern const ecs_entity_t ecs_id(EcsRest);
48
49/** Private REST data. */
50typedef struct {
51 ecs_world_t *world; /**< The world. */
52 ecs_http_server_t *srv; /**< HTTP server instance. */
53 int32_t rc; /**< Reference count. */
54 ecs_map_t cmd_captures; /**< Map of command captures. */
55 double last_time; /**< Last processing time. */
56 bool readonly; /**< Refuse requests that modify the world. */
58
59/** Component that creates a REST API server when instantiated. */
60typedef struct {
61 uint16_t port; /**< Port of server (optional, default = 27750). */
62 char *ipaddr; /**< Interface address (optional, default = 0.0.0.0). */
63 ecs_rest_ctx_t *impl; /**< Private implementation data. */
64 bool readonly; /**< Refuse requests that modify the world (optional, default = false). */
65} EcsRest;
66
67/** Create HTTP server for REST API.
68 * This allows for the creation of a REST server that can be managed by the
69 * application without using Flecs systems.
70 *
71 * @param world The world.
72 * @param desc The HTTP server descriptor.
73 * @return The HTTP server, or NULL if failed.
74 */
75FLECS_API
77 ecs_world_t *world,
78 const ecs_http_server_desc_t *desc);
79
80/** Clean up REST HTTP server.
81 * The server must have been created with ecs_rest_server_init().
82 *
83 * @param srv The server to destroy.
84 */
85FLECS_API
88
89/** REST module import function.
90 * Usage:
91 * @code
92 * ECS_IMPORT(world, FlecsRest)
93 * @endcode
94 *
95 * @param world The world.
96 */
97FLECS_API
99 ecs_world_t *world);
100
101#ifdef __cplusplus
102}
103#endif
104
105#endif
106
107/** @} */
108
109#endif
FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
Component ID for EcsDocDescription.
struct ecs_http_server_t ecs_http_server_t
HTTP server.
Definition http.h:48
FLECS_API ecs_http_server_t * ecs_rest_server_init(ecs_world_t *world, const ecs_http_server_desc_t *desc)
Create HTTP server for REST API.
FLECS_API void ecs_rest_server_fini(ecs_http_server_t *srv)
Clean up REST HTTP server.
FLECS_API void FlecsRestImport(ecs_world_t *world)
REST module import function.
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:396
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
Definition flecs.h:440
Component that creates a REST API server when instantiated.
Definition rest.h:60
bool readonly
Refuse requests that modify the world (optional, default = false).
Definition rest.h:64
uint16_t port
Port of server (optional, default = 27750).
Definition rest.h:61
char * ipaddr
Interface address (optional, default = 0.0.0.0).
Definition rest.h:62
ecs_rest_ctx_t * impl
Private implementation data.
Definition rest.h:63
Used with ecs_http_server_init().
Definition http.h:124
Private REST data.
Definition rest.h:50
int32_t rc
Reference count.
Definition rest.h:53
ecs_http_server_t * srv
HTTP server instance.
Definition rest.h:52
double last_time
Last processing time.
Definition rest.h:55
ecs_world_t * world
The world.
Definition rest.h:51
ecs_map_t cmd_captures
Map of command captures.
Definition rest.h:54
bool readonly
Refuse requests that modify the world.
Definition rest.h:56