aboutsummaryrefslogtreecommitdiff
path: root/examples/hello/main.c
blob: f44ff0c98d6d353bf962792cb93eb0e8222cd1a3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <dynstr.h>
#include <slweb/handler.h>
#include <slweb/html.h>
#include <slweb/http.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

static int hello(const struct http_payload *const pl,
    struct http_response *const r, void *const user)
{
    int ret = -1;
    struct html_node *html = html_node_alloc("html"), *body, *p;
    struct dynstr d;

    dynstr_init(&d);

    if (!html)
    {
        fprintf(stderr, "%s: html_node_alloc failed\n", __func__);
        goto end;
    }
    else if (!(body = html_node_add_child(html, "body")))
    {
        fprintf(stderr, "%s: html_node_add_child body failed\n", __func__);
        goto end;
    }
    else if (!(p = html_node_add_child(html, "p")))
    {
        fprintf(stderr, "%s: html_node_add_child p failed\n", __func__);
        goto end;
    }
    else if (html_node_set_value(p, "Hello from slweb!"))
    {
        fprintf(stderr, "%s: html_node_set_value p failed\n", __func__);
        goto end;
    }
    else if (html_serialize(html, &d))
    {
        fprintf(stderr, "%s: html_serialize failed\n", __func__);
        goto end;
    }

    *r = (const struct http_response)
    {
        .status = HTTP_STATUS_OK,
        .buf.rw = d.str,
        .n = d.len,
        .free = free
    };

    if (http_response_add_header(r, "Content-Type", "text/html"))
    {
        fprintf(stderr, "%s: http_response_add_header failed\n", __func__);
        goto end;
    }

    ret = 0;

end:
    html_node_free(html);

    if (ret)
        dynstr_free(&d);

    return ret;
}

int main(int argc, char *argv[])
{
    int ret = EXIT_FAILURE;
    const short port = 8080;
    const struct handler_cfg cfg = {0};
    struct handler *const h = handler_alloc(&cfg);
    static const char *const urls[] = {"/", "/index.html"};

    if (!h)
    {
        fprintf(stderr, "%s: handler_alloc failed\n", __func__);
        goto end;
    }

    for (size_t i = 0; i < sizeof urls / sizeof *urls; i++)
        if (handler_add(h, urls[i], HTTP_OP_GET, hello, NULL))
        {
            fprintf(stderr, "%s: handler_add failed\n", __func__);
            goto end;
        }

    if (handler_listen(h, port))
    {
        fprintf(stderr, "%s: handler_listen failed\n", __func__);
        goto end;
    }

    ret = EXIT_SUCCESS;

end:
    handler_free(h);
    return ret;
}