balde  master
A microframework for C based on GLib.
hello-with-form.c

An example with HTTP forms. It depends on more files. Take a look at it on the balde source code.

/*
* balde: A microframework for C based on GLib.
* Copyright (C) 2013-2017 Rafael G. Martins <rafael@rafaelmartins.eng.br>
*
* This program can be distributed under the terms of the LGPL-2 License.
* See the file COPYING.
*/
#include <balde.h>
#include "templates/hello.h"
#include "templates/form.h"
hello(balde_app_t *app, balde_request_t *request)
{
if (request->method == BALDE_HTTP_POST) {
const gchar *name = balde_request_get_form(request, "name");
balde_response_set_tmpl_var(response, "name",
name != NULL && name[0] != '\0' ? name : "World");
balde_template_hello(app, request, response);
}
else
balde_template_form(app, request, response);
return response;
}
int
main(int argc, char **argv)
{
balde_app_add_url_rule(app, "hello", "/", BALDE_HTTP_GET | BALDE_HTTP_POST,
hello);
balde_app_run(app, argc, argv);
return 0;
}
balde_app_add_url_rule
void balde_app_add_url_rule(balde_app_t *app, const gchar *endpoint, const gchar *rule, const balde_http_method_t method, balde_view_func_t view_func)
Adds a view to the balde application.
balde.h
balde public API.
balde_request_t
balde HTTP request context
Definition: balde.h:212
balde_request_get_form
const gchar * balde_request_get_form(balde_request_t *request, const gchar *name)
Gets a form input value.
balde_response_t
balde HTTP response context
Definition: balde.h:268
balde_response_set_tmpl_var
void balde_response_set_tmpl_var(balde_response_t *response, const gchar *name, const gchar *value)
Sets a template variable.
balde_app_free
void balde_app_free(balde_app_t *app)
Free application context memory.
balde_app_run
void balde_app_run(balde_app_t *app, gint argc, gchar **argv)
Application main loop.
balde_request_t::method
balde_http_method_t method
Request HTTP method.
Definition: balde.h:243
balde_app_init
balde_app_t * balde_app_init(void)
Initializes the application context.
balde_make_response
balde_response_t * balde_make_response(const gchar *content)
Initialize a response context.
balde_app_t
balde application context
Definition: balde.h:122