Make this bih multithreaded
This commit is contained in:
93
routes.c
93
routes.c
@ -31,7 +31,7 @@ bool gpp_run(char *path, NString_List *env, String_Builder *out)
|
||||
return cmd_run_collect(&cmd, out) == 0;
|
||||
}
|
||||
|
||||
void route_build_id(struct mg_connection *conn, struct mg_http_message *msg)
|
||||
void route_build_id(struct mg_http_message *msg, Route_Result *result)
|
||||
{
|
||||
cJSON *root = cJSON_CreateObject();
|
||||
defer { cJSON_Delete(root); }
|
||||
@ -53,10 +53,13 @@ void route_build_id(struct mg_connection *conn, struct mg_http_message *msg)
|
||||
char *root_text = cJSON_PrintUnformatted(root);
|
||||
defer { free(root_text); }
|
||||
|
||||
mg_http_reply(conn, 200, "Content-Type: application/json\r\n", 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);
|
||||
}
|
||||
|
||||
void route_page_not_found(struct mg_connection *conn, struct mg_http_message *msg)
|
||||
void route_page_not_found(struct mg_http_message *msg, Route_Result *result)
|
||||
{
|
||||
NString_List env = {0};
|
||||
defer { list_free(&env); }
|
||||
@ -68,7 +71,10 @@ void route_page_not_found(struct mg_connection *conn, struct mg_http_message *ms
|
||||
|
||||
char path[PATH_MAX] = {0};
|
||||
if (!get_baked_resource_path("page-missing.t", path, sizeof(path))) {
|
||||
mg_http_reply(conn, 500, "Content-Type: text/plain\r\n", INTERNAL_SERVER_ERROR_MSG);
|
||||
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);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -82,70 +88,104 @@ void route_page_not_found(struct mg_connection *conn, struct mg_http_message *ms
|
||||
sb_finish(&out);
|
||||
|
||||
if (!ok) {
|
||||
mg_http_reply(conn, 500, "Content-Type: text/plain\r\n", INTERNAL_SERVER_ERROR_MSG);
|
||||
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);
|
||||
} else {
|
||||
mg_http_reply(conn, 404, "Content-Type: text/html\r\n", out.items);
|
||||
result->status_code = 200;
|
||||
list_append(&result->headers, "Content-Type: text/html");
|
||||
sb_append_nstr(&result->body, out.items);
|
||||
sb_finish(&result->body);
|
||||
}
|
||||
}
|
||||
|
||||
void route_simple_css(struct mg_connection *conn, struct mg_http_message *msg)
|
||||
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))) {
|
||||
mg_http_reply(conn, 500, "Content-Type: text/plain\r\n", INTERNAL_SERVER_ERROR_MSG);
|
||||
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);
|
||||
return;
|
||||
}
|
||||
|
||||
String_Builder sb = {0};
|
||||
defer { sb_free(&sb); }
|
||||
if (!sb_read_file(&sb, path)) {
|
||||
mg_http_reply(conn, 500, "Content-Type: text/plain\r\n", INTERNAL_SERVER_ERROR_MSG);
|
||||
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);
|
||||
return;
|
||||
}
|
||||
sb_finish(&sb);
|
||||
|
||||
mg_http_reply(conn, 200, "Content-Type: text/css\r\n", "%s", sb.items);
|
||||
result->status_code = 200;
|
||||
list_append(&result->headers, "Content-Type: text/css");
|
||||
sb_append_nstr(&result->body, sb.items);
|
||||
sb_finish(&result->body);
|
||||
}
|
||||
|
||||
void route_favicon(struct mg_connection *conn, struct mg_http_message *msg)
|
||||
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))) {
|
||||
mg_http_reply(conn, 500, "Content-Type: text/plain\r\n", INTERNAL_SERVER_ERROR_MSG);
|
||||
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);
|
||||
return;
|
||||
}
|
||||
|
||||
String_Builder sb = {0};
|
||||
defer { sb_free(&sb); }
|
||||
if (!sb_read_file(&sb, path)) {
|
||||
mg_http_reply(conn, 500, "Content-Type: text/plain\r\n", INTERNAL_SERVER_ERROR_MSG);
|
||||
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);
|
||||
return;
|
||||
}
|
||||
sb_finish(&sb);
|
||||
|
||||
mg_http_reply(conn, 200, "Content-Type: image/x-icon\r\n", "%s", sb.items);
|
||||
result->status_code = 200;
|
||||
list_append(&result->headers, "Content-Type: image/x-icon");
|
||||
sb_append_nstr(&result->body, sb.items);
|
||||
sb_finish(&result->body);
|
||||
}
|
||||
|
||||
void route_hotreload_js(struct mg_connection *conn, struct mg_http_message *msg)
|
||||
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))) {
|
||||
mg_http_reply(conn, 500, "Content-Type: text/plain\r\n", INTERNAL_SERVER_ERROR_MSG);
|
||||
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);
|
||||
return;
|
||||
}
|
||||
|
||||
String_Builder sb = {0};
|
||||
defer { sb_free(&sb); }
|
||||
if (!sb_read_file(&sb, path)) {
|
||||
mg_http_reply(conn, 500, "Content-Type: text/plain\r\n", INTERNAL_SERVER_ERROR_MSG);
|
||||
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);
|
||||
return;
|
||||
}
|
||||
sb_finish(&sb);
|
||||
|
||||
mg_http_reply(conn, 200, "Content-Type: text/javascript\r\n", "%s", sb.items);
|
||||
|
||||
result->status_code = 200;
|
||||
list_append(&result->headers, "Content-Type: text/javascript");
|
||||
sb_append_nstr(&result->body, sb.items);
|
||||
sb_finish(&result->body);
|
||||
}
|
||||
|
||||
void route_home(struct mg_connection *conn, struct mg_http_message *msg)
|
||||
void route_home(struct mg_http_message *msg, Route_Result *result)
|
||||
{
|
||||
NString_List env = {0};
|
||||
defer { list_free(&env); }
|
||||
@ -155,7 +195,10 @@ void route_home(struct mg_connection *conn, struct mg_http_message *msg)
|
||||
|
||||
char path[PATH_MAX] = {0};
|
||||
if (!get_baked_resource_path("home.t", path, sizeof(path))) {
|
||||
mg_http_reply(conn, 500, "Content-Type: text/plain\r\n", INTERNAL_SERVER_ERROR_MSG);
|
||||
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);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -169,8 +212,14 @@ void route_home(struct mg_connection *conn, struct mg_http_message *msg)
|
||||
sb_finish(&out);
|
||||
|
||||
if (!ok) {
|
||||
mg_http_reply(conn, 500, "Content-Type: text/plain\r\n", INTERNAL_SERVER_ERROR_MSG);
|
||||
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);
|
||||
} else {
|
||||
mg_http_reply(conn, 200, "Content-Type: text/html\r\n", out.items);
|
||||
result->status_code = 200;
|
||||
list_append(&result->headers, "Content-Type: text/html");
|
||||
sb_append_nstr(&result->body, out.items);
|
||||
sb_finish(&result->body);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user