Csilk 0.2.1
A lightweight, high-performance C HTTP web framework
Loading...
Searching...
No Matches
example_server.c File Reference

Full-featured example server using the low-level core API. More...

#include "csilk/core/internal.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "csilk/csilk.h"
#include "csilk/reflection/reflect.h"
Include dependency graph for example_server.c:

Data Structures

struct  login_request_t
 Login request body type for reflection. More...
 
struct  login_response_t
 Login response body type for reflection. More...
 
struct  user_profile_t
 User profile type for reflection. More...
 

Macros

#define LOGIN_REQUEST_MAP(_, ...)
 
#define LOGIN_RESPONSE_MAP(_, ...)
 
#define USER_PROFILE_MAP(_, ...)
 

Functions

void openapi_spec_handler (csilk_ctx_t *c)
 OpenAPI spec handler - serves the generated spec at runtime.
 
int mock_auth_validator (const char *token)
 Mock authentication validator — accepts only "secret123".
 
void hello_handler (csilk_ctx_t *c)
 Handler for GET / — returns welcome message.
 
void user_handler (csilk_ctx_t *c)
 Handler for GET /user/:id — returns the user ID from the path param.
 
void login_handler (csilk_ctx_t *c)
 Handler for POST /login — validates credentials and returns a token.
 
void protected_handler (csilk_ctx_t *c)
 Handler for GET /protected — returns content only if authorized.
 
void api_data_handler (csilk_ctx_t *c)
 Handler for GET /api/data — returns a JSON object with sample data.
 
static void sse_on_timer (csilk_ctx_t *c)
 SSE timer callback — sends counter events.
 
void events_handler (csilk_ctx_t *c)
 Handler for GET /events — SSE stream demo.
 
void upload_part_handler (csilk_multipart_part_t *part)
 Multipart part handler — prints file/field info from uploads.
 
void upload_handler (csilk_ctx_t *c)
 Handler for POST /upload — parses multipart form data.
 
void large_handler (csilk_ctx_t *c)
 Handler for GET /large — returns a sizable response for gzip demo.
 
void cookie_handler (csilk_ctx_t *c)
 Handler for GET/POST /cookie — set, read, or delete a demo cookie.
 
void request_id_middleware (csilk_ctx_t *c)
 Custom middleware — logs a sequential request ID.
 
void request_timer_middleware (csilk_ctx_t *c)
 Custom middleware — measures and logs request processing time.
 
int main (int argc, char *argv[])
 

Variables

static csilk_router_tg_router = NULL
 Global router reference for OpenAPI handler.
 

Detailed Description

Full-featured example server using the low-level core API.

Demonstrates router, groups, middleware, WebSocket, SSE, multipart upload, cookie handling, gzip compression, and YAML config loading.


Data Structure Documentation

◆ login_request_t

struct login_request_t

Login request body type for reflection.

Data Fields
char password[64]
char username[64]

◆ login_response_t

struct login_response_t

Login response body type for reflection.

Data Fields
char message[256]
char token[128]

◆ user_profile_t

struct user_profile_t

User profile type for reflection.

Data Fields
bool active
int id
char name[128]
double score

Macro Definition Documentation

◆ LOGIN_REQUEST_MAP

#define LOGIN_REQUEST_MAP (   _,
  ... 
)
Value:
username, \
sizeof(((login_request_t*)0)->username), \
0, \
false, \
NULL) \
password, \
sizeof(((login_request_t*)0)->password), \
0, \
false, \
NULL)
Login request body type for reflection.
Definition example_server.c:24
@ CSILK_TYPE_STRING
Definition reflect.h:42

◆ LOGIN_RESPONSE_MAP

#define LOGIN_RESPONSE_MAP (   _,
  ... 
)
Value:
token, \
sizeof(((login_response_t*)0)->token), \
0, \
false, \
NULL) \
message, \
sizeof(((login_response_t*)0)->message), \
0, \
false, \
NULL)
Login response body type for reflection.
Definition example_server.c:30

◆ USER_PROFILE_MAP

#define USER_PROFILE_MAP (   _,
  ... 
)
Value:
_(user_profile_t, id, CSILK_TYPE_INT32, sizeof(int), 0, false, NULL) \
name, \
sizeof(((user_profile_t*)0)->name), \
0, \
false, \
NULL) \
_(user_profile_t, score, CSILK_TYPE_DOUBLE, sizeof(double), 0, false, NULL) \
_(user_profile_t, active, CSILK_TYPE_BOOL, sizeof(bool), 0, false, NULL)
User profile type for reflection.
Definition example_server.c:36
@ CSILK_TYPE_INT32
Definition reflect.h:34
@ CSILK_TYPE_BOOL
Definition reflect.h:41
@ CSILK_TYPE_DOUBLE
Definition reflect.h:40

Function Documentation

◆ api_data_handler()

void api_data_handler ( csilk_ctx_t *  c)

Handler for GET /api/data — returns a JSON object with sample data.

◆ cookie_handler()

void cookie_handler ( csilk_ctx_t *  c)

Handler for GET/POST /cookie — set, read, or delete a demo cookie.

◆ events_handler()

void events_handler ( csilk_ctx_t *  c)

Handler for GET /events — SSE stream demo.

◆ hello_handler()

void hello_handler ( csilk_ctx_t *  c)

Handler for GET / — returns welcome message.

◆ large_handler()

void large_handler ( csilk_ctx_t *  c)

Handler for GET /large — returns a sizable response for gzip demo.

◆ login_handler()

void login_handler ( csilk_ctx_t *  c)

Handler for POST /login — validates credentials and returns a token.

◆ main()

int main ( int  argc,
char *  argv[] 
)

◆ mock_auth_validator()

int mock_auth_validator ( const char *  token)

Mock authentication validator — accepts only "secret123".

◆ openapi_spec_handler()

void openapi_spec_handler ( csilk_ctx_t *  c)

OpenAPI spec handler - serves the generated spec at runtime.

◆ protected_handler()

void protected_handler ( csilk_ctx_t *  c)

Handler for GET /protected — returns content only if authorized.

◆ request_id_middleware()

void request_id_middleware ( csilk_ctx_t *  c)

Custom middleware — logs a sequential request ID.

◆ request_timer_middleware()

void request_timer_middleware ( csilk_ctx_t *  c)

Custom middleware — measures and logs request processing time.

◆ sse_on_timer()

static void sse_on_timer ( csilk_ctx_t *  c)
static

SSE timer callback — sends counter events.

◆ upload_handler()

void upload_handler ( csilk_ctx_t *  c)

Handler for POST /upload — parses multipart form data.

◆ upload_part_handler()

void upload_part_handler ( csilk_multipart_part_t part)

Multipart part handler — prints file/field info from uploads.

◆ user_handler()

void user_handler ( csilk_ctx_t *  c)

Handler for GET /user/:id — returns the user ID from the path param.

Variable Documentation

◆ g_router

csilk_router_t* g_router = NULL
static

Global router reference for OpenAPI handler.