Ejemplo n.º 1
0
balde_response_t*
profile(balde_app_t *app, balde_request_t *request)
{
    const gchar *name = balde_request_get_view_arg(request, "name");
    gchar *str = g_strdup_printf("Hello, %s\n", name != NULL ? name : "World");
    balde_response_t *response = balde_make_response(str);
    g_free(str);
    return response;
}
Ejemplo n.º 2
0
void
test_request_get_view_arg(void)
{
    balde_app_t *app = balde_app_init();
    balde_request_t *request = balde_make_request(app);
    request->view_args = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
    g_hash_table_replace(request->view_args, g_strdup("foo"), g_strdup("bar"));
    g_assert_cmpstr(balde_request_get_view_arg(request, "foo"), == , "bar");
    g_assert(balde_request_get_view_arg(request, "xd") == NULL);
    balde_request_free(request);
    balde_app_free(app);
}
Ejemplo n.º 3
0
static balde_response_t*
main_view(balde_app_t *app, balde_request_t *request)
{
    bluster_gist_ctx_t *ctx = balde_app_get_user_data(app);
    if (ctx == NULL)
        return balde_abort(app, 404);

    const gchar *slug = balde_request_get_view_arg(request, "slug");

    if ((slug != NULL) ^ (ctx->files != NULL && ctx->files->next == NULL)) {
        bluster_gist_file_t *file = NULL;
        if (ctx->files != NULL && ctx->files->next == NULL) {
            file = ctx->files->data;
        }
        else {
            for (GSList *tmp = ctx->files; tmp != NULL; tmp = tmp->next) {
                bluster_gist_file_t *tmp_file = tmp->data;
                if (g_strcmp0(tmp_file->slug, slug) == 0) {
                    file = tmp_file;
                    break;
                }
            }
        }
        if (file == NULL)
            return balde_abort(app, 404);
        balde_response_t* response = create_response(ctx);
        balde_response_set_tmpl_var(response, "css",
            file->parsed_content->css == NULL ? "" : file->parsed_content->css);
        gchar *content;
        gchar *title;
        if (file->title != NULL) {
            title = file->title;
            content = g_strdup(file->parsed_content->content);
        }
        else {
            title = ctx->headline;
            content = g_strdup_printf("<h1>%s</h1>\n%s", ctx->headline,
                file->parsed_content->content);
        }
        balde_response_set_tmpl_var(response, "title", title);
        balde_response_set_tmpl_var(response, "content", content);
        balde_template_base(app, request, response);
        g_free(content);
        return response;
    }
    if (slug == NULL) {
        balde_response_t* response = create_response(ctx);
        balde_response_set_tmpl_var(response, "title", ctx->headline);
        GString *list = g_string_new("");
        for (GSList *tmp = ctx->files; tmp != NULL; tmp = tmp->next) {
            bluster_gist_file_t *tmp_file = tmp->data;
            balde_response_set_tmpl_var(response, "slug", tmp_file->slug);
            balde_response_set_tmpl_var(response, "link_title", tmp_file->title);
            g_string_append(list, balde_str_template_list(app, request, response));
        }
        balde_response_set_tmpl_var(response, "list", list->str);
        g_string_free(list, TRUE);
        balde_template_base(app, request, response);
        return response;
    }
    return balde_abort(app, 404);
}