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

Example using the high-level csilk_app_t API. More...

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "csilk/app/app.h"
#include "csilk/csilk.h"
#include "csilk/reflection/reflect.h"
Include dependency graph for example_app.c:

Data Structures

struct  reflect_address_t
 Nested address type. More...
 
struct  reflect_user_t
 Complex user profile with nested structs, arrays, and mixed types. More...
 
struct  reflect_order_item_t
 Order item inside a nested array. More...
 
struct  reflect_order_t
 Full order with nested items array via struct fields. More...
 

Macros

#define REFLECT_ADDRESS_MAP(_, ...)
 
#define REFLECT_USER_MAP(_, ...)
 
#define REFLECT_ORDER_ITEM_MAP(_, ...)
 
#define REFLECT_ORDER_MAP(_, ...)
 

Functions

static void hello (csilk_ctx_t *c)
 Handler for GET / — "Hello from csilk easy API!".
 
static void user (csilk_ctx_t *c)
 Handler for GET /user/:id — returns the user ID from path param.
 
static void ping (csilk_ctx_t *c)
 Handler for GET /ping — returns a JSON status response.
 
static void login (csilk_ctx_t *c)
 Handler for POST /login — authenticates user and returns a token.
 
static void echo (csilk_ctx_t *c)
 Handler for GET /echo?msg=... — echoes back the query parameter.
 
static void create_user (csilk_ctx_t *c)
 Handler for POST /api/users — creates a user via reflection binding. Accepts JSON body with nested address, arrays, booleans, etc.
 
static void get_user (csilk_ctx_t *c)
 Handler for GET /api/users/:id — returns a user profile via reflection.
 
static void create_order (csilk_ctx_t *c)
 Handler for POST /api/orders — creates an order via reflection binding.
 
static void get_order (csilk_ctx_t *c)
 Handler for GET /api/orders/:id — returns a mock order via reflection.
 
static void sse_handler (csilk_ctx_t *c)
 Handler for GET /stream — SSE connection demo.
 
static void timer_mw (csilk_ctx_t *c)
 Custom middleware — measures and logs request processing time.
 
int main (void)
 

Detailed Description

Example using the high-level csilk_app_t API.

Demonstrates csilk_app_new, reflection binding, nested structs, arrays, route groups, SSE, gzip, and Swagger UI.


Data Structure Documentation

◆ reflect_address_t

struct reflect_address_t

Nested address type.

Data Fields
char city[64]
bool is_primary
char street[128]
char zip[16]

◆ reflect_user_t

struct reflect_user_t

Complex user profile with nested structs, arrays, and mixed types.

Collaboration diagram for reflect_user_t:
Data Fields
bool active
reflect_address_t address
char * bio
char email[128]
uint8_t flags[4]
int64_t id
int16_t level
char name[64]
float rating
double score

◆ reflect_order_item_t

struct reflect_order_item_t

Order item inside a nested array.

Data Fields
int32_t quantity
char sku[32]
double unit_price

◆ reflect_order_t

struct reflect_order_t

Full order with nested items array via struct fields.

Collaboration diagram for reflect_order_t:
Data Fields
char customer_email[128]
reflect_order_item_t items[3]
char notes[256]
int64_t order_id
double total

Macro Definition Documentation

◆ REFLECT_ADDRESS_MAP

#define REFLECT_ADDRESS_MAP (   _,
  ... 
)
Value:
street, \
sizeof(((reflect_address_t*)0)->street), \
0, \
false, \
NULL) \
city, \
sizeof(((reflect_address_t*)0)->city), \
0, \
false, \
NULL) \
zip, \
sizeof(((reflect_address_t*)0)->zip), \
0, \
false, \
NULL) \
_(reflect_address_t, is_primary, CSILK_TYPE_BOOL, 0, 0, false, NULL)
Nested address type.
Definition example_app.c:28
@ CSILK_TYPE_STRING
Definition reflect.h:42
@ CSILK_TYPE_BOOL
Definition reflect.h:41

◆ REFLECT_ORDER_ITEM_MAP

#define REFLECT_ORDER_ITEM_MAP (   _,
  ... 
)
Value:
sku, \
sizeof(((reflect_order_item_t*)0)->sku), \
0, \
false, \
NULL) \
_(reflect_order_item_t, quantity, CSILK_TYPE_INT32, 0, 0, false, NULL) \
_(reflect_order_item_t, unit_price, CSILK_TYPE_DOUBLE, 0, 0, false, NULL)
Order item inside a nested array.
Definition example_app.c:50
@ CSILK_TYPE_INT32
Definition reflect.h:34
@ CSILK_TYPE_DOUBLE
Definition reflect.h:40

◆ REFLECT_ORDER_MAP

#define REFLECT_ORDER_MAP (   _,
  ... 
)
Value:
_(reflect_order_t, order_id, CSILK_TYPE_INT64, 0, 0, false, NULL) \
customer_email, \
sizeof(((reflect_order_t*)0)->customer_email), \
0, \
false, \
NULL) \
_(reflect_order_t, total, CSILK_TYPE_DOUBLE, 0, 0, false, NULL) \
items, \
3, \
false, \
"reflect_order_item_t") \
notes, \
sizeof(((reflect_order_t*)0)->notes), \
0, \
false, \
NULL)
Full order with nested items array via struct fields.
Definition example_app.c:57
@ CSILK_TYPE_STRUCT
Definition reflect.h:44
@ CSILK_TYPE_INT64
Definition reflect.h:36

◆ REFLECT_USER_MAP

#define REFLECT_USER_MAP (   _,
  ... 
)
Value:
_(reflect_user_t, id, CSILK_TYPE_INT64, 0, 0, false, NULL) \
name, \
sizeof(((reflect_user_t*)0)->name), \
0, \
false, \
NULL) \
email, \
sizeof(((reflect_user_t*)0)->email), \
0, \
false, \
NULL) \
_(reflect_user_t, active, CSILK_TYPE_BOOL, 0, 0, false, NULL) \
_(reflect_user_t, score, CSILK_TYPE_DOUBLE, 0, 0, false, NULL) \
_(reflect_user_t, rating, CSILK_TYPE_FLOAT, 0, 0, false, NULL) \
_(reflect_user_t, level, CSILK_TYPE_INT16, 0, 0, false, NULL) \
_(reflect_user_t, flags, CSILK_TYPE_UINT8, sizeof(uint8_t), 4, false, NULL) \
address, \
sizeof(reflect_address_t), \
0, \
false, \
"reflect_address_t") \
_(reflect_user_t, bio, CSILK_TYPE_STRING, 0, 0, true, NULL)
Complex user profile with nested structs, arrays, and mixed types.
Definition example_app.c:36
@ CSILK_TYPE_INT16
Definition reflect.h:31
@ CSILK_TYPE_UINT8
Definition reflect.h:30
@ CSILK_TYPE_FLOAT
Definition reflect.h:39

Function Documentation

◆ create_order()

static void create_order ( csilk_ctx_t *  c)
static

Handler for POST /api/orders — creates an order via reflection binding.

◆ create_user()

static void create_user ( csilk_ctx_t *  c)
static

Handler for POST /api/users — creates a user via reflection binding. Accepts JSON body with nested address, arrays, booleans, etc.

◆ echo()

static void echo ( csilk_ctx_t *  c)
static

Handler for GET /echo?msg=... — echoes back the query parameter.

◆ get_order()

static void get_order ( csilk_ctx_t *  c)
static

Handler for GET /api/orders/:id — returns a mock order via reflection.

◆ get_user()

static void get_user ( csilk_ctx_t *  c)
static

Handler for GET /api/users/:id — returns a user profile via reflection.

◆ hello()

static void hello ( csilk_ctx_t *  c)
static

Handler for GET / — "Hello from csilk easy API!".

◆ login()

static void login ( csilk_ctx_t *  c)
static

Handler for POST /login — authenticates user and returns a token.

◆ main()

int main ( void  )

◆ ping()

static void ping ( csilk_ctx_t *  c)
static

Handler for GET /ping — returns a JSON status response.

◆ sse_handler()

static void sse_handler ( csilk_ctx_t *  c)
static

Handler for GET /stream — SSE connection demo.

◆ timer_mw()

static void timer_mw ( csilk_ctx_t *  c)
static

Custom middleware — measures and logs request processing time.

◆ user()

static void user ( csilk_ctx_t *  c)
static

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