54 lines
1.3 KiB
C
54 lines
1.3 KiB
C
#include <stdio.h>
|
|
|
|
#include "http.h"
|
|
#include "http_loop.h"
|
|
#include "log.h"
|
|
|
|
const char index_page[] =
|
|
"<!DOCTYPE html>\n"
|
|
"<html>\n"
|
|
" <body>\n"
|
|
" <h1>r8r.be</h1>\n"
|
|
" <p>This is the URL shortener and pastebin accompanying my site, <a "
|
|
"href=\"https://rustybever.be\">The Rusty Bever</a>.</p>\n"
|
|
" </body>\n"
|
|
"</html>\n";
|
|
|
|
bool lander_get_index(event_loop_conn *conn) {
|
|
http_loop_ctx *ctx = conn->ctx;
|
|
|
|
http_loop_res_set_body(ctx, index_page, sizeof(index_page) - 1, false);
|
|
|
|
conn->state = event_loop_conn_state_res;
|
|
|
|
return true;
|
|
}
|
|
|
|
http_route routes[] = {{.type = http_route_literal,
|
|
.method = http_get,
|
|
.path = "/",
|
|
.steps = {lander_get_index, NULL}}};
|
|
|
|
int main() {
|
|
setvbuf(stdout, NULL, _IONBF, 0);
|
|
|
|
info("Initializing trie");
|
|
|
|
Trie *trie = trie_init();
|
|
int count = trie_populate(trie, "lander.data");
|
|
|
|
if (count < 0) {
|
|
critical(1, "An error occured while populating the trie.");
|
|
}
|
|
|
|
info("Trie initialized and populated with %i entries", count);
|
|
|
|
http_loop_gctx *gctx = http_loop_gctx_init();
|
|
gctx->trie = trie;
|
|
gctx->routes = routes;
|
|
gctx->route_count = sizeof(routes) / sizeof(routes[0]);
|
|
event_loop *el = http_loop_init(gctx);
|
|
|
|
event_loop_run(el, 8000);
|
|
}
|