Csilk 0.2.1
A lightweight, high-performance C HTTP web framework
Loading...
Searching...
No Matches
csilk.h
Go to the documentation of this file.
1
13#ifndef CSILK_H
14#define CSILK_H
15
16#include <setjmp.h>
17#include <stddef.h>
18#include <stdint.h>
19#include <uv.h>
20
21#include "cJSON.h"
22
23/* Forward declarations to break circular dependency:
24 csilk/drivers/db.h includes csilk.h, so types defined in db.h
25 must be forward-declared here before the include. */
26typedef struct csilk_db_pool_s csilk_db_pool_t;
27
28#include "csilk/drivers/ai.h"
30#include "csilk/drivers/db.h"
31#include "csilk/drivers/perm.h"
33
38#define CSILK_VERSION "0.2.3"
39
45#define CSILK_MAX_PARAMS 20
46
53#define CSILK_MAX_STORAGE 64
54
59#define CSILK_STATUS_CONTINUE 100
60#define CSILK_STATUS_SWITCHING_PROTOCOLS 101
61#define CSILK_STATUS_OK 200
62#define CSILK_STATUS_CREATED 201
63#define CSILK_STATUS_NO_CONTENT 204
64#define CSILK_STATUS_PARTIAL_CONTENT 206
65#define CSILK_STATUS_MOVED_PERMANENTLY 301
66#define CSILK_STATUS_FOUND 302
67#define CSILK_STATUS_NOT_MODIFIED 304
68#define CSILK_STATUS_TEMPORARY_REDIRECT 307
69#define CSILK_STATUS_BAD_REQUEST 400
70#define CSILK_STATUS_UNAUTHORIZED 401
71#define CSILK_STATUS_PAYMENT_REQUIRED 402
72#define CSILK_STATUS_FORBIDDEN 403
73#define CSILK_STATUS_NOT_FOUND 404
74#define CSILK_STATUS_METHOD_NOT_ALLOWED 405
75#define CSILK_STATUS_REQUEST_TIMEOUT 408
76#define CSILK_STATUS_CONFLICT 409
77#define CSILK_STATUS_GONE 410
78#define CSILK_STATUS_PAYLOAD_TOO_LARGE 413
79#define CSILK_STATUS_RANGE_NOT_SATISFIABLE 416
80#define CSILK_STATUS_URI_TOO_LONG 414
81#define CSILK_STATUS_UNSUPPORTED_MEDIA_TYPE 415
82#define CSILK_STATUS_TOO_MANY_REQUESTS 429
83#define CSILK_STATUS_INTERNAL_SERVER_ERROR 500
84#define CSILK_STATUS_NOT_IMPLEMENTED 501
85#define CSILK_STATUS_BAD_GATEWAY 502
86#define CSILK_STATUS_SERVICE_UNAVAILABLE 503
87#define CSILK_STATUS_GATEWAY_TIMEOUT 504
97typedef struct csilk_ctx_s csilk_ctx_t;
98
109typedef struct {
114 void (*set)(csilk_ctx_t* c, const char* key, void* value);
119 void* (*get)(csilk_ctx_t* c, const char* key);
122 void (*clear)(csilk_ctx_t* c);
124
134typedef void (*csilk_handler_t)(csilk_ctx_t* c);
135
145typedef struct csilk_header_s {
146 char* key;
148 char* value;
150 size_t key_len;
151 size_t value_len;
152 struct csilk_header_s* next;
155
163#ifndef CSILK_HEADER_BUCKETS
164#define CSILK_HEADER_BUCKETS 64
165#endif
166
176typedef struct csilk_header_map_s {
180
188typedef struct {
189 char* method;
190 char* path;
192 char* body;
193 size_t body_len;
194 csilk_header_map_t headers;
196 csilk_header_map_t query_params;
197 csilk_header_map_t form_params;
200
208typedef struct {
209 int status;
210 const char* body;
212 size_t body_len;
213 csilk_header_map_t headers;
214 int body_is_managed;
217
227typedef struct {
228 char* key;
229 char* value;
231
242typedef struct csilk_arena_s csilk_arena_t;
243
251const char* csilk_get_method(csilk_ctx_t* c);
252
261const char* csilk_get_path(csilk_ctx_t* c);
262
275const char* csilk_get_body(csilk_ctx_t* c, size_t* out_len);
276
286size_t csilk_get_body_len(csilk_ctx_t* c);
287
296int csilk_is_websocket(csilk_ctx_t* c);
297
306void csilk_ws_join_room(csilk_ctx_t* c, const char* room_name);
307
311void csilk_ws_leave_room(csilk_ctx_t* c, const char* room_name);
312
317void csilk_ws_broadcast_room(csilk_ctx_t* c, const char* room_name, const char* message);
318
327void csilk_set_websocket(csilk_ctx_t* c, int is_websocket);
328
337int csilk_is_sse(csilk_ctx_t* c);
338
348void csilk_set_sse(csilk_ctx_t* c, int is_sse);
349
359int csilk_is_aborted(csilk_ctx_t* c);
360
373 csilk_ctx_t* c,
374 void (*callback)(csilk_ctx_t* c, const uint8_t* payload, size_t len, int opcode));
375
378 csilk_ctx_t* c,
379 void (*callback)(csilk_ctx_t* c, const uint8_t* payload, size_t len, int opcode));
380
387void (*csilk_get_on_ws_message(csilk_ctx_t* c))(csilk_ctx_t* c,
388 const uint8_t* payload,
389 size_t len,
390 int opcode);
391
402const char* csilk_get_request_id(csilk_ctx_t* c);
403
414csilk_arena_t* csilk_get_arena(csilk_ctx_t* c);
415
422int csilk_get_status(csilk_ctx_t* c);
423
435void csilk_set_async(csilk_ctx_t* c, int is_async);
436
443int csilk_is_async(csilk_ctx_t* c);
444
451int csilk_get_handler_index(csilk_ctx_t* c);
452
459void csilk_set_request_id(csilk_ctx_t* c, const char* id);
460
470uv_work_t* csilk_get_work_req(csilk_ctx_t* c);
471
484void csilk_set_file_response(csilk_ctx_t* c, int fd, size_t offset, size_t size);
485
492int csilk_get_file_fd(csilk_ctx_t* c);
493
500const char* csilk_ctx_get_handler_path(csilk_ctx_t* c);
501
508const char* csilk_ctx_get_handler_perm_required(csilk_ctx_t* c);
509
516const char* csilk_ctx_get_handler_perm_resource(csilk_ctx_t* c);
517
532void csilk_set_response_body(csilk_ctx_t* c, const char* body, size_t len, int managed);
533
534const char* csilk_get_response_body(csilk_ctx_t* c, size_t* out_len);
535
547void csilk_redirect(csilk_ctx_t* c, int status, const char* location);
548
557void csilk_redirect_simple(csilk_ctx_t* c, const char* url);
558
573void csilk_set(csilk_ctx_t* c, const char* key, void* value);
574
584void* csilk_get(csilk_ctx_t* c, const char* key);
585
596void csilk_next(csilk_ctx_t* c);
597
607void csilk_abort(csilk_ctx_t* c);
608
615void csilk_status(csilk_ctx_t* c, int status);
616
628void csilk_string(csilk_ctx_t* c, int status, const char* msg);
629
641const char* csilk_get_param(csilk_ctx_t* c, const char* key);
642
649int csilk_get_params_count(csilk_ctx_t* c);
650
658const char* csilk_get_param_key(csilk_ctx_t* c, int index);
659
667const char* csilk_get_param_value(csilk_ctx_t* c, int index);
668
677const char* csilk_get_header(csilk_ctx_t* c, const char* key);
678
687const char* csilk_get_response_header(csilk_ctx_t* c, const char* key);
688
700const char* csilk_get_query(csilk_ctx_t* c, const char* key);
701
711void csilk_parse_form_urlencoded(csilk_ctx_t* c);
712
724const char* csilk_get_form_field(csilk_ctx_t* c, const char* key);
725
735void csilk_set_request_header(csilk_ctx_t* c, const char* key, const char* value);
736
747void csilk_set_header(csilk_ctx_t* c, const char* key, const char* value);
748
760void csilk_add_header(csilk_ctx_t* c, const char* key, const char* value);
761
771void csilk_ctx_cleanup(csilk_ctx_t* c);
772
783csilk_arena_t* csilk_arena_new(size_t default_chunk_size);
784
796void* csilk_arena_alloc(csilk_arena_t* arena, size_t size);
797
806char* csilk_arena_strdup(csilk_arena_t* arena, const char* s);
807
817char* csilk_arena_strndup(csilk_arena_t* arena, const char* s, size_t n);
818
827
838void csilk_arena_get_stats(csilk_arena_t* arena, size_t* total_size, size_t* total_used);
839
852void csilk_recovery_handler(csilk_ctx_t* c);
853
863void csilk_panic(csilk_ctx_t* c);
864
882
889typedef struct {
890 csilk_log_level_t level;
892 const char* file_path;
893 size_t max_file_size;
895 int use_colors;
897 int json_format;
900
905
914 csilk_log_level_t lv, const char* file, int line, const char* func, const char* fmt, ...);
915
917void csilk_log_close();
918
934 const char* file,
935 int line,
936 const char* func,
937 cJSON* extra,
938 const char* fmt,
939 ...);
940
943int csilk_log_is_json(void);
944
947void csilk_log_set_request_id(const char* request_id);
948
963cJSON* csilk_log_make_kv(const char* key, ...);
964
969#define CSILK_LOG_T(...) \
970 _csilk_log_internal(CSILK_LOG_TRACE, __FILE__, __LINE__, __func__, __VA_ARGS__)
972#define CSILK_LOG_D(...) \
973 _csilk_log_internal(CSILK_LOG_DEBUG, __FILE__, __LINE__, __func__, __VA_ARGS__)
975#define CSILK_LOG_I(...) \
976 _csilk_log_internal(CSILK_LOG_INFO, __FILE__, __LINE__, __func__, __VA_ARGS__)
978#define CSILK_LOG_W(...) \
979 _csilk_log_internal(CSILK_LOG_WARN, __FILE__, __LINE__, __func__, __VA_ARGS__)
981#define CSILK_LOG_E(...) \
982 _csilk_log_internal(CSILK_LOG_ERROR, __FILE__, __LINE__, __func__, __VA_ARGS__)
984#define CSILK_LOG_F(...) \
985 _csilk_log_internal(CSILK_LOG_FATAL, __FILE__, __LINE__, __func__, __VA_ARGS__)
986
992#define CSILK_LOG_STRUCT(level, extra, ...) \
993 _csilk_log_structured(level, __FILE__, __LINE__, __func__, extra, __VA_ARGS__)
999void csilk_logger_handler(csilk_ctx_t* c);
1000
1004void csilk_request_id_middleware(csilk_ctx_t* c);
1005
1009void csilk_health_check_handler(csilk_ctx_t* c);
1010
1014void csilk_ready_check_handler(csilk_ctx_t* c);
1015
1023typedef struct {
1024 const char* allow_origin;
1026 const char* allow_methods;
1028 const char* allow_headers;
1030 int allow_credentials;
1032 int max_age;
1035
1047void csilk_cors_middleware(csilk_ctx_t* c, const csilk_cors_config_t* config);
1048
1058void csilk_rate_limit_middleware(csilk_ctx_t* c, int limit);
1059
1069void csilk_csrf_middleware(csilk_ctx_t* c);
1070
1074typedef struct {
1077 uint64_t auth_failures;
1079
1088
1091
1102int csilk_csrf_generate_token(char* buf, size_t buf_size);
1103
1111typedef struct csilk_server_config_s {
1112 unsigned int idle_timeout_ms;
1115 unsigned int read_timeout_ms;
1117 unsigned int write_timeout_ms;
1119 unsigned int request_timeout_ms;
1122 size_t max_body_size;
1124 size_t max_header_size;
1126 size_t max_url_size;
1127 size_t max_headers_count;
1129 int max_connections;
1131 int listen_backlog;
1132 int tcp_nodelay;
1134 int tcp_keepalive;
1136 int worker_threads;
1139 /* TLS configuration */
1140 int enable_tls;
1142 char* tls_cert_file;
1144 char* tls_key_file;
1146 char* tls_ca_file;
1148 int tls_verify_peer;
1151
1158typedef struct {
1159 int port;
1160 csilk_server_config_t server;
1161 csilk_log_config_t logger;
1162 struct {
1163 int enable;
1164 csilk_cors_config_t config;
1165 } cors;
1166 struct {
1167 int enable;
1168 int requests_per_minute;
1169 } rate_limit;
1170 struct {
1171 int enable;
1172 char* root_dir;
1174 char* prefix;
1175 } static_files;
1176 struct {
1177 int enable_logger;
1179 int enable_recovery;
1181 int enable_csrf;
1182 int enable_auth;
1183 char* auth_token;
1185 } middleware;
1186 struct {
1187 char* driver;
1188 char* model;
1189 char* api_key;
1190 char* base_url;
1191 } ai;
1192 struct {
1193 int enable;
1194 char* driver;
1195 } cipher;
1197
1209int csilk_load_config(const char* yaml_path, csilk_config_t* config);
1210
1222int csilk_config_validate(const csilk_config_t* config, const char** error_msg);
1223
1233void csilk_config_free(csilk_config_t* config);
1234
1244typedef int (*csilk_auth_validator_t)(const char* token);
1245
1257void csilk_auth_middleware(csilk_ctx_t* c, csilk_auth_validator_t validator);
1258
1274void csilk_static(csilk_ctx_t* c, const char* root_dir);
1275
1285void csilk_file(csilk_ctx_t* c, const char* file_path);
1286
1297cJSON* csilk_bind_json(csilk_ctx_t* c);
1298
1310cJSON* csilk_bind_json_err(csilk_ctx_t* c, const char** error);
1311
1322const char* csilk_get_cookie(csilk_ctx_t* c, const char* name);
1323
1341void csilk_set_cookie(csilk_ctx_t* c,
1342 const char* name,
1343 const char* value,
1344 int max_age,
1345 const char* path,
1346 const char* domain,
1347 int secure,
1348 int http_only);
1349
1356void csilk_session_init(void);
1357
1366void csilk_session_start(csilk_ctx_t* c);
1367
1375void csilk_session_set(csilk_ctx_t* c, const char* key, void* value);
1376
1385void* csilk_session_get(csilk_ctx_t* c, const char* key);
1386
1395void csilk_session_destroy(csilk_ctx_t* c);
1396
1400#define CSILK_VALID_REQUIRED (1 << 0)
1401#define CSILK_VALID_INT (1 << 1)
1402#define CSILK_VALID_STRING \
1403 (1 << 2)
1405#define CSILK_VALID_EMAIL \
1406 (1 << 3)
1416typedef struct csilk_valid_rule_s {
1417 const char* field;
1418 int flags;
1420 int min;
1422 int max;
1424 const char* source;
1427
1442const char* csilk_validate(csilk_ctx_t* c, const csilk_valid_rule_t* rules);
1443
1456void csilk_json(csilk_ctx_t* c, int status, cJSON* json);
1457
1468void csilk_json_error(csilk_ctx_t* c, int status, const char* message);
1469
1483int csilk_bind_reflect(csilk_ctx_t* c, const char* type_name, void* ptr);
1484
1496void csilk_json_reflect(csilk_ctx_t* c, int status, const char* type_name, const void* ptr);
1497
1503#define csilk_bind(c, type, ptr) csilk_bind_reflect(c, #type, ptr)
1510#define csilk_json_t(c, status, type, ptr) csilk_json_reflect(c, status, #type, ptr)
1511
1522const char* csilk_get_client_ip(csilk_ctx_t* c);
1523
1535void csilk_split_url(const char* url, char** path, char** query);
1536
1547void csilk_parse_query(csilk_ctx_t* c, const char* query_string);
1548
1555typedef struct csilk_router_node_s csilk_router_node_t;
1556
1566typedef struct csilk_router_s {
1567 csilk_router_node_t* root;
1569
1571typedef struct csilk_group_s csilk_group_t;
1572
1582
1599 const char* method,
1600 const char* path,
1601 csilk_handler_t* handlers,
1602 size_t handler_count);
1603
1616csilk_handler_t* csilk_router_match(csilk_router_t* r, const char* method, const char* path);
1617
1628int csilk_router_match_ctx(csilk_router_t* r, csilk_ctx_t* c);
1629
1639
1646
1659
1675 const char* title,
1676 const char* version,
1677 const char* description);
1678
1700 const char* method,
1701 const char* path,
1702 csilk_handler_t* handlers,
1703 size_t handler_count,
1704 const char* path_pattern,
1705 const char* input_type,
1706 const char* output_type,
1707 const char* summary,
1708 const char* description);
1709
1719 const char* method,
1720 const char* path,
1721 csilk_handler_t* handlers,
1722 size_t handler_count,
1723 const char* perm_required,
1724 const char* perm_resource);
1725
1740 const char* method,
1741 const char* path,
1742 csilk_handler_t* handlers,
1743 size_t handler_count,
1744 const char* path_pattern,
1745 const char* input_type,
1746 const char* output_type,
1747 const char* summary,
1748 const char* description,
1749 const char* perm_required,
1750 const char* perm_resource);
1751
1758#define CSILK_ROUTE( \
1759 r, method, path, handlers, handler_count, input_type, output_type, summary, desc) \
1760 csilk_router_add_extended(r, \
1761 method, \
1762 path, \
1763 handlers, \
1764 handler_count, \
1765 path, \
1766 input_type, \
1767 output_type, \
1768 summary, \
1769 desc)
1770
1788void csilk_serve_openapi(csilk_ctx_t* c,
1789 csilk_router_t* r,
1790 const char* title,
1791 const char* version,
1792 const char* description);
1793
1809void csilk_serve_swagger_ui(csilk_ctx_t* c);
1810
1821csilk_group_t* csilk_group_new(csilk_router_t* router, const char* prefix);
1822
1833csilk_group_t* csilk_group_group(csilk_group_t* parent, const char* prefix);
1834
1844void csilk_group_use(csilk_group_t* group, csilk_handler_t handler);
1845
1857void csilk_group_add_route(csilk_group_t* group,
1858 const char* method,
1859 const char* path,
1860 csilk_handler_t handler);
1861
1879void csilk_group_add_route_extended(csilk_group_t* group,
1880 const char* method,
1881 const char* path,
1882 csilk_handler_t handler,
1883 const char* input_type,
1884 const char* output_type,
1885 const char* summary,
1886 const char* description);
1887
1888void csilk_group_add_route_extended_perm(csilk_group_t* group,
1889 const char* method,
1890 const char* path,
1891 csilk_handler_t handler,
1892 const char* input_type,
1893 const char* output_type,
1894 const char* summary,
1895 const char* description,
1896 const char* perm_required,
1897 const char* perm_resource);
1898
1913void csilk_group_add_handlers(csilk_group_t* group,
1914 const char* method,
1915 const char* path,
1916 csilk_handler_t* handlers,
1917 size_t count);
1918
1927void csilk_group_free(csilk_group_t* group);
1928
1933#define csilk_GET(group, path, handler) csilk_group_add_route(group, "GET", path, handler)
1935#define csilk_POST(group, path, handler) csilk_group_add_route(group, "POST", path, handler)
1937#define csilk_PUT(group, path, handler) csilk_group_add_route(group, "PUT", path, handler)
1939#define csilk_DELETE(group, path, handler) csilk_group_add_route(group, "DELETE", path, handler)
1941#define csilk_PATCH(group, path, handler) csilk_group_add_route(group, "PATCH", path, handler)
1943#define csilk_OPTIONS(group, path, handler) csilk_group_add_route(group, "OPTIONS", path, handler)
1945#define csilk_HEAD(group, path, handler) csilk_group_add_route(group, "HEAD", path, handler)
1958void csilk_ws_handshake(csilk_ctx_t* c);
1959
1971void csilk_ws_send(csilk_ctx_t* c, const uint8_t* payload, size_t len, int opcode);
1972
1985void csilk_ws_close(csilk_ctx_t* c, uint16_t status_code, const char* reason);
1986
1987/* --- Streaming Response (Chunked Transfer Encoding) --- */
1988
2001void csilk_response_write(csilk_ctx_t* c, const uint8_t* data, size_t len);
2002
2011void csilk_response_end(csilk_ctx_t* c);
2012
2013/* --- Server-Sent Events (SSE) --- */
2014
2024void csilk_sse_init(csilk_ctx_t* c);
2025
2037void csilk_sse_send(csilk_ctx_t* c, const char* event, const char* data);
2038
2046void csilk_sse_close(csilk_ctx_t* c);
2047
2048/* --- JWT Authentication Middleware --- */
2049
2062char* csilk_jwt_generate(csilk_ctx_t* c, cJSON* payload, const char* secret);
2063
2076cJSON* csilk_jwt_verify(csilk_ctx_t* c, const char* token, const char* secret);
2077
2089void csilk_jwt_middleware(csilk_ctx_t* c, const char* secret);
2090
2091/* --- Gzip Compression Middleware --- */
2092
2103void csilk_gzip_middleware(csilk_ctx_t* c);
2104
2105/* --- Multipart Form Data --- */
2106
2114typedef struct csilk_multipart_part_s {
2115 char name[128];
2117 char filename[256];
2119 char content_type[64];
2121 uint8_t* data;
2123 size_t data_len;
2124 csilk_ctx_t* ctx;
2126
2135
2146void csilk_multipart_parse(csilk_ctx_t* c, csilk_multipart_handler_t handler);
2147
2149typedef struct csilk_server_s csilk_server_t;
2150
2156csilk_server_t* csilk_ctx_get_server(csilk_ctx_t* c);
2157
2158/* --- Hook System --- */
2159
2182
2188typedef void (*csilk_server_hook_handler_t)(csilk_server_t* s);
2189
2195typedef void (*csilk_ctx_hook_handler_t)(csilk_ctx_t* c);
2196
2209void csilk_server_add_hook(csilk_server_t* s, csilk_hook_type_t type, void* handler);
2210
2211/* --- Crypto Driver Interface --- */
2212
2220typedef struct {
2225 void (*sha256)(const uint8_t* data, size_t len, uint8_t out[32]);
2232 void (*hmac_sha256)(const uint8_t* key,
2233 size_t key_len,
2234 const uint8_t* data,
2235 size_t data_len,
2236 uint8_t out[32]);
2240 void (*generate_uuid)(char buf[37]);
2242
2253void csilk_server_set_crypto_driver(csilk_server_t* server, csilk_crypto_driver_t* driver);
2254
2266void csilk_server_set_cipher_driver(csilk_server_t* server, csilk_cipher_driver_t* driver);
2267
2273void csilk_ctx_set_storage_driver(csilk_ctx_t* c, csilk_storage_driver_t* driver);
2274
2280void csilk_ctx_set_crypto_driver(csilk_ctx_t* c, csilk_crypto_driver_t* driver);
2281
2287void csilk_ctx_set_cipher_driver(csilk_ctx_t* c, csilk_cipher_driver_t* driver);
2288
2298csilk_server_t* csilk_server_new(csilk_router_t* router);
2299
2310int csilk_server_use(csilk_server_t* server, csilk_handler_t handler);
2311
2322void csilk_server_set_not_found_handler(csilk_server_t* server, csilk_handler_t handler);
2323
2335void csilk_server_set_spa_fallback(csilk_server_t* server, const char* doc_root);
2336
2345void csilk_server_free(csilk_server_t* server);
2346
2355void csilk_server_stop(csilk_server_t* server);
2356void csilk_server_get_stats(csilk_server_t* server, int* active_conn, int* pooled_conn);
2357
2367void csilk_server_set_config(csilk_server_t* server, const csilk_server_config_t* config);
2368
2377int csilk_server_set_max_connections(csilk_server_t* server, int max);
2378
2387void csilk_server_set_storage_driver(csilk_server_t* server, csilk_storage_driver_t* driver);
2388
2399int csilk_server_run(csilk_server_t* server, int port);
2400
2401/* --- Database Interface --- */
2402
2409void csilk_db_init(void);
2410
2425csilk_db_pool_t* csilk_db_pool_new(const char* driver_name, const char* dsn);
2426
2435
2446cJSON* csilk_db_query_json(csilk_db_pool_t* pool, const char* sql);
2447
2457int csilk_db_exec(csilk_db_pool_t* pool, const char* sql);
2458
2472cJSON* csilk_db_query_param_json(csilk_db_pool_t* pool, const char* sql, const char** params);
2473
2483void csilk_metrics_middleware(csilk_ctx_t* c, const char* arg);
2484
2493void csilk_metrics_handler(csilk_ctx_t* c);
2496
2505typedef struct csilk_mq_s csilk_mq_t;
2506
2510csilk_mq_t* csilk_ctx_get_mq(csilk_ctx_t* c);
2511
2521csilk_mq_t* csilk_server_get_mq(csilk_server_t* server);
2522
2526csilk_router_t* csilk_server_get_router(csilk_server_t* server);
2527
2535typedef struct csilk_mq_ctx_s csilk_mq_ctx_t;
2536
2542typedef void (*csilk_mq_handler_t)(csilk_mq_ctx_t* ctx);
2543
2552void csilk_mq_next(csilk_mq_ctx_t* ctx);
2553
2561void csilk_mq_abort(csilk_mq_ctx_t* ctx);
2562
2570typedef void (*csilk_mq_worker_t)(const char* topic, const void* payload, size_t len);
2571
2584void csilk_mq_offload(csilk_mq_ctx_t* ctx, csilk_mq_worker_t worker);
2585
2592const char* csilk_mq_get_topic(csilk_mq_ctx_t* ctx);
2593
2603const void* csilk_mq_get_payload(csilk_mq_ctx_t* ctx, size_t* len);
2604
2615void csilk_mq_use(csilk_mq_t* mq, const char* topic, csilk_mq_handler_t middleware);
2616
2627void csilk_mq_subscribe(csilk_mq_t* mq, const char* topic, csilk_mq_handler_t subscriber);
2628
2643int csilk_mq_publish(csilk_mq_t* mq, const char* topic, const void* payload, size_t len);
2644
2648typedef struct {
2651 uint64_t failed_total;
2652 uint32_t queue_depth;
2653 uint32_t topic_count;
2655
2661void csilk_mq_get_stats(csilk_mq_t* mq, csilk_mq_stats_t* stats);
2662
2668char* csilk_mq_stats_to_json(const csilk_mq_stats_t* stats);
2669
2675void csilk_mq_register_monitor(csilk_mq_t* mq, csilk_ctx_t* c);
2676
2688int csilk_mq_set_persistence(csilk_mq_t* mq, const char* wal_path);
2689
2690#endif /* CSILK_H */
Unified pluggable interface for AI/LLM service integration.
Arena allocator for request-scoped memory.
Definition arena.c:101
Pluggable cryptographic primitive driver interface.
void csilk_serve_openapi(csilk_ctx_t *c, csilk_router_t *r, const char *title, const char *version, const char *description)
Serve the OpenAPI JSON specification as the response.
Definition swagger.c:625
uint64_t published_total
Definition csilk.h:2649
void csilk_set_websocket(csilk_ctx_t *c, int is_websocket)
Enable or disable WebSocket mode.
Definition context.c:516
char * csilk_jwt_generate(csilk_ctx_t *c, cJSON *payload, const char *secret)
Generate a signed JWT token (HS256).
Definition jwt.c:45
void csilk_ws_send(csilk_ctx_t *c, const uint8_t *payload, size_t len, int opcode)
Send a WebSocket data frame.
Definition websocket.c:119
int csilk_get_params_count(csilk_ctx_t *c)
Get the number of path parameters extracted from the URL.
Definition context.c:262
void csilk_group_add_route_extended(csilk_group_t *group, const char *method, const char *path, csilk_handler_t handler, const char *input_type, const char *output_type, const char *summary, const char *description)
Add a route with OpenAPI/reflection metadata to a group.
Definition group.c:303
void csilk_sse_close(csilk_ctx_t *c)
Close the SSE connection.
Definition sse.c:174
@ CSILK_HOOK_SERVER_START
Definition csilk.h:2167
const char * csilk_ctx_get_handler_perm_resource(csilk_ctx_t *c)
Get the resource pattern for the matched handler's permission check.
Definition context.c:746
const char * csilk_get_param_key(csilk_ctx_t *c, int index)
Get the name of a path parameter by its index.
Definition context.c:273
uint64_t csilk_metrics_get_total_requests(void)
Definition metrics.c:544
double cpu_sys_time_sec
Definition csilk.h:1086
void csilk_jwt_middleware(csilk_ctx_t *c, const char *secret)
JWT authentication middleware.
Definition jwt.c:235
int csilk_get_handler_index(csilk_ctx_t *c)
Get the index of the currently executing handler in the chain.
Definition context.c:667
void csilk_group_add_route(csilk_group_t *group, const char *method, const char *path, csilk_handler_t handler)
Add a route to the group.
Definition group.c:276
void(*)(csilk_ctx_t *c, const uint8_t *payload, size_t len, int opcode) csilk_get_on_ws_message(csilk_ctx_t *c)
Get the currently registered WebSocket message callback.
Definition csilk.h:387
void csilk_group_free(csilk_group_t *group)
Destroy a route group and release its resources.
Definition group.c:491
void csilk_ws_handshake(csilk_ctx_t *c)
Perform the WebSocket upgrade handshake (HTTP Upgrade request).
Definition websocket.c:57
uint64_t auth_failures
Definition csilk.h:1077
uint64_t rate_limit_blocks
Definition csilk.h:1075
const char * csilk_get_param_value(csilk_ctx_t *c, int index)
Get the value of a path parameter by its index.
Definition context.c:287
void csilk_process_get_stats(csilk_process_stats_t *stats)
Definition metrics.c:167
void csilk_group_add_handlers(csilk_group_t *group, const char *method, const char *path, csilk_handler_t *handlers, size_t count)
Add a route with an explicit array of handlers.
Definition group.c:441
void(* csilk_multipart_handler_t)(csilk_multipart_part_t *part)
Callback invoked for each part during multipart parsing.
Definition csilk.h:2134
int csilk_get_file_fd(csilk_ctx_t *c)
Get the current zero-copy file descriptor.
Definition context.c:716
void csilk_response_write(csilk_ctx_t *c, const uint8_t *data, size_t len)
Write a chunk to the response stream (chunked transfer encoding).
Definition context.c:1702
uv_work_t * csilk_get_work_req(csilk_ctx_t *c)
Get the libuv work request associated with the context.
Definition context.c:690
void csilk_serve_swagger_ui(csilk_ctx_t *c)
Serve the embedded Swagger UI page.
Definition swagger.c:709
void csilk_set_request_id(csilk_ctx_t *c, const char *id)
Set the request unique identifier.
Definition context.c:677
void csilk_security_get_stats(csilk_security_stats_t *stats)
Definition metrics.c:156
uint64_t csrf_violations
Definition csilk.h:1076
void csilk_server_get_stats(csilk_server_t *server, int *active_conn, int *pooled_conn)
Definition server.c:1708
void csilk_router_add_perm(csilk_router_t *r, const char *method, const char *path, csilk_handler_t *handlers, size_t handler_count, const char *perm_required, const char *perm_resource)
Register a route with permission metadata.
Definition router.c:513
cJSON * csilk_generate_openapi_json(csilk_router_t *router, const char *title, const char *version, const char *description)
Generate an OpenAPI 3.0 specification JSON from the router.
Definition swagger.c:311
void csilk_sse_init(csilk_ctx_t *c)
Initialise a Server-Sent Events connection.
Definition sse.c:55
void csilk_group_add_route_extended_perm(csilk_group_t *group, const char *method, const char *path, csilk_handler_t handler, const char *input_type, const char *output_type, const char *summary, const char *description, const char *perm_required, const char *perm_resource)
Add a route with OpenAPI/reflection metadata to a group.
Definition group.c:363
uint64_t csilk_metrics_get_total_duration(void)
Definition metrics.c:550
uint32_t topic_count
Definition csilk.h:2653
void csilk_sse_send(csilk_ctx_t *c, const char *event, const char *data)
Send an SSE event (or comment) to the client.
Definition sse.c:122
void csilk_mq_register_monitor(csilk_mq_t *mq, csilk_ctx_t *c)
Register a WebSocket monitor for real-time MQ events.
Definition mq.c:313
cJSON * csilk_router_collect_routes(csilk_router_t *r)
Collect all registered routes from the router tree as a cJSON array.
Definition router.c:281
void csilk_ctx_set_crypto_driver(csilk_ctx_t *c, csilk_crypto_driver_t *driver)
Set the crypto driver for the context.
Definition context.c:887
uint32_t queue_depth
Definition csilk.h:2652
csilk_group_t * csilk_group_new(csilk_router_t *router, const char *prefix)
Create a new route group with a URL prefix.
Definition group.c:132
void csilk_set_sse(csilk_ctx_t *c, int is_sse)
Enable or disable Server-Sent Events (SSE) mode.
Definition context.c:539
const char * csilk_ctx_get_handler_perm_required(csilk_ctx_t *c)
Get the permission string required by the matched handler.
Definition context.c:736
void csilk_arena_get_stats(csilk_arena_t *arena, size_t *total_size, size_t *total_used)
Get total allocated size and used bytes in the arena.
Definition arena.c:275
void csilk_response_end(csilk_ctx_t *c)
Finalise a chunked streaming response.
Definition context.c:1734
csilk_server_t * csilk_ctx_get_server(csilk_ctx_t *c)
Get the server instance associated with the current context.
Definition context.c:635
void csilk_router_add_extended(csilk_router_t *r, const char *method, const char *path, csilk_handler_t *handlers, size_t handler_count, const char *path_pattern, const char *input_type, const char *output_type, const char *summary, const char *description)
Register a route with full OpenAPI/reflection metadata.
Definition router.c:452
void csilk_ws_close(csilk_ctx_t *c, uint16_t status_code, const char *reason)
Send a WebSocket close frame.
Definition websocket.c:221
void csilk_ready_check_handler(csilk_ctx_t *c)
Built-in Readiness Check handler. Performs deep health check (MQ, connections) and returns 200 or 503...
Definition request_id.c:94
const char * csilk_ctx_get_handler_path(csilk_ctx_t *c)
Get the route pattern for the matched handler (e.g., "/users/:id").
Definition context.c:726
void csilk_mq_get_stats(csilk_mq_t *mq, csilk_mq_stats_t *stats)
Get current MQ statistics.
Definition mq.c:276
void csilk_set_file_response(csilk_ctx_t *c, int fd, size_t offset, size_t size)
Set the zero-copy file response parameters.
Definition context.c:702
void csilk_ctx_set_cipher_driver(csilk_ctx_t *c, csilk_cipher_driver_t *driver)
Set the cipher driver for the context.
Definition context.c:899
void csilk_multipart_parse(csilk_ctx_t *c, csilk_multipart_handler_t handler)
Parse a multipart/form-data request body.
Definition multipart.c:45
uint64_t delivered_total
Definition csilk.h:2650
cJSON * csilk_jwt_verify(csilk_ctx_t *c, const char *token, const char *secret)
Verify a JWT token and extract its payload.
Definition jwt.c:144
uint64_t failed_total
Definition csilk.h:2651
double cpu_user_time_sec
Definition csilk.h:1085
void csilk_gzip_middleware(csilk_ctx_t *c)
Gzip response compression middleware.
Definition gzip.c:151
char * csilk_mq_stats_to_json(const csilk_mq_stats_t *stats)
Convert MQ statistics to a JSON string.
Definition mq.c:296
void csilk_ctx_set_storage_driver(csilk_ctx_t *c, csilk_storage_driver_t *driver)
Set the storage driver for the context.
Definition context.c:875
void csilk_router_add_extended_perm(csilk_router_t *r, const char *method, const char *path, csilk_handler_t *handlers, size_t handler_count, const char *path_pattern, const char *input_type, const char *output_type, const char *summary, const char *description, const char *perm_required, const char *perm_resource)
Register a route with full metadata including permissions.
Definition router.c:553
void csilk_router_free(csilk_router_t *r)
Destroy the router and release all its resources.
Definition router.c:221
size_t rss_bytes
Definition csilk.h:1084
Top-level application configuration.
Definition csilk.h:1000
CORS middleware configuration.
Definition csilk.h:886
A fixed-size chained hash table for HTTP headers.
Definition csilk.h:162
A single HTTP header stored as a node in a chained hash table.
Definition csilk.h:131
Logger initialisation configuration.
Definition csilk.h:757
Message Queue statistics.
Definition csilk.h:2648
A single part parsed from a multipart/form-data request body.
Definition csilk.h:1982
A single URL path parameter extracted from a route pattern.
Definition csilk.h:213
OS-level process statistics.
Definition csilk.h:1083
Parsed HTTP request.
Definition csilk.h:174
Mutable HTTP response.
Definition csilk.h:194
The main HTTP router.
Definition csilk.h:1419
Security layer statistics.
Definition csilk.h:1074
Low-level server configuration options.
Definition csilk.h:953
A single validation rule for request parameter checking.
Definition csilk.h:1258
void csilk_set_on_ws_send(csilk_ctx_t *c, void(*callback)(csilk_ctx_t *c, const uint8_t *payload, size_t len, int opcode))
Set a callback for outgoing WebSocket frames (for testing).
Definition context.c:835
void csilk_set_on_ws_message(csilk_ctx_t *c, void(*callback)(csilk_ctx_t *c, const uint8_t *payload, size_t len, int opcode))
Register a callback for incoming WebSocket messages.
Definition context.c:825
void csilk_request_id_middleware(csilk_ctx_t *c)
Request ID middleware. Generates a unique ID for each request and sets X-Request-Id header.
Definition request_id.c:31
csilk_handler_t * csilk_router_match(csilk_router_t *r, const char *method, const char *path)
Match a raw method+path to handlers (standalone, no context).
Definition router.c:775
void(* csilk_server_hook_handler_t)(csilk_server_t *s)
Callback signature for server-level hooks.
Definition csilk.h:2056
void csilk_mq_subscribe(csilk_mq_t *mq, const char *topic, csilk_mq_handler_t subscriber)
Register a subscriber for a topic.
Definition mq.c:486
char * csilk_arena_strndup(csilk_arena_t *arena, const char *s, size_t n)
Duplicate n bytes of a string using the arena allocator.
Definition arena.c:208
void csilk_string(csilk_ctx_t *c, int status, const char *msg)
Set a plain-text response body and status code.
Definition context.c:214
void csilk_mq_next(csilk_mq_ctx_t *ctx)
Pass control to the next middleware or subscriber in the MQ chain.
Definition mq.c:60
void _csilk_log_structured(csilk_log_level_t lv, const char *file, int line, const char *func, cJSON *extra, const char *fmt,...)
Log a structured JSON message with extra key-value fields.
Definition logger.c:411
void csilk_server_set_config(csilk_server_t *server, const csilk_server_config_t *config)
Apply server configuration options.
Definition server.c:1730
void csilk_mq_use(csilk_mq_t *mq, const char *topic, csilk_mq_handler_t middleware)
Register MQ middleware for a topic.
Definition mq.c:449
cJSON * csilk_bind_json(csilk_ctx_t *c)
Bind the request body (JSON) to a cJSON object.
Definition context.c:1048
void csilk_status(csilk_ctx_t *c, int status)
Set the HTTP response status code.
Definition context.c:196
const char * csilk_get_cookie(csilk_ctx_t *c, const char *name)
Get a cookie value by name from the Cookie request header.
Definition context.c:1110
void csilk_server_set_crypto_driver(csilk_server_t *server, csilk_crypto_driver_t *driver)
Set the global crypto driver for the server.
Definition server.c:1802
void(* csilk_mq_worker_t)(const char *topic, const void *payload, size_t len)
Signature for a background MQ worker function.
Definition csilk.h:2422
void csilk_server_set_storage_driver(csilk_server_t *server, csilk_storage_driver_t *driver)
Replace the context key-value storage driver.
Definition server.c:1787
csilk_hook_type_t
Lifecycle hook types for the server and individual requests.
Definition csilk.h:2034
@ CSILK_HOOK_COUNT
Definition csilk.h:2047
@ CSILK_HOOK_CONN_CLOSE
Definition csilk.h:2041
@ CSILK_HOOK_REQUEST_BEGIN
Definition csilk.h:2043
@ CSILK_HOOK_CONN_OPEN
Definition csilk.h:2039
@ CSILK_HOOK_SERVER_STOP
Definition csilk.h:2037
@ CSILK_HOOK_REQUEST_END
Definition csilk.h:2045
char * csilk_arena_strdup(csilk_arena_t *arena, const char *s)
Duplicate a NUL-terminated string using the arena allocator.
Definition arena.c:184
void csilk_server_free(csilk_server_t *server)
Destroy the server and release all resources.
Definition server.c:1647
void csilk_health_check_handler(csilk_ctx_t *c)
Built-in Health Check handler. Returns a simple JSON response {"status": "up"}.
Definition request_id.c:67
void csilk_log_set_request_id(const char *request_id)
Set the Request ID for the current thread (for log correlation).
Definition logger.c:476
void csilk_split_url(const char *url, char **path, char **query)
Split a URL into path and query-string components.
Definition url.c:98
csilk_group_t * csilk_group_group(csilk_group_t *parent, const char *prefix)
Create a nested sub-group within an existing group.
Definition group.c:160
void csilk_json_reflect(csilk_ctx_t *c, int status, const char *type_name, const void *ptr)
Serialise a reflected struct as a JSON response.
Definition context.c:1333
void csilk_server_stop(csilk_server_t *server)
Request a graceful server shutdown.
Definition server.c:1699
void csilk_arena_reset(csilk_arena_t *arena)
Reset the arena without freeing its chunks.
Definition arena.c:253
void csilk_session_set(csilk_ctx_t *c, const char *key, void *value)
Store a value in the session.
Definition session.c:332
void csilk_config_free(csilk_config_t *config)
Free all heap-allocated strings inside a configuration.
Definition config.c:344
int csilk_mq_publish(csilk_mq_t *mq, const char *topic, const void *payload, size_t len)
Publish a message to a topic.
Definition mq.c:781
void csilk_ws_leave_room(csilk_ctx_t *c, const char *room_name)
Remove a WebSocket client from a room.
Definition ws_room.c:136
const char * csilk_get_query(csilk_ctx_t *c, const char *key)
Get a query-string parameter by key.
Definition context.c:339
void csilk_set(csilk_ctx_t *c, const char *key, void *value)
Store an opaque value in the request context.
Definition context.c:968
void csilk_set_request_header(csilk_ctx_t *c, const char *key, const char *value)
Set (or overwrite) a request header.
Definition context.c:353
const char * csilk_validate(csilk_ctx_t *c, const csilk_valid_rule_t *rules)
Validate request parameters against a set of rules.
Definition validate.c:119
void csilk_session_init(void)
Initialise the session subsystem (call once at startup).
Definition session.c:251
cJSON * csilk_db_query_param_json(csilk_db_pool_t *pool, const char *sql, const char **params)
Execute a parameterised SELECT query with ? placeholders.
Definition db.c:261
void * csilk_arena_alloc(csilk_arena_t *arena, size_t size)
Allocate zero-initialised memory from an arena.
Definition arena.c:145
csilk_arena_t * csilk_arena_new(size_t default_chunk_size)
Create a new arena allocator.
Definition arena.c:121
int csilk_bind_reflect(csilk_ctx_t *c, const char *type_name, void *ptr)
Parse the JSON request body into a struct registered via reflection.
Definition context.c:1305
csilk_db_pool_t * csilk_db_pool_new(const char *driver_name, const char *dsn)
Create a new database connection pool.
Definition db.c:142
void csilk_abort(csilk_ctx_t *c)
Immediately abort the handler chain.
Definition context.c:185
const char * csilk_get_header(csilk_ctx_t *c, const char *key)
Get a request header value by name (case-insensitive).
Definition context.c:306
const char * csilk_get_method(csilk_ctx_t *c)
Get the HTTP method of the current request.
Definition context.c:453
void csilk_ws_broadcast_room(csilk_ctx_t *c, const char *room_name, const char *message)
Broadcast a message to all WebSockets in a room via MQ.
Definition ws_room.c:156
void csilk_set_response_body(csilk_ctx_t *c, const char *body, size_t len, int managed)
Overwrite the response body from middleware.
Definition context.c:787
csilk_arena_t * csilk_get_arena(csilk_ctx_t *c)
Get the arena allocator associated with the request context.
Definition context.c:595
const char * csilk_get_body(csilk_ctx_t *c, size_t *out_len)
Get the raw request body and its length.
Definition context.c:481
const char * csilk_get_form_field(csilk_ctx_t *c, const char *key)
Get a form-urlencoded field value by key.
Definition context.c:1494
void csilk_parse_form_urlencoded(csilk_ctx_t *c)
Parse the request body as application/x-www-form-urlencoded.
Definition context.c:1427
int csilk_router_match_ctx(csilk_router_t *r, csilk_ctx_t *c)
Match the current request against the router and update the context.
Definition router.c:796
void * csilk_get(csilk_ctx_t *c, const char *key)
Retrieve an opaque value from the request context.
Definition context.c:1020
void csilk_json(csilk_ctx_t *c, int status, cJSON *json)
Send a JSON response (takes ownership of the cJSON object).
Definition context.c:1244
void csilk_db_pool_free(csilk_db_pool_t *pool)
Free a database pool and disconnect.
Definition db.c:180
void csilk_set_async(csilk_ctx_t *c, int is_async)
Enable or disable asynchronous response mode.
Definition context.c:623
int csilk_log_is_json(void)
Check whether the logger is in JSON format mode.
Definition logger.c:465
int csilk_server_use(csilk_server_t *server, csilk_handler_t handler)
Register global middleware.
Definition server.c:1610
void(* csilk_handler_t)(csilk_ctx_t *c)
Function pointer for route handlers and middleware.
Definition csilk.h:120
int csilk_load_config(const char *yaml_path, csilk_config_t *config)
Load and parse a YAML configuration file.
Definition config.c:75
void csilk_redirect(csilk_ctx_t *c, int status, const char *location)
Send an HTTP redirect response with a custom status code.
Definition context.c:930
const char * csilk_get_response_header(csilk_ctx_t *c, const char *key)
Get a response header value by name (case-insensitive).
Definition context.c:322
const char * csilk_get_request_id(csilk_ctx_t *c)
Get the unique identifier for the current request.
Definition context.c:579
csilk_server_t * csilk_server_new(csilk_router_t *router)
Create a new server instance.
Definition server.c:1469
size_t csilk_get_body_len(csilk_ctx_t *c)
Get the length of the raw request body.
Definition context.c:495
void(* csilk_ctx_hook_handler_t)(csilk_ctx_t *c)
Callback signature for request/connection-level hooks.
Definition csilk.h:2063
void csilk_mq_offload(csilk_mq_ctx_t *ctx, csilk_mq_worker_t worker)
Offload message processing to a background thread.
Definition mq.c:171
const char * csilk_get_client_ip(csilk_ctx_t *c)
Get the client's IP address.
Definition server.c:1435
void csilk_redirect_simple(csilk_ctx_t *c, const char *url)
Send a simple 302 Found redirect.
Definition context.c:950
void csilk_db_init(void)
Initialise the database subsystem.
Definition db.c:335
csilk_mq_t * csilk_ctx_get_mq(csilk_ctx_t *c)
Get the internal MQ instance from the context.
Definition context.c:645
int csilk_log_init(csilk_log_config_t config)
Initialize the global logger with config.
Definition logger.c:313
void csilk_next(csilk_ctx_t *c)
Pass control to the next handler in the middleware/handler chain.
Definition context.c:165
void csilk_auth_middleware(csilk_ctx_t *c, csilk_auth_validator_t validator)
Simple token-based authentication middleware.
Definition auth.c:32
int csilk_server_set_max_connections(csilk_server_t *server, int max)
Set the maximum number of concurrent connections and return the previous limit.
Definition server.c:1768
cJSON * csilk_db_query_json(csilk_db_pool_t *pool, const char *sql)
Execute a SELECT query and return the result as a JSON array.
Definition db.c:194
void csilk_metrics_handler(csilk_ctx_t *c)
Prometheus /metrics endpoint handler.
Definition metrics.c:304
void csilk_parse_query(csilk_ctx_t *c, const char *query_string)
Parse a raw query string and populate the query_params map.
Definition context.c:1370
void csilk_metrics_middleware(csilk_ctx_t *c, const char *arg)
Prometheus metrics middleware.
Definition metrics.c:212
void csilk_ws_join_room(csilk_ctx_t *c, const char *room_name)
Join a WebSocket client to a room.
Definition ws_room.c:88
void * csilk_session_get(csilk_ctx_t *c, const char *key)
Retrieve a value from the session.
Definition session.c:376
void csilk_set_header(csilk_ctx_t *c, const char *key, const char *value)
Set (or overwrite) a response header.
Definition context.c:368
void csilk_json_error(csilk_ctx_t *c, int status, const char *message)
Send a JSON-formatted error response.
Definition context.c:1277
void csilk_log_close()
Close the global logger.
Definition logger.c:515
cJSON * csilk_bind_json_err(csilk_ctx_t *c, const char **error)
Bind request body to cJSON with a descriptive error message.
Definition context.c:1068
void _csilk_log_internal(csilk_log_level_t lv, const char *file, int line, const char *func, const char *fmt,...)
Internal log function (use macros instead).
Definition logger.c:361
void csilk_server_set_not_found_handler(csilk_server_t *server, csilk_handler_t handler)
Set a custom handler for 404 (route-not-found) responses.
Definition server.c:1568
int(* csilk_auth_validator_t)(const char *token)
Authentication validator callback.
Definition csilk.h:1086
csilk_log_level_t
Log severity levels.
Definition csilk.h:739
@ CSILK_LOG_FATAL
Definition csilk.h:748
@ CSILK_LOG_TRACE
Definition csilk.h:740
@ CSILK_LOG_INFO
Definition csilk.h:743
@ CSILK_LOG_ERROR
Definition csilk.h:746
@ CSILK_LOG_WARN
Definition csilk.h:744
@ CSILK_LOG_DEBUG
Definition csilk.h:742
int csilk_server_run(csilk_server_t *server, int port)
Start the server and enter the libuv event loop.
Definition server.c:2137
const char * csilk_get_path(csilk_ctx_t *c)
Get the decoded URL path of the current request.
Definition context.c:468
csilk_router_t * csilk_router_new(void)
Create a new empty router.
Definition router.c:204
void csilk_arena_free(csilk_arena_t *arena)
Free all memory chunks owned by the arena.
Definition arena.c:229
void csilk_static(csilk_ctx_t *c, const char *root_dir)
Serve static files from a local directory.
Definition static.c:287
void csilk_panic(csilk_ctx_t *c)
Trigger a panic (caught by recovery middleware).
Definition recovery.c:70
const char * csilk_mq_get_topic(csilk_mq_ctx_t *ctx)
Get the topic of the current message.
Definition mq.c:91
csilk_router_t * csilk_server_get_router(csilk_server_t *server)
Get the router instance attached to a server.
Definition server.c:2502
const void * csilk_mq_get_payload(csilk_mq_ctx_t *ctx, size_t *len)
Get the payload of the current message.
Definition mq.c:105
void(* csilk_mq_handler_t)(csilk_mq_ctx_t *ctx)
MQ handler signature for middleware and subscribers.
Definition csilk.h:2394
int csilk_is_async(csilk_ctx_t *c)
Check whether asynchronous response mode is enabled.
Definition context.c:657
void csilk_server_set_cipher_driver(csilk_server_t *server, csilk_cipher_driver_t *driver)
Set the global cipher algorithm driver for the server.
Definition server.c:1810
int csilk_db_exec(csilk_db_pool_t *pool, const char *sql)
Execute a statement that returns no result rows.
Definition db.c:219
int csilk_mq_set_persistence(csilk_mq_t *mq, const char *wal_path)
Enable Write-Ahead Log (WAL) persistence for the MQ.
Definition mq.c:368
int csilk_is_aborted(csilk_ctx_t *c)
Check whether the handler chain has been aborted.
Definition context.c:807
void csilk_session_destroy(csilk_ctx_t *c)
Destroy the session and clear the session cookie.
Definition session.c:408
void csilk_set_cookie(csilk_ctx_t *c, const char *name, const char *value, int max_age, const char *path, const char *domain, int secure, int http_only)
Set a cookie in the Set-Cookie response header.
Definition context.c:1176
void csilk_router_add(csilk_router_t *r, const char *method, const char *path, csilk_handler_t *handlers, size_t handler_count)
Register a route with one or more handlers.
Definition router.c:489
void csilk_recovery_handler(csilk_ctx_t *c)
Panic-recovery middleware.
Definition recovery.c:36
void csilk_mq_abort(csilk_mq_ctx_t *ctx)
Abort the MQ middleware/subscriber chain.
Definition mq.c:78
void csilk_session_start(csilk_ctx_t *c)
Start or resume a session for the current request.
Definition session.c:274
void csilk_csrf_middleware(csilk_ctx_t *c)
Stateless CSRF protection middleware.
Definition csrf.c:38
int csilk_config_validate(const csilk_config_t *config, const char **error_msg)
Validate configuration values for semantic correctness.
Definition config.c:432
void csilk_group_use(csilk_group_t *group, csilk_handler_t handler)
Add middleware to a group.
Definition group.c:195
const char * csilk_get_param(csilk_ctx_t *c, const char *key)
Get a URL path parameter by key.
Definition context.c:244
int csilk_csrf_generate_token(char *buf, size_t buf_size)
Generate a cryptographically random CSRF token.
Definition csrf.c:100
csilk_mq_t * csilk_server_get_mq(csilk_server_t *server)
Get the Message Queue instance attached to a server.
Definition server.c:2496
void csilk_server_add_hook(csilk_server_t *s, csilk_hook_type_t type, void *handler)
Register a lifecycle hook callback.
Definition server.c:1832
void csilk_file(csilk_ctx_t *c, const char *file_path)
Serve a specific file from the local filesystem.
Definition static.c:307
void csilk_cors_middleware(csilk_ctx_t *c, const csilk_cors_config_t *config)
CORS middleware — handles preflight and adds CORS headers.
Definition cors.c:34
int csilk_get_status(csilk_ctx_t *c)
Get the current response status code.
Definition context.c:606
int csilk_is_sse(csilk_ctx_t *c)
Check whether the connection is in Server-Sent Events mode.
Definition context.c:529
#define CSILK_HEADER_BUCKETS
Number of buckets in the header chained hash table.
Definition csilk.h:150
void csilk_ctx_cleanup(csilk_ctx_t *c)
Release all memory and resources associated with a request context.
Definition context.c:382
int csilk_is_websocket(csilk_ctx_t *c)
Check whether the connection has been upgraded to WebSocket.
Definition context.c:506
void csilk_logger_handler(csilk_ctx_t *c)
Logging middleware handler. Logs request method, path, and processing time.
Definition logger.c:79
void csilk_add_header(csilk_ctx_t *c, const char *key, const char *value)
Append a response header, preserving any existing value(s).
Definition context.c:1153
void csilk_server_set_spa_fallback(csilk_server_t *server, const char *doc_root)
Enable single-page application (SPA) fallback mode.
Definition server.c:1588
cJSON * csilk_log_make_kv(const char *key,...)
Create a simple key-value cJSON object for structured logging.
Definition logger.c:491
const char * csilk_get_response_body(csilk_ctx_t *c, size_t *out_len)
Get the current response body and its length.
Definition context.c:759
void csilk_rate_limit_middleware(csilk_ctx_t *c, int limit)
Simple per-IP rate-limiting middleware.
Definition ratelimit.c:142
A (currently single-connection) database pool.
Definition db.h:62
csilk_router_t * router
Definition group.c:47
char * prefix
Definition group.c:43
csilk_group_t * parent
Definition group.c:56
Route group structure.
Definition group.c:42
csilk_mq_t * mq
Definition internal.h:512
Internal: Per-message MQ context passed to middleware and subscribers.
Definition internal.h:511
Internal: The Message Queue instance.
Definition internal.h:471
Pluggable permission/access-control (ACL) driver interface.
static csilk_perm_rule_t rules[MAX_RULES]
Global rule table. Populated at startup by csilk_perm_simple_allow().
Definition perm_simple.c:32
Node in the router trie — represents a URL path segment.
Definition router.c:79
Main Server structure — represents the core HTTP server instance.
Definition server_internal.h:54
Virtual function table implemented by each cipher backend.
Definition cipher.h:50
Pluggable cryptographic primitive driver.
Definition csilk.h:2090
Main Request Context — holds all state for the current HTTP request/response cycle.
Definition context_internal.h:107
Pluggable storage driver for context key-value pairs.
Definition csilk.h:95