this repo has no description
1#include <string.h>
2#include <stddef.h>
3#include <stdlib.h>
4#define WBY_IMPLEMENTATION
5#include "web.h"
6
7// $ CFLAGS="-I examples/11_platforms/web/" ./compiler.py --compile --platform examples/11_platforms/web/web.c examples/11_platforms/web/handler.scrap
8// or
9// $ ./compiler.py --platform examples/11_platforms/web/web.c examples/11_platforms/web/handler.scrap
10// $ cc -I examples/11_platforms/web/ output.c
11
12static int
13dispatch(struct wby_con *connection, void *userdata)
14{
15 HANDLES();
16 GC_HANDLE(struct object*, handler, *(struct object**)userdata);
17 GC_HANDLE(struct object*, url, mkstring(heap, connection->request.uri, strlen(connection->request.uri)));
18 GC_HANDLE(struct object*, response, closure_call(handler, url));
19 assert(is_record(response));
20 GC_HANDLE(struct object*, code, record_get(response, Record_code));
21 assert(is_num(code));
22 GC_HANDLE(struct object*, body, record_get(response, Record_body));
23 assert(is_string(body));
24
25 wby_response_begin(connection, num_value(code), string_length(body), NULL, 0);
26 // TODO(max): Copy into buffer or strdup
27 wby_write(connection, as_heap_string(body)->data, string_length(body));
28 wby_response_end(connection);
29 fprintf(stderr, "%ld %s\n", num_value(code), connection->request.uri);
30 return num_value(code) == 200;
31}
32
33int main(int argc, const char * argv[])
34{
35 /* boot scrapscript */
36#ifdef STATIC_HEAP
37 char memory[MEMORY_SIZE] = {0};
38 struct space space = make_space(memory, MEMORY_SIZE);
39#else
40 struct space space = make_space(MEMORY_SIZE);
41#endif
42 init_heap(heap, space);
43 HANDLES();
44 GC_HANDLE(struct object*, handler, scrap_main());
45 assert(is_closure(handler));
46
47 /* setup config */
48 struct wby_config config;
49 memset(&config, 0, sizeof(config));
50 config.address = "0.0.0.0";
51 config.port = 8000;
52 config.connection_max = 8;
53 config.request_buffer_size = 2048;
54 config.io_buffer_size = 8192;
55 config.dispatch = dispatch;
56 config.userdata = &handler;
57
58 /* compute and allocate needed memory and start server */
59 struct wby_server server;
60 size_t needed_memory;
61 wby_init(&server, &config, &needed_memory);
62 void *memory = calloc(needed_memory, 1);
63 printf("serving at http://%s:%d\n", config.address, config.port);
64 wby_start(&server, memory);
65 while (1) {
66 wby_update(&server);
67 }
68 wby_stop(&server);
69 free(memory);
70 destroy_space(space);
71}
72