Clean up routes
This commit is contained in:
123
routes.c
123
routes.c
@ -5,9 +5,48 @@
|
||||
|
||||
#include "routes.h"
|
||||
#include "baked.h"
|
||||
#include "clonestr.h"
|
||||
|
||||
#define INTERNAL_SERVER_ERROR_MSG "Internal server error ;(. Try again later..."
|
||||
|
||||
void make_internal_server_error(Route_Result *result)
|
||||
{
|
||||
result->status_code = 500;
|
||||
list_append(&result->headers, "Content-Type: text/plain");
|
||||
sb_append_nstr(&result->body, INTERNAL_SERVER_ERROR_MSG);
|
||||
sb_finish(&result->body);
|
||||
}
|
||||
|
||||
void make_application_json(Route_Result *result, int code, cJSON *root)
|
||||
{
|
||||
char *root_text = cJSON_PrintUnformatted(root);
|
||||
defer { free(root_text); }
|
||||
|
||||
result->status_code = code;
|
||||
list_append(&result->headers, clonestr("Content-Type: application/json"));
|
||||
sb_append_nstr(&result->body, root_text);
|
||||
sb_finish(&result->body);
|
||||
}
|
||||
|
||||
void make_image_xicon(Route_Result *result, int code, char *icon)
|
||||
{
|
||||
result->status_code = code;
|
||||
list_append(&result->headers, clonestr("Content-Type: image/x-icon"));
|
||||
sb_append_nstr(&result->body, icon);
|
||||
sb_finish(&result->body);
|
||||
}
|
||||
|
||||
void make_text(Route_Result *result, char *subtype, int code, char *in)
|
||||
{
|
||||
char type[100];
|
||||
snprintf(type, sizeof(type), "Content-Type: text/%s", subtype);
|
||||
|
||||
result->status_code = code;
|
||||
list_append(&result->headers, clonestr(type));
|
||||
sb_append_nstr(&result->body, in);
|
||||
sb_finish(&result->body);
|
||||
}
|
||||
|
||||
bool gpp_run(char *path, NString_List *env, String_Builder *out)
|
||||
{
|
||||
Cmd cmd = {0};
|
||||
@ -50,13 +89,7 @@ void route_build_id(struct mg_http_message *msg, Route_Result *result)
|
||||
|
||||
cJSON_AddItemToObject(root, "build_id", cJSON_CreateString(sb.items));
|
||||
|
||||
char *root_text = cJSON_PrintUnformatted(root);
|
||||
defer { free(root_text); }
|
||||
|
||||
result->status_code = 200;
|
||||
list_append(&result->headers, "Content-Type: application/json");
|
||||
sb_append_nstr(&result->body, root_text);
|
||||
sb_finish(&result->body);
|
||||
make_application_json(result, 200, root);
|
||||
}
|
||||
|
||||
void route_page_not_found(struct mg_http_message *msg, Route_Result *result)
|
||||
@ -71,10 +104,7 @@ void route_page_not_found(struct mg_http_message *msg, Route_Result *result)
|
||||
|
||||
char path[PATH_MAX] = {0};
|
||||
if (!get_baked_resource_path("page-missing.t", path, sizeof(path))) {
|
||||
result->status_code = 500;
|
||||
list_append(&result->headers, "Content-Type: text/plain");
|
||||
sb_append_nstr(&result->body, INTERNAL_SERVER_ERROR_MSG);
|
||||
sb_finish(&result->body);
|
||||
make_internal_server_error(result);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -88,15 +118,9 @@ void route_page_not_found(struct mg_http_message *msg, Route_Result *result)
|
||||
sb_finish(&out);
|
||||
|
||||
if (!ok) {
|
||||
result->status_code = 500;
|
||||
list_append(&result->headers, "Content-Type: text/plain");
|
||||
sb_append_nstr(&result->body, INTERNAL_SERVER_ERROR_MSG);
|
||||
sb_finish(&result->body);
|
||||
make_internal_server_error(result);
|
||||
} else {
|
||||
result->status_code = 200;
|
||||
list_append(&result->headers, "Content-Type: text/html");
|
||||
sb_append_nstr(&result->body, out.items);
|
||||
sb_finish(&result->body);
|
||||
make_text(result, "html", 200, out.items);
|
||||
}
|
||||
}
|
||||
|
||||
@ -104,85 +128,57 @@ void route_simple_css(struct mg_http_message *msg, Route_Result *result)
|
||||
{
|
||||
char path[PATH_MAX] = {0};
|
||||
if (!get_baked_resource_path("simple.min.css", path, sizeof(path))) {
|
||||
result->status_code = 500;
|
||||
list_append(&result->headers, "Content-Type: text/plain");
|
||||
sb_append_nstr(&result->body, INTERNAL_SERVER_ERROR_MSG);
|
||||
sb_finish(&result->body);
|
||||
make_internal_server_error(result);
|
||||
return;
|
||||
}
|
||||
|
||||
String_Builder sb = {0};
|
||||
defer { sb_free(&sb); }
|
||||
if (!sb_read_file(&sb, path)) {
|
||||
result->status_code = 500;
|
||||
list_append(&result->headers, "Content-Type: text/plain");
|
||||
sb_append_nstr(&result->body, INTERNAL_SERVER_ERROR_MSG);
|
||||
sb_finish(&result->body);
|
||||
make_internal_server_error(result);
|
||||
return;
|
||||
}
|
||||
sb_finish(&sb);
|
||||
|
||||
result->status_code = 200;
|
||||
list_append(&result->headers, "Content-Type: text/css");
|
||||
sb_append_nstr(&result->body, sb.items);
|
||||
sb_finish(&result->body);
|
||||
make_text(result, "css", 200, sb.items);
|
||||
}
|
||||
|
||||
void route_favicon(struct mg_http_message *msg, Route_Result *result)
|
||||
{
|
||||
char path[PATH_MAX] = {0};
|
||||
if (!get_baked_resource_path("favicon.ico", path, sizeof(path))) {
|
||||
result->status_code = 500;
|
||||
list_append(&result->headers, "Content-Type: text/plain");
|
||||
sb_append_nstr(&result->body, INTERNAL_SERVER_ERROR_MSG);
|
||||
sb_finish(&result->body);
|
||||
make_internal_server_error(result);
|
||||
return;
|
||||
}
|
||||
|
||||
String_Builder sb = {0};
|
||||
defer { sb_free(&sb); }
|
||||
if (!sb_read_file(&sb, path)) {
|
||||
result->status_code = 500;
|
||||
list_append(&result->headers, "Content-Type: text/plain");
|
||||
sb_append_nstr(&result->body, INTERNAL_SERVER_ERROR_MSG);
|
||||
sb_finish(&result->body);
|
||||
make_internal_server_error(result);
|
||||
return;
|
||||
}
|
||||
sb_finish(&sb);
|
||||
|
||||
result->status_code = 200;
|
||||
list_append(&result->headers, "Content-Type: image/x-icon");
|
||||
sb_append_nstr(&result->body, sb.items);
|
||||
sb_finish(&result->body);
|
||||
make_image_xicon(result, 200, sb.items);
|
||||
}
|
||||
|
||||
void route_hotreload_js(struct mg_http_message *msg, Route_Result *result)
|
||||
{
|
||||
char path[PATH_MAX] = {0};
|
||||
if (!get_baked_resource_path("hotreload.js", path, sizeof(path))) {
|
||||
result->status_code = 500;
|
||||
list_append(&result->headers, "Content-Type: text/plain");
|
||||
sb_append_nstr(&result->body, INTERNAL_SERVER_ERROR_MSG);
|
||||
sb_finish(&result->body);
|
||||
make_internal_server_error(result);
|
||||
return;
|
||||
}
|
||||
|
||||
String_Builder sb = {0};
|
||||
defer { sb_free(&sb); }
|
||||
if (!sb_read_file(&sb, path)) {
|
||||
result->status_code = 500;
|
||||
list_append(&result->headers, "Content-Type: text/plain");
|
||||
sb_append_nstr(&result->body, INTERNAL_SERVER_ERROR_MSG);
|
||||
sb_finish(&result->body);
|
||||
make_internal_server_error(result);
|
||||
return;
|
||||
}
|
||||
sb_finish(&sb);
|
||||
|
||||
|
||||
result->status_code = 200;
|
||||
list_append(&result->headers, "Content-Type: text/javascript");
|
||||
sb_append_nstr(&result->body, sb.items);
|
||||
sb_finish(&result->body);
|
||||
make_text(result, "javascript", 200, sb.items);
|
||||
}
|
||||
|
||||
void route_home(struct mg_http_message *msg, Route_Result *result)
|
||||
@ -195,10 +191,7 @@ void route_home(struct mg_http_message *msg, Route_Result *result)
|
||||
|
||||
char path[PATH_MAX] = {0};
|
||||
if (!get_baked_resource_path("home.t", path, sizeof(path))) {
|
||||
result->status_code = 500;
|
||||
list_append(&result->headers, "Content-Type: text/plain");
|
||||
sb_append_nstr(&result->body, INTERNAL_SERVER_ERROR_MSG);
|
||||
sb_finish(&result->body);
|
||||
make_internal_server_error(result);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -212,14 +205,8 @@ void route_home(struct mg_http_message *msg, Route_Result *result)
|
||||
sb_finish(&out);
|
||||
|
||||
if (!ok) {
|
||||
result->status_code = 500;
|
||||
list_append(&result->headers, "Content-Type: text/plain");
|
||||
sb_append_nstr(&result->body, INTERNAL_SERVER_ERROR_MSG);
|
||||
sb_finish(&result->body);
|
||||
make_internal_server_error(result);
|
||||
} else {
|
||||
result->status_code = 200;
|
||||
list_append(&result->headers, "Content-Type: text/html");
|
||||
sb_append_nstr(&result->body, out.items);
|
||||
sb_finish(&result->body);
|
||||
make_text(result, "html", 200, out.items);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user