Csilk 0.2.1
A lightweight, high-performance C HTTP web framework
Loading...
Searching...
No Matches
app.h File Reference
#include "csilk/csilk.h"
Include dependency graph for app.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define csilk_app_get(app, path, handler)   csilk_app_add_route(app, "GET", path, handler)
 Convenience macro to register a GET route via the app API.
 
#define csilk_app_get_ext(app, path, handler, in, out, summary, desc)    csilk_app_add_route_extended(app, "GET", path, handler, in, out, summary, desc)
 Convenience macro to register a GET route with OpenAPI metadata.
 
#define csilk_app_get_perm(app, path, handler, perm, res)    csilk_app_add_route_perm(app, "GET", path, handler, perm, res)
 Convenience macro to register a GET route with permission.
 
#define csilk_app_post(app, path, handler)   csilk_app_add_route(app, "POST", path, handler)
 Convenience macro to register a POST route via the app API.
 
#define csilk_app_post_ext(app, path, handler, in, out, summary, desc)    csilk_app_add_route_extended(app, "POST", path, handler, in, out, summary, desc)
 Convenience macro to register a POST route with OpenAPI metadata.
 
#define csilk_app_post_perm(app, path, handler, perm, res)    csilk_app_add_route_perm(app, "POST", path, handler, perm, res)
 Convenience macro to register a POST route with permission.
 
#define csilk_app_put(app, path, handler)   csilk_app_add_route(app, "PUT", path, handler)
 Convenience macro to register a PUT route via the app API.
 
#define csilk_app_put_ext(app, path, handler, in, out, summary, desc)    csilk_app_add_route_extended(app, "PUT", path, handler, in, out, summary, desc)
 Convenience macro to register a PUT route with OpenAPI metadata.
 
#define csilk_app_put_perm(app, path, handler, perm, res)    csilk_app_add_route_perm(app, "PUT", path, handler, perm, res)
 Convenience macro to register a PUT route with permission.
 
#define csilk_app_delete(app, path, handler)   csilk_app_add_route(app, "DELETE", path, handler)
 Convenience macro to register a DELETE route via the app API.
 
#define csilk_app_delete_ext(app, path, handler, in, out, summary, desc)    csilk_app_add_route_extended(app, "DELETE", path, handler, in, out, summary, desc)
 Convenience macro to register a DELETE route with OpenAPI metadata.
 
#define csilk_app_delete_perm(app, path, handler, perm, res)    csilk_app_add_route_perm(app, "DELETE", path, handler, perm, res)
 Convenience macro to register a DELETE route with permission.
 
#define csilk_app_patch(app, path, handler)   csilk_app_add_route(app, "PATCH", path, handler)
 Convenience macro to register a PATCH route via the app API.
 
#define csilk_app_patch_ext(app, path, handler, in, out, summary, desc)    csilk_app_add_route_extended(app, "PATCH", path, handler, in, out, summary, desc)
 Convenience macro to register a PATCH route with OpenAPI metadata.
 
#define csilk_app_patch_perm(app, path, handler, perm, res)    csilk_app_add_route_perm(app, "PATCH", path, handler, perm, res)
 Convenience macro to register a PATCH route with permission.
 
#define csilk_app_options(app, path, handler)   csilk_app_add_route(app, "OPTIONS", path, handler)
 Convenience macro to register an OPTIONS route via the app API.
 
#define csilk_app_options_ext(app, path, handler, in, out, summary, desc)    csilk_app_add_route_extended(app, "OPTIONS", path, handler, in, out, summary, desc)
 Convenience macro to register an OPTIONS route with OpenAPI metadata.
 
#define csilk_app_options_perm(app, path, handler, perm, res)    csilk_app_add_route_perm(app, "OPTIONS", path, handler, perm, res)
 Convenience macro to register an OPTIONS route with permission.
 
#define csilk_app_head(app, path, handler)   csilk_app_add_route(app, "HEAD", path, handler)
 Convenience macro to register a HEAD route via the app API.
 
#define csilk_app_head_ext(app, path, handler, in, out, summary, desc)    csilk_app_add_route_extended(app, "HEAD", path, handler, in, out, summary, desc)
 Convenience macro to register a HEAD route with OpenAPI metadata.
 
#define csilk_app_head_perm(app, path, handler, perm, res)    csilk_app_add_route_perm(app, "HEAD", path, handler, perm, res)
 Convenience macro to register a HEAD route with permission.
 

Functions

csilk_app_t * csilk_app_new (const char *config_path)
 Create a new application with optional YAML config.
 
void csilk_app_free (csilk_app_t *app)
 Deallocate all application resources.
 
void csilk_app_log_level (csilk_app_t *app, csilk_log_level_t level)
 Set the minimum log level.
 
void csilk_app_log_file (csilk_app_t *app, const char *path, size_t max_sz)
 Enable file logging with optional rotation.
 
void csilk_app_log_json (csilk_app_t *app, int enable)
 Enable or disable JSON structured log output.
 
void csilk_app_use (csilk_app_t *app, csilk_handler_t h)
 Register a global middleware that runs on every route.
 
void csilk_app_use_group (csilk_app_t *app, const char *prefix, csilk_handler_t h)
 Register a middleware that runs only on a specific prefix group.
 
void csilk_app_apply_config (csilk_app_t *app)
 Auto-apply built-in middleware based on current config.
 
void csilk_app_add_route (csilk_app_t *app, const char *method, const char *path, csilk_handler_t handler)
 Register a route with a single handler.
 
void csilk_app_add_handlers (csilk_app_t *app, const char *method, const char *path, csilk_handler_t *handlers, size_t n)
 Register a route with multiple handlers (middleware + handler).
 
void csilk_app_add_route_extended (csilk_app_t *app, const char *method, const char *path, csilk_handler_t handler, const char *input_type, const char *output_type, const char *summary, const char *description)
 Register a route with OpenAPI metadata (input/output types).
 
void csilk_app_add_route_perm (csilk_app_t *app, const char *method, const char *path, csilk_handler_t handler, const char *perm_required, const char *perm_resource)
 Register a route with permission metadata.
 
void csilk_app_add_route_extended_perm (csilk_app_t *app, 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)
 Register a route with full metadata including permissions.
 
void csilk_app_static (csilk_app_t *app, const char *prefix, const char *root_dir)
 Serve static files from a local directory.
 
void csilk_app_set_server_config (csilk_app_t *app, csilk_server_config_t c)
 Apply server-level configuration (timeouts, limits, TCP options).
 
csilk_config_tcsilk_app_config (csilk_app_t *app)
 Get a copy of the current application configuration.
 
void csilk_app_enable_openapi (csilk_app_t *app, int enable)
 Enable or disable the built-in /openapi.json endpoint. The endpoint is enabled by default when the app is created.
 
int csilk_app_run (csilk_app_t *app, int port)
 Start the server and block until stopped (Ctrl+C).
 
csilk_router_tcsilk_app_router (csilk_app_t *app)
 Get the underlying router for advanced operations.
 
csilk_server_t * csilk_app_server (csilk_app_t *app)
 Get the underlying server for advanced operations.
 

Macro Definition Documentation

◆ csilk_app_delete

#define csilk_app_delete (   app,
  path,
  handler 
)    csilk_app_add_route(app, "DELETE", path, handler)

Convenience macro to register a DELETE route via the app API.

Parameters
appApplication handle.
pathURL path pattern.
handlerHandler function.

◆ csilk_app_delete_ext

#define csilk_app_delete_ext (   app,
  path,
  handler,
  in,
  out,
  summary,
  desc 
)     csilk_app_add_route_extended(app, "DELETE", path, handler, in, out, summary, desc)

Convenience macro to register a DELETE route with OpenAPI metadata.

◆ csilk_app_delete_perm

#define csilk_app_delete_perm (   app,
  path,
  handler,
  perm,
  res 
)     csilk_app_add_route_perm(app, "DELETE", path, handler, perm, res)

Convenience macro to register a DELETE route with permission.

◆ csilk_app_get

#define csilk_app_get (   app,
  path,
  handler 
)    csilk_app_add_route(app, "GET", path, handler)

Convenience macro to register a GET route via the app API.

Parameters
appApplication handle.
pathURL path pattern.
handlerHandler function.

◆ csilk_app_get_ext

#define csilk_app_get_ext (   app,
  path,
  handler,
  in,
  out,
  summary,
  desc 
)     csilk_app_add_route_extended(app, "GET", path, handler, in, out, summary, desc)

Convenience macro to register a GET route with OpenAPI metadata.

◆ csilk_app_get_perm

#define csilk_app_get_perm (   app,
  path,
  handler,
  perm,
  res 
)     csilk_app_add_route_perm(app, "GET", path, handler, perm, res)

Convenience macro to register a GET route with permission.

◆ csilk_app_head

#define csilk_app_head (   app,
  path,
  handler 
)    csilk_app_add_route(app, "HEAD", path, handler)

Convenience macro to register a HEAD route via the app API.

Parameters
appApplication handle.
pathURL path pattern.
handlerHandler function.

◆ csilk_app_head_ext

#define csilk_app_head_ext (   app,
  path,
  handler,
  in,
  out,
  summary,
  desc 
)     csilk_app_add_route_extended(app, "HEAD", path, handler, in, out, summary, desc)

Convenience macro to register a HEAD route with OpenAPI metadata.

◆ csilk_app_head_perm

#define csilk_app_head_perm (   app,
  path,
  handler,
  perm,
  res 
)     csilk_app_add_route_perm(app, "HEAD", path, handler, perm, res)

Convenience macro to register a HEAD route with permission.

◆ csilk_app_options

#define csilk_app_options (   app,
  path,
  handler 
)    csilk_app_add_route(app, "OPTIONS", path, handler)

Convenience macro to register an OPTIONS route via the app API.

Parameters
appApplication handle.
pathURL path pattern.
handlerHandler function.

◆ csilk_app_options_ext

#define csilk_app_options_ext (   app,
  path,
  handler,
  in,
  out,
  summary,
  desc 
)     csilk_app_add_route_extended(app, "OPTIONS", path, handler, in, out, summary, desc)

Convenience macro to register an OPTIONS route with OpenAPI metadata.

◆ csilk_app_options_perm

#define csilk_app_options_perm (   app,
  path,
  handler,
  perm,
  res 
)     csilk_app_add_route_perm(app, "OPTIONS", path, handler, perm, res)

Convenience macro to register an OPTIONS route with permission.

◆ csilk_app_patch

#define csilk_app_patch (   app,
  path,
  handler 
)    csilk_app_add_route(app, "PATCH", path, handler)

Convenience macro to register a PATCH route via the app API.

Parameters
appApplication handle.
pathURL path pattern.
handlerHandler function.

◆ csilk_app_patch_ext

#define csilk_app_patch_ext (   app,
  path,
  handler,
  in,
  out,
  summary,
  desc 
)     csilk_app_add_route_extended(app, "PATCH", path, handler, in, out, summary, desc)

Convenience macro to register a PATCH route with OpenAPI metadata.

◆ csilk_app_patch_perm

#define csilk_app_patch_perm (   app,
  path,
  handler,
  perm,
  res 
)     csilk_app_add_route_perm(app, "PATCH", path, handler, perm, res)

Convenience macro to register a PATCH route with permission.

◆ csilk_app_post

#define csilk_app_post (   app,
  path,
  handler 
)    csilk_app_add_route(app, "POST", path, handler)

Convenience macro to register a POST route via the app API.

Parameters
appApplication handle.
pathURL path pattern.
handlerHandler function.

◆ csilk_app_post_ext

#define csilk_app_post_ext (   app,
  path,
  handler,
  in,
  out,
  summary,
  desc 
)     csilk_app_add_route_extended(app, "POST", path, handler, in, out, summary, desc)

Convenience macro to register a POST route with OpenAPI metadata.

◆ csilk_app_post_perm

#define csilk_app_post_perm (   app,
  path,
  handler,
  perm,
  res 
)     csilk_app_add_route_perm(app, "POST", path, handler, perm, res)

Convenience macro to register a POST route with permission.

◆ csilk_app_put

#define csilk_app_put (   app,
  path,
  handler 
)    csilk_app_add_route(app, "PUT", path, handler)

Convenience macro to register a PUT route via the app API.

Parameters
appApplication handle.
pathURL path pattern.
handlerHandler function.

◆ csilk_app_put_ext

#define csilk_app_put_ext (   app,
  path,
  handler,
  in,
  out,
  summary,
  desc 
)     csilk_app_add_route_extended(app, "PUT", path, handler, in, out, summary, desc)

Convenience macro to register a PUT route with OpenAPI metadata.

◆ csilk_app_put_perm

#define csilk_app_put_perm (   app,
  path,
  handler,
  perm,
  res 
)     csilk_app_add_route_perm(app, "PUT", path, handler, perm, res)

Convenience macro to register a PUT route with permission.

Function Documentation

◆ csilk_app_add_handlers()

void csilk_app_add_handlers ( csilk_app_t *  app,
const char *  method,
const char *  path,
csilk_handler_t handlers,
size_t  n 
)

Register a route with multiple handlers (middleware + handler).

Parameters
appApplication handle.
methodHTTP method string.
pathURL pattern.
handlersArray of handler functions.
nNumber of handlers.

Register a route with multiple handlers (middleware + handler).

Parameters
appApplication instance.
methodHTTP method.
pathURL path.
handlersArray of handler functions.
nNumber of handlers in the array.
Examples
/home/runner/work/csilk/csilk/include/csilk/app/app.h.

◆ csilk_app_add_route()

void csilk_app_add_route ( csilk_app_t *  app,
const char *  method,
const char *  path,
csilk_handler_t  handler 
)

Register a route with a single handler.

Parameters
appApplication handle.
methodHTTP method string (e.g., "GET").
pathURL pattern (supports :param and *wildcard).
handlerRoute handler function.

Register a route with a single handler.

Parameters
appApplication instance.
methodHTTP method (e.g., "GET", "POST").
pathURL path (e.g., "/users").
handlerHandler function.
Examples
/home/runner/work/csilk/csilk/include/csilk/app/app.h.

◆ csilk_app_add_route_extended()

void csilk_app_add_route_extended ( csilk_app_t *  app,
const char *  method,
const char *  path,
csilk_handler_t  handler,
const char *  input_type,
const char *  output_type,
const char *  summary,
const char *  description 
)

Register a route with OpenAPI metadata (input/output types).

Parameters
appApplication handle.
methodHTTP method string.
pathURL pattern (supports :param and *wildcard).
handlerRoute handler function.
input_typeRegistered type name for request body (NULL if none).
output_typeRegistered type name for response (NULL if none).
summaryShort operation summary (NULL if none).
descriptionDetailed operation description (NULL if none).

Register a route with OpenAPI metadata (input/output types).

Parameters
appApplication instance.
methodHTTP method.
pathURL path.
handlerHandler function.
input_typeRegistered type name for request body JSON schema.
output_typeRegistered type name for response body JSON schema.
summaryShort description for the OpenAPI operation.
descriptionDetailed description for the OpenAPI operation.
Examples
/home/runner/work/csilk/csilk/include/csilk/app/app.h.

◆ csilk_app_add_route_extended_perm()

void csilk_app_add_route_extended_perm ( csilk_app_t *  app,
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 
)

Register a route with full metadata including permissions.

Parameters
appApplication handle.
methodHTTP method string.
pathURL pattern.
handlerRoute handler function.
input_typeRegistered type name for request body (NULL if none).
output_typeRegistered type name for response (NULL if none).
summaryShort operation summary (NULL if none).
descriptionDetailed operation description (NULL if none).
perm_requiredPermission identifier (e.g., "read"), or NULL.
perm_resourceResource pattern (e.g., "users:*"), or NULL.

Register a route with full metadata including permissions.

Parameters
appApplication handle.
methodHTTP method string.
pathURL pattern (supports :param and *wildcard).
handlerRoute handler function.
input_typeRegistered type name for request body (NULL if none).
output_typeRegistered type name for response (NULL if none).
summaryShort operation summary (NULL if none).
descriptionDetailed operation description (NULL if none).

Register a route with OpenAPI metadata (input/output types).

Parameters
appApplication instance.
methodHTTP method.
pathURL path.
handlerHandler function.
input_typeRegistered type name for request body JSON schema.
output_typeRegistered type name for response body JSON schema.
summaryShort description for the OpenAPI operation.
descriptionDetailed description for the OpenAPI operation.
perm_requiredPermission required for this route, or NULL.
perm_resourceResource pattern for permission check, or NULL.
Examples
/home/runner/work/csilk/csilk/include/csilk/app/app.h.

◆ csilk_app_add_route_perm()

void csilk_app_add_route_perm ( csilk_app_t *  app,
const char *  method,
const char *  path,
csilk_handler_t  handler,
const char *  perm_required,
const char *  perm_resource 
)

Register a route with permission metadata.

Parameters
appApplication handle.
methodHTTP method string.
pathURL pattern (supports :param and *wildcard).
handlerRoute handler function.
perm_requiredPermission identifier (e.g., "read"), or NULL.
perm_resourceResource pattern (e.g., "users:*"), or NULL.
appApplication instance.
methodHTTP method.
pathURL path.
handlerHandler function.
perm_requiredPermission identifier (e.g., "read"), or NULL.
perm_resourceResource pattern (e.g., "users:*"), or NULL.
Examples
/home/runner/work/csilk/csilk/include/csilk/app/app.h.

◆ csilk_app_apply_config()

void csilk_app_apply_config ( csilk_app_t *  app)

Auto-apply built-in middleware based on current config.

Reads the loaded YAML configuration and installs the following middleware when the corresponding config flags are enabled: logger, recovery, CORS, CSRF, rate-limit, auth, gzip. Safe to call even without a loaded config — disabled options are no-ops.

Parameters
appApplication handle.

Auto-apply built-in middleware based on current config.

Reads the current app config and sets up static file serving if config.static_files.enable is true and root_dir is configured. The prefix defaults to "/static" if not specified in the config.

Parameters
appApplication instance.
Examples
/home/runner/work/csilk/csilk/include/csilk/app/app.h.

◆ csilk_app_config()

csilk_config_t * csilk_app_config ( csilk_app_t *  app)

Get a copy of the current application configuration.

Parameters
appApplication handle.
Returns
Copy of internal config (caller must csilk_config_free it), or NULL on error.

Get a copy of the current application configuration.

Parameters
appApplication instance.
Returns
A malloc'd copy of the configuration struct. The caller must free it with free(). Returns NULL if app is NULL or allocation fails.
Note
The returned copy includes deep copies of any dynamically allocated string fields? No — it is a shallow memcpy. Use csilk_config_free() only if you modify strings separately.
Examples
/home/runner/work/csilk/csilk/include/csilk/app/app.h.

◆ csilk_app_enable_openapi()

void csilk_app_enable_openapi ( csilk_app_t *  app,
int  enable 
)

Enable or disable the built-in /openapi.json endpoint. The endpoint is enabled by default when the app is created.

Parameters
appApplication handle.
enable1 to enable, 0 to disable (handler returns 404).

Enable or disable the built-in /openapi.json endpoint. The endpoint is enabled by default when the app is created.

When enabled, the router's routes are exposed as an OpenAPI 3.0 specification at /openapi.json. When disabled, the endpoint returns 404.

Parameters
appApplication instance.
enable1 to enable, 0 to disable.
Examples
/home/runner/work/csilk/csilk/include/csilk/app/app.h.

◆ csilk_app_free()

void csilk_app_free ( csilk_app_t *  app)

Deallocate all application resources.

Stops the server (if running), frees the router, config, and any registered middleware. Safe to call with a NULL app.

Parameters
appApplication handle (may be NULL).

Deallocate all application resources.

Teardown order (reverse of init)

  1. Close logger — stops accepting log entries.
  2. Free server — joins worker threads, closes connections, stops libuv.
  3. Free cached child groups (groups[] array).
  4. Free root group.
  5. Free router — releases all registered routes and OpenAPI metadata.
  6. Free config — releases dynamically allocated strings (log path, etc.).
  7. Free the app struct itself.

Server must be freed before the router because the server holds a reference to the router internally.

Parameters
appThe application instance to free (may be NULL).
Note
Safe to call with NULL. After this call the app pointer is invalid.
Examples
/home/runner/work/csilk/csilk/include/csilk/app/app.h.

◆ csilk_app_log_file()

void csilk_app_log_file ( csilk_app_t *  app,
const char *  path,
size_t  max_sz 
)

Enable file logging with optional rotation.

Parameters
appApplication handle.
pathLog file path.
max_szMax file size before rotation (0 to disable).

Enable file logging with optional rotation.

Sets the log output to the specified file path. If max_sz > 0, the log file is rotated (renamed to .1) when it exceeds this size.

Parameters
appApplication instance.
pathFile path for log output. Pass NULL to disable file logging and revert to stdout.
max_szMaximum file size in bytes before rotation (0 = no limit).
Examples
/home/runner/work/csilk/csilk/include/csilk/app/app.h.

◆ csilk_app_log_json()

void csilk_app_log_json ( csilk_app_t *  app,
int  enable 
)

Enable or disable JSON structured log output.

Parameters
appApplication handle.
enable1 to enable JSON format, 0 for plain text.

Enable or disable JSON structured log output.

When enabled, log entries are emitted as JSON objects with structured fields (timestamp, level, file, line, message, request_id). When disabled, plain text format is used.

Parameters
appApplication instance.
enable1 for JSON format, 0 for plain text.
Examples
/home/runner/work/csilk/csilk/include/csilk/app/app.h.

◆ csilk_app_log_level()

void csilk_app_log_level ( csilk_app_t *  app,
csilk_log_level_t  level 
)

Set the minimum log level.

Parameters
appApplication handle.
levelMinimum log level.

Set the minimum log level.

Reinitializes the logger with the updated level. Messages below this level are filtered out.

Parameters
appApplication instance.
levelNew minimum log level (e.g., CSILK_LOG_DEBUG).
Examples
/home/runner/work/csilk/csilk/include/csilk/app/app.h.

◆ csilk_app_new()

csilk_app_t * csilk_app_new ( const char *  config_path)

Create a new application with optional YAML config.

Initialises the router, server, logger, database subsystem, and AI subsystem. If config_path is provided and readable, settings for CORS, rate-limiting, static files, auth, AI, and cipher are loaded. If config_path is NULL or the file doesn't exist, sensible defaults are used (port 8080, stderr logging, all middleware disabled).

Parameters
config_pathPath to YAML config file, or NULL for defaults.
Returns
New application handle, or NULL on fatal error.

Create a new application with optional YAML config.

Bootstrap sequence (step-by-step)

Phase 1 — Config & Logging

  1. Allocate app struct (calloc). Set up the process-level app mutex (uv_once, thread-safe for concurrent csilk_app_new calls).
  2. Load YAML config from config_path, or apply hard-coded defaults (port 8080, info-level logging, 5 s idle timeout, 30 s I/O timeouts, 1 MB max body, 64 KB max header, 8 KB max URL, 100 headers, 128 backlog, TCP_NODELAY on).
  3. Initialize the logger from config.

Phase 2 — Core objects

  1. Create router + server, wire them together.
  2. Push built-in middleware onto the server's global middleware chain: recovery handler first, then request logger. Order matters — recovery must wrap everything.
  3. Create the root route group (prefix "").

Phase 3 — Built-in endpoints

  1. Register the global OpenAPI router reference so /openapi.json works.
  2. Register /openapi.json (GET) — reads the router's OpenAPI spec.
  3. Register /docs (GET) — serves embedded Swagger UI HTML.

Register /csdk-docs/ as a static file route pointing to the bundled Swagger UI assets.

Parameters
config_pathPath to a YAML configuration file, or NULL to use defaults (port 8080, info-level logging to stdout).
Returns
A new csilk_app_t instance, or NULL on initialization failure.
Note
The returned app must be freed with csilk_app_free(). On failure, any partially allocated resources are cleaned up internally.
Examples
/home/runner/work/csilk/csilk/include/csilk/app/app.h.

◆ csilk_app_router()

csilk_router_t * csilk_app_router ( csilk_app_t *  app)

Get the underlying router for advanced operations.

Parameters
appApplication handle.
Returns
Router handle.

Get the underlying router for advanced operations.

Parameters
appApplication instance.
Returns
The router pointer, or NULL if app is NULL.
Examples
/home/runner/work/csilk/csilk/include/csilk/app/app.h.

◆ csilk_app_run()

int csilk_app_run ( csilk_app_t *  app,
int  port 
)

Start the server and block until stopped (Ctrl+C).

Parameters
appApplication handle.
portTCP port to listen on.
Returns
0 on clean exit, -1 on error.

Start the server and block until stopped (Ctrl+C).

This is the main entry point into the event-driven I/O loop. It delegates to csilk_server_run() which:

  1. Creates a TCP listener on the given port.
  2. Spawns worker threads (if thread pool is configured).
  3. Calls uv_run(UV_RUN_DEFAULT) — blocks until the loop stops.
Parameters
appApplication instance.
portTCP port to listen on. Pass 0 or negative to use the port from the application config (default 8080).
Returns
The uv_run() return value on exit, or -1 on error.
Examples
/home/runner/work/csilk/csilk/include/csilk/app/app.h.

◆ csilk_app_server()

csilk_server_t * csilk_app_server ( csilk_app_t *  app)

Get the underlying server for advanced operations.

Parameters
appApplication handle.
Returns
Server handle.

Get the underlying server for advanced operations.

Parameters
appApplication instance.
Returns
The server pointer, or NULL if app is NULL.
Examples
/home/runner/work/csilk/csilk/include/csilk/app/app.h.

◆ csilk_app_set_server_config()

void csilk_app_set_server_config ( csilk_app_t *  app,
csilk_server_config_t  c 
)

Apply server-level configuration (timeouts, limits, TCP options).

Parameters
appApplication handle.
cServer configuration struct.

Apply server-level configuration (timeouts, limits, TCP options).

Updates both the app's stored config and applies it to the server instance. This overrides any previous server config.

Parameters
appApplication instance.
cServer configuration struct.
Examples
/home/runner/work/csilk/csilk/include/csilk/app/app.h.

◆ csilk_app_static()

void csilk_app_static ( csilk_app_t *  app,
const char *  prefix,
const char *  root_dir 
)

Serve static files from a local directory.

Parameters
appApplication handle.
prefixURL path prefix (e.g., "/static").
root_dirLocal directory to serve files from.

Serve static files from a local directory.

What it does

  1. Acquires the app mutex, checks the g_static[] table capacity (max 32).
  2. Writes url_prefix + root_dir into the next free global slot.
  3. Releases mutex, then registers two GET routes on the group matching prefix:
    • WILDCARD path — wildcard route that captures everything after the prefix.
    • / — the prefix root (redirects to the index file). Both routes use the same internal static_serve handler, which scans g_static[] at request time to find the correct root_dir.

The dual-route pattern means both /static/ and /static/foo/bar.jpg work. The static_prefix context variable lets csilk_static() strip the URL prefix from the filesystem path.

Parameters
appApplication instance.
prefixURL path prefix for static files (e.g., "/static").
root_dirLocal filesystem directory to serve files from.
Note
Routes are added using the app's internal static_serve handler which dispatches to csilk_static() with the correct root directory.
Examples
/home/runner/work/csilk/csilk/include/csilk/app/app.h.

◆ csilk_app_use()

void csilk_app_use ( csilk_app_t *  app,
csilk_handler_t  h 
)

Register a global middleware that runs on every route.

Middleware is executed in registration order for every request, before the route-specific handler. The onion model applies — code before csilk_next() runs on the way in, code after runs on the way out.

Parameters
appApplication handle.
hMiddleware handler function.

Register a global middleware that runs on every route.

Global middleware runs before route-specific middleware and handlers for every request. Built-in recovery and logger middleware are registered automatically by csilk_app_new().

Parameters
appApplication instance.
hMiddleware handler function.
Note
There is a hard limit of 32 global middlewares.
Examples
/home/runner/work/csilk/csilk/include/csilk/app/app.h.

◆ csilk_app_use_group()

void csilk_app_use_group ( csilk_app_t *  app,
const char *  prefix,
csilk_handler_t  h 
)

Register a middleware that runs only on a specific prefix group.

Group middleware is prepended to routes whose path matches prefix. For example, a middleware registered with prefix "/api" runs on all "/api/..." routes but not on "/health".

Parameters
appApplication handle.
prefixURL path prefix (e.g., "/api").
hMiddleware handler function.

Register a middleware that runs only on a specific prefix group.

Creates (or finds) a route group for the given prefix and adds the middleware to it. The middleware runs for any route whose path starts with the given prefix.

Parameters
appApplication instance.
prefixURL prefix (e.g., "/api/admin").
hMiddleware handler function.
Examples
/home/runner/work/csilk/csilk/include/csilk/app/app.h.