diff options
| author | frosty <frosty@illegalfirearms.store> | 2025-12-28 03:26:05 -0500 |
|---|---|---|
| committer | frosty <frosty@illegalfirearms.store> | 2025-12-28 03:26:05 -0500 |
| commit | 4af132cf6adeeeeb5d6764c378bec2d05cad042f (patch) | |
| tree | e422cff2831424775ba5c20196064f94cbe1e5c3 /examples | |
Migrated from GitHub
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/hello-world/Makefile | 6 | ||||
| -rw-r--r-- | examples/hello-world/main.c | 20 | ||||
| -rw-r--r-- | examples/template-demo/Makefile | 6 | ||||
| -rw-r--r-- | examples/template-demo/main.c | 74 | ||||
| -rw-r--r-- | examples/template-demo/static/style.css | 67 | ||||
| -rw-r--r-- | examples/template-demo/templates/header.html | 8 | ||||
| -rw-r--r-- | examples/template-demo/templates/index.html | 48 |
7 files changed, 229 insertions, 0 deletions
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 <beaker.h> +#include <stdio.h> +#include <stdlib.h> + +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 <beaker.h> +#include <stdio.h> +#include <stdlib.h> +#include <time.h> + +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 <b>bold</b> and <i>italic</i> HTML."); + context_set(&ctx, "unsafe_html", "<script>alert(0);"); + + char *features[] = {"Fast and Lightweight", "Simple API", + "Basic Routing", "Templating Engine", + "Static File Serving", "Cookie Management"}; + int num_features = sizeof(features) / sizeof(features[0]); + context_set_string_array(&ctx, "features", features, num_features); + + char *user1[] = {"Alice", "30", "New York"}; + char *user2[] = {"Bob", "24", "Paris"}; + char *user3[] = {"Charlie", "35", "London"}; + char **users_2d[] = {user1, user2, user3}; + + int user_inner_counts[] = {3, 3, 3}; + int num_users = sizeof(users_2d) / sizeof(users_2d[0]); + context_set_array_of_arrays(&ctx, "users", users_2d, num_users, + user_inner_counts); + + char *rendered_html = render_template("index.html", &ctx); + if (rendered_html == NULL) { + fprintf(stderr, "[APP] Error: Failed to render template.\n"); + free_context(&ctx); + send_response( + "<h1>500 Internal Server Error</h1><p>Failed to render template.</p>"); + return -1; + } + + send_response(rendered_html); + + free(rendered_html); + free_context(&ctx); + + return 0; +} + +int main() { + + set_handler("/", templating_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/static/style.css b/examples/template-demo/static/style.css new file mode 100644 index 0000000..1faafe4 --- /dev/null +++ b/examples/template-demo/static/style.css @@ -0,0 +1,67 @@ +body { + font-family: sans-serif; + margin: 20px; + background-color: #f4f7f6; + color: #333; + line-height: 1.6; +} + +.container { + max-width: 800px; + margin: 0 auto; + background-color: #ffffff; + padding: 30px; + border-radius: 12px; + box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1); +} + +h1, +h2, +h3 { + color: #2c3e50; + border-bottom: 2px solid #e0e0e0; + padding-bottom: 10px; + margin-top: 30px; +} + +ul { + list-style-type: none; + padding: 0; +} + +li { + background-color: #ecf0f1; + margin-bottom: 8px; + padding: 10px 15px; + border-radius: 8px; + display: flex; + align-items: center; +} + +li:nth-child(odd) { + background-color: #e0e0e0; +} + +.highlight { + font-weight: bold; + color: #2980b9; +} + +.code-block { + background-color: #2c3e50; + color: #ecf0f1; + padding: 5px; + border-radius: 8px; + font-family: monospace; + overflow-x: auto; + margin-top: 15px; +} + +.footer { + text-align: center; + margin-top: 40px; + padding-top: 20px; + border-top: 1px solid #e0e0e0; + color: #7f8c8d; + font-size: 0.9em; +}
\ No newline at end of file diff --git a/examples/template-demo/templates/header.html b/examples/template-demo/templates/header.html new file mode 100644 index 0000000..746cee3 --- /dev/null +++ b/examples/template-demo/templates/header.html @@ -0,0 +1,8 @@ +<header style="text-align: center; padding-bottom: 20px; margin-bottom: 20px; border-bottom: 1px dashed #ccc;"> + <h2 style="color: #34495e;">Beaker Templating Demonstration</h2> + <nav> + <a href="/" style="margin: 0 15px; text-decoration: none; color: #2980b9; font-weight: bold;">Home</a> + <a href="#" style="margin: 0 15px; text-decoration: none; color: #2980b9; font-weight: bold;">Nothing</a> + + </nav> +</header> diff --git a/examples/template-demo/templates/index.html b/examples/template-demo/templates/index.html new file mode 100644 index 0000000..837688f --- /dev/null +++ b/examples/template-demo/templates/index.html @@ -0,0 +1,48 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>{{title}}</title> + <link rel="stylesheet" href="/static/style.css"> +</head> +<body> + <div class="container"> + {{include "header.html"}} + + <h1>{{page_heading}}</h1> + + <p>Welcome, <span class="highlight">{{username}}</span>! This page demonstrates the templating capabilities of the Beaker web framework.</p> + + <h2>Simple Variable Substitution</h2> + <p>Your favourite colour is: <span class="highlight">{{favourite_colour}}</span></p> + <p>The current year is: <span class="highlight">{{current_year}}</span></p> + <p>This is a safe string: <span class="code-block">{{safe_html|safe}}</span></p> + <p>This is an unsafe string (will be escaped): <span class="code-block">{{unsafe_html}}</span></p> + + <h2>List of Features (String Array)</h2> + <p>Here are some features:</p> + <ul> + {{for feature in features}} + <li>{{feature}}</li> + {{endfor}} + </ul> + + <h2>User Data (Array of Arrays)</h2> + <p>Here's some user information:</p> + <ul> + {{for user_row in users}} + <li> + Name: <span class="highlight">{{user_row[0]}}</span>, + Age: <span class="highlight">{{user_row[1]}}</span>, + City: <span class="highlight">{{user_row[2]}}</span> + </li> + {{endfor}} + </ul> + + <div class="footer"> + <p>Rendered at: {{timestamp}}</p> + </div> + </div> +</body> +</html> |
