Static template generator for C
git clone git://soophie.de/ductape
blob: 11d9fb6d09f6c1024815742f8ed672fefde5cf59 (1447 bytes)[doc][blame][raw]
1
#ifndef LIB_CTL_H2
#define LIB_CTL_H4
#include <libhttp.h>5
#include <libstr.h>7
typedef struct ctl_ctx ctl_ctx_t;8
typedef void (*ctl_fn_t)(char **ctl_out, ctl_ctx_t ctx);10
struct ctl_ctx {11
http_req_t *request;12
char *title;13
ctl_fn_t fn;14
void *data;15
};17
void ctl_init(ctl_fn_t fn);18
http_resp_t ctl_serve_page(http_req_t *request, const char *title, ctl_fn_t fn, void *data);19
http_resp_t ctl_serve_component(http_req_t *request, ctl_fn_t fn, void *data);21
#ifdef LIB_CTL_IMPL23
static ctl_fn_t ctl_fn = NULL;25
void ctl_init(ctl_fn_t fn) {26
ctl_fn = fn;27
}29
http_resp_t ctl_serve_page(http_req_t *request, const char *title, ctl_fn_t fn, void *data) {30
char *html = NULL;31
ctl_ctx_t ctx = {32
.request = request,33
.title = title,34
.fn = fn,35
.data = data,36
};37
ctl_fn(&html, ctx);38
http_resp_t response = http_resp_create(HTTP_STATUS_OK);39
http_header_set(&response, "Content-Type", "text/html");40
http_resp_body_set(&response, html, strlen(html));41
free(html);42
return response;43
}45
http_resp_t ctl_serve_component(http_req_t *request, ctl_fn_t fn, void *data) {46
char *html = NULL;47
ctl_ctx_t ctx = {48
.request = request,49
.data = data,50
};51
fn(&html, ctx);52
http_resp_t response = http_resp_create(HTTP_STATUS_OK);53
http_header_set(&response, "Content-Type", "text/html");54
http_resp_body_set(&response, html, strlen(html));55
free(html);56
return response;57
}59
#endif /* LIB_CTL_IMPL */60
#endif /* LIB_CTL_H */