static balde_response_t* create_response(bluster_gist_ctx_t *ctx) { balde_response_t* response = balde_make_response(""); balde_response_set_header(response, "x-powered-by", PACKAGE_STRING); balde_response_set_tmpl_var(response, "bluster_url", PACKAGE_URL); gchar *gist_datetime = g_date_time_format(ctx->datetime, "%Y-%m-%d %H:%M:%S GMT"); balde_response_set_tmpl_var(response, "gist_datetime", gist_datetime); g_free(gist_datetime); return response; }
balde_response_t* hello(balde_app_t *app, balde_request_t *request) { balde_response_t *response = balde_make_response(""); const gchar *name = balde_request_get_arg(request, "name"); balde_response_set_tmpl_var(response, "name", name != NULL ? name : "World"); balde_template_hello(app, request, response); return response; }
void test_response_set_tmpl_var(void) { balde_response_t *res = balde_make_response("lol"); balde_response_set_tmpl_var(res, "bola", "guda"); g_assert(g_hash_table_size(res->template_ctx) == 1); g_assert_cmpstr(g_hash_table_lookup(res->template_ctx, "bola"), ==, "guda"); balde_response_free(res); }
balde_response_t* hello(balde_app_t *app, balde_request_t *request) { balde_response_t *response = balde_make_response(""); 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; }
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); }