KEEL 1.0.0
Minimal C11 HTTP client/server library built on epoll/kqueue/io_uring/poll
Loading...
Searching...
No Matches
server.h
Go to the documentation of this file.
1#ifndef KEEL_SERVER_H
2#define KEEL_SERVER_H
3
4#include <keel/allocator.h>
5#include <keel/compress.h>
6#include <keel/error.h>
7#include <keel/parser.h>
8#include <keel/router.h>
9#include <keel/tls.h>
10#include <keel/h2_server.h>
11#include <keel/connection.h>
12#include <keel/event_ctx.h>
13#include <stdarg.h>
14#include <stdatomic.h>
15#include <stdint.h>
16
19typedef KlParser *(*KlParserFactory)(KlAllocator *alloc);
20
22typedef void (*KlAccessLogFn)(const KlRequest *req, int status,
23 size_t body_bytes, double duration_ms,
24 void *user_data);
25
27#define KL_LOG_TRACE 0
28#define KL_LOG_DEBUG 1
29#define KL_LOG_INFO 2
30#define KL_LOG_WARN 3
31#define KL_LOG_ERROR 4
32#define KL_LOG_FATAL 5
36typedef void (*KlLogFn)(int level, const char *fmt, va_list ap,
37 void *user_data);
38
40#define KL_DEFAULT_MAX_CONNS 256
42#define KL_DEFAULT_READ_TIMEOUT 30000
44#define KL_DEFAULT_MAX_BODY_SIZE (1024 * 1024)
67
68typedef struct KlAsyncOp KlAsyncOp;
69
89
96int kl_server_init(KlServer *s, const KlConfig *config);
97
102int kl_server_route(KlServer *s, const char *method, const char *pattern,
103 KlHandler handler, void *user_data,
104 KlBodyReaderFactory body_reader);
105
115int kl_server_use(KlServer *s, const char *method, const char *pattern,
116 KlMiddleware fn, void *user_data);
117
131int kl_server_use_post(KlServer *s, const char *method, const char *pattern,
132 KlMiddleware fn, void *user_data);
133
141int kl_server_ws(KlServer *s, const char *pattern, KlWsServerConfig *config);
142
148
154
157
165
172
173#endif
KlBodyReader *(* KlBodyReaderFactory)(KlAllocator *alloc, const KlRequest *req, void *user_data)
Factory creates a body reader for a given request.
Definition body_reader.h:34
KlError
Diagnostic error codes for Keel public functions.
Definition error.h:11
Server-side HTTP/2 API.
void(* KlHandler)(KlRequest *req, KlResponse *res, void *user_data)
Route handler function.
Definition router.h:11
int(* KlMiddleware)(KlRequest *req, KlResponse *res, void *user_data)
Middleware function signature.
Definition router.h:18
int kl_server_ws(KlServer *s, const char *pattern, KlWsServerConfig *config)
Register a WebSocket endpoint. Matches GET with Upgrade: websocket.
int kl_server_run(KlServer *s)
Start the event loop (blocks until stopped).
int kl_server_route(KlServer *s, const char *method, const char *pattern, KlHandler handler, void *user_data, KlBodyReaderFactory body_reader)
Register a route on the server.
void kl_server_free(KlServer *s)
Free all server resources (pool, router, event loop).
int kl_server_use_post(KlServer *s, const char *method, const char *pattern, KlMiddleware fn, void *user_data)
Register post-body middleware on the server.
void kl_server_stop(KlServer *s)
Request server shutdown. If drain_timeout_ms is configured, enters drain mode first (stops accepting,...
int kl_server_init(KlServer *s, const KlConfig *config)
Initialize server with the given configuration.
void(* KlAccessLogFn)(const KlRequest *req, int status, size_t body_bytes, double duration_ms, void *user_data)
Access log callback — called after each response is fully sent. NULL = disabled.
Definition server.h:22
void kl_server_stats(const KlServer *s, KlServerStats *out)
Populate a stats snapshot from current server state.
void(* KlLogFn)(int level, const char *fmt, va_list ap, void *user_data)
Diagnostic log callback. NULL = fprintf(stderr) fallback.
Definition server.h:36
KlParser *(* KlParserFactory)(KlAllocator *alloc)
Factory function for creating request parsers.
Definition server.h:19
int kl_server_use(KlServer *s, const char *method, const char *pattern, KlMiddleware fn, void *user_data)
Register pre-body middleware on the server.
Bring-your-own allocator vtable.
Definition allocator.h:12
An in-flight async operation that suspends a connection.
Definition async.h:29
Compression configuration for KlConfig.
Definition compress.h:81
Definition server.h:46
KlAccessLogFn access_log
Definition server.h:54
KlH2ServerConfig * h2
Definition server.h:62
int install_signal_handlers
Definition server.h:58
KlParserFactory parser
Definition server.h:53
KlCompressConfig * compress
Definition server.h:65
int port
Definition server.h:47
int read_timeout_ms
Definition server.h:50
void * access_log_data
Definition server.h:55
int max_connections
Definition server.h:49
KlTlsConfig * tls
Definition server.h:61
size_t max_body_size
Definition server.h:63
const char * bind_addr
Definition server.h:48
KlAllocator * alloc
Definition server.h:52
int body_timeout_ms
Definition server.h:51
void * log_user_data
Definition server.h:57
KlLogFn log_fn
Definition server.h:56
int drain_timeout_ms
Definition server.h:60
size_t max_header_size
Definition server.h:64
Definition connection.h:86
Composable event loop context.
Definition event_ctx.h:42
Definition file_io.h:16
Definition h2_server.h:74
Definition parser.h:18
Definition request.h:26
Definition router.h:42
Server load statistics — read-only snapshot for load-shedding decisions.
Definition server.h:159
int async_suspended
Definition server.h:162
int active_connections
Definition server.h:160
int listen_paused
Definition server.h:163
int max_connections
Definition server.h:161
Definition server.h:70
KlEventCtx ev
Definition server.h:78
KlConfig config
Definition server.h:71
int bound_port
Definition server.h:80
KlAllocator alloc_storage
Definition server.h:72
_Atomic int running
Definition server.h:82
KlRouter router
Definition server.h:76
KlFileIO * file_io
Definition server.h:86
KlAsyncOp * async_ops
Definition server.h:85
KlConnPool pool
Definition server.h:77
KlCompressConfig compress_storage
Definition server.h:75
KlError last_error
Definition server.h:87
KlH2ServerConfig h2_storage
Definition server.h:74
int listen_fd
Definition server.h:79
uint64_t drain_deadline_ms
Definition server.h:84
int listen_paused
Definition server.h:81
_Atomic int draining
Definition server.h:83
KlTlsConfig tls_storage
Definition server.h:73
TLS configuration for KlConfig.
Definition tls.h:108
Definition websocket_server.h:34
void * user_data
Definition websocket_server.h:36