From 4af132cf6adeeeeb5d6764c378bec2d05cad042f Mon Sep 17 00:00:00 2001 From: frosty Date: Sun, 28 Dec 2025 03:26:05 -0500 Subject: Migrated from GitHub --- examples/hello-world/Makefile | 6 +++ examples/hello-world/main.c | 20 ++++++++ examples/template-demo/Makefile | 6 +++ examples/template-demo/main.c | 74 ++++++++++++++++++++++++++++ examples/template-demo/static/style.css | 67 +++++++++++++++++++++++++ examples/template-demo/templates/header.html | 8 +++ examples/template-demo/templates/index.html | 48 ++++++++++++++++++ 7 files changed, 229 insertions(+) create mode 100644 examples/hello-world/Makefile create mode 100644 examples/hello-world/main.c create mode 100644 examples/template-demo/Makefile create mode 100644 examples/template-demo/main.c create mode 100644 examples/template-demo/static/style.css create mode 100644 examples/template-demo/templates/header.html create mode 100644 examples/template-demo/templates/index.html (limited to 'examples') diff --git a/examples/hello-world/Makefile b/examples/hello-world/Makefile new file mode 100644 index 0000000..56813e3 --- /dev/null +++ b/examples/hello-world/Makefile @@ -0,0 +1,6 @@ +CC = gcc + +hello-world: + $(CC) -o hello-world main.c -lbeaker +clean: + rm hello-world \ No newline at end of file diff --git a/examples/hello-world/main.c b/examples/hello-world/main.c new file mode 100644 index 0000000..bf3c8b0 --- /dev/null +++ b/examples/hello-world/main.c @@ -0,0 +1,20 @@ +#include +#include +#include + +int hello_world_handler(UrlParams *params) { + send_response("Hello, World!"); + return 0; +} + +int main() { + set_handler("/", hello_world_handler); + int result = beaker_run("127.0.0.1", 8080); + + if (result != 0) { + fprintf(stderr, "[APP] Error: Beaker server failed to start.\n"); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} diff --git a/examples/template-demo/Makefile b/examples/template-demo/Makefile new file mode 100644 index 0000000..ff0acd4 --- /dev/null +++ b/examples/template-demo/Makefile @@ -0,0 +1,6 @@ +CC = gcc + +template-demo: + $(CC) -o template-demo main.c -lbeaker +clean: + rm template-demo \ No newline at end of file diff --git a/examples/template-demo/main.c b/examples/template-demo/main.c new file mode 100644 index 0000000..2adbb52 --- /dev/null +++ b/examples/template-demo/main.c @@ -0,0 +1,74 @@ +#include +#include +#include +#include + +int templating_handler(UrlParams *params) { + + TemplateContext ctx = new_context(); + + context_set(&ctx, "title", "Beaker Example"); + context_set(&ctx, "page_heading", "Dynamic Content with Beaker Templates"); + context_set(&ctx, "username", "John Doe"); + context_set(&ctx, "favourite_colour", "blue"); + + time_t t = time(NULL); + struct tm *tm = localtime(&t); + char current_year_str[5]; + snprintf(current_year_str, sizeof(current_year_str), "%d", + tm->tm_year + 1900); + context_set(&ctx, "current_year", current_year_str); + + char timestamp_str[64]; + strftime(timestamp_str, sizeof(timestamp_str), "%Y-%m-%d %H:%M:%S", tm); + context_set(&ctx, "timestamp", timestamp_str); + + context_set(&ctx, "safe_html", "This is bold and italic HTML."); + context_set(&ctx, "unsafe_html", "