so://phie

ductape

Static template generator for C
git clone git://soophie.de/ductape

blob: 4f8c619ee9dacefedee48000885148816956a2b7 (1401 bytes)[doc][blame][raw]

1#ifndef LIB_CTL_H

2#define LIB_CTL_H

3

4#include <libhttp.h>

5#include <libstr.h>

6

7typedef struct ctl_ctx ctl_ctx_t;

8typedef void (*ctl_fn_t)(char **ctl_out, ctl_ctx_t ctx);

9

10struct ctl_ctx {

11 http_request_t *request;

12 char *title;

13 ctl_fn_t fn;

14};

15

16void ctl_init(ctl_fn_t fn);

17http_response_t ctl_serve_page(http_request_t *request, const char *title, ctl_fn_t fn);

18http_response_t ctl_serve_component(http_request_t *request, ctl_fn_t fn);

19

20#ifdef LIB_CTL_IMPL

21

22static ctl_fn_t ctl_fn = NULL;

23

24void ctl_init(ctl_fn_t fn) {

25 ctl_fn = fn;

26}

27

28http_response_t ctl_serve_page(http_request_t *request, const char *title, ctl_fn_t fn) {

29 char *html = NULL;

30 ctl_ctx_t ctx = {

31 .request = request,

32 .title = title,

33 .fn = fn,

34 };

35 ctl_fn(&html, ctx);

36 http_response_t response = http_response_create(HTTP_STATUS_OK);

37 http_header_set(&response, "Content-Type", "text/html");

38 http_response_body(&response, html, strlen(html));

39 free(html);

40 return response;

41}

42

43http_response_t ctl_serve_component(http_request_t *request, ctl_fn_t fn) {

44 char *html = NULL;

45 ctl_ctx_t ctx = {

46 .request = request,

47 };

48 fn(&html, ctx);

49 http_response_t response = http_response_create(HTTP_STATUS_OK);

50 http_header_set(&response, "Content-Type", "text/html");

51 http_response_body(&response, html, strlen(html));

52 free(html);

53 return response;

54}

55

56#endif /* LIB_CTL_IMPL */

57#endif /* LIB_CTL_H */