177 lines
4.9 KiB
C
177 lines
4.9 KiB
C
#include "gebs/gebs.h"
|
|
#include "mongoose/mongoose.h"
|
|
#include "cJSON/cJSON.h"
|
|
#include "md5-c/md5.h"
|
|
|
|
#include "routes.h"
|
|
#include "baked.h"
|
|
|
|
#define INTERNAL_SERVER_ERROR_MSG "Internal server error ;(. Try again later..."
|
|
|
|
bool gpp_run(char *path, NString_List *env, String_Builder *out)
|
|
{
|
|
Cmd cmd = {0};
|
|
defer { cmd_free(&cmd); }
|
|
|
|
char gpp1[PATH_MAX];
|
|
if (!get_baked_resource_path("gpp1", gpp1, sizeof(gpp1))) {
|
|
return false;
|
|
}
|
|
|
|
cmd_append(&cmd, gpp1);
|
|
cmd_append(&cmd, "-H");
|
|
cmd_append(&cmd, "-x");
|
|
cmd_append(&cmd, "--nostdinc");
|
|
cmd_append(&cmd, path);
|
|
|
|
for (size_t i = 0; i < env->count; i++) {
|
|
cmd_append(&cmd, env->items[i]);
|
|
}
|
|
|
|
return cmd_run_collect(&cmd, out) == 0;
|
|
}
|
|
|
|
void route_build_id(struct mg_connection *conn, struct mg_http_message *msg)
|
|
{
|
|
cJSON *root = cJSON_CreateObject();
|
|
defer { cJSON_Delete(root); }
|
|
|
|
char *time = __TIME__;
|
|
uchar md5_buf[16];
|
|
md5String(time, md5_buf);
|
|
String_Builder sb = {0};
|
|
defer { sb_free(&sb); }
|
|
for (size_t i = 0; i < 16; i++) {
|
|
char tmp[3];
|
|
snprintf(tmp, sizeof(tmp), "%02x", md5_buf[i]);
|
|
sb_append_nstr(&sb, tmp);
|
|
}
|
|
sb_finish(&sb);
|
|
|
|
cJSON_AddItemToObject(root, "build_id", cJSON_CreateString(sb.items));
|
|
|
|
char *root_text = cJSON_PrintUnformatted(root);
|
|
defer { free(root_text); }
|
|
|
|
mg_http_reply(conn, 200, "Content-Type: application/json\r\n", root_text);
|
|
}
|
|
|
|
void route_page_not_found(struct mg_connection *conn, struct mg_http_message *msg)
|
|
{
|
|
NString_List env = {0};
|
|
defer { list_free(&env); }
|
|
|
|
list_append(&env, fmt("-DURL=%.*s", msg->uri.len, msg->uri.buf));
|
|
|
|
String_Builder out = {0};
|
|
defer { sb_free(&out); }
|
|
|
|
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);
|
|
return;
|
|
}
|
|
|
|
#if DEBUG
|
|
list_append(&env, "-DHOTRELOAD=1");
|
|
#else
|
|
list_append(&env, "-DHOTRELOAD=0");
|
|
#endif
|
|
|
|
bool ok = gpp_run(path, &env, &out);
|
|
sb_finish(&out);
|
|
|
|
if (!ok) {
|
|
mg_http_reply(conn, 500, "Content-Type: text/plain\r\n", INTERNAL_SERVER_ERROR_MSG);
|
|
} else {
|
|
mg_http_reply(conn, 404, "Content-Type: text/html\r\n", out.items);
|
|
}
|
|
}
|
|
|
|
void route_simple_css(struct mg_connection *conn, struct mg_http_message *msg)
|
|
{
|
|
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);
|
|
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);
|
|
return;
|
|
}
|
|
sb_finish(&sb);
|
|
|
|
mg_http_reply(conn, 200, "Content-Type: text/css\r\n", "%s", sb.items);
|
|
}
|
|
|
|
void route_favicon(struct mg_connection *conn, struct mg_http_message *msg)
|
|
{
|
|
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);
|
|
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);
|
|
return;
|
|
}
|
|
sb_finish(&sb);
|
|
|
|
mg_http_reply(conn, 200, "Content-Type: image/x-icon\r\n", "%s", sb.items);
|
|
}
|
|
|
|
void route_hotreload_js(struct mg_connection *conn, struct mg_http_message *msg)
|
|
{
|
|
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);
|
|
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);
|
|
return;
|
|
}
|
|
sb_finish(&sb);
|
|
|
|
mg_http_reply(conn, 200, "Content-Type: text/javascript\r\n", "%s", sb.items);
|
|
}
|
|
|
|
void route_home(struct mg_connection *conn, struct mg_http_message *msg)
|
|
{
|
|
NString_List env = {0};
|
|
defer { list_free(&env); }
|
|
|
|
String_Builder out = {0};
|
|
defer { sb_free(&out); }
|
|
|
|
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);
|
|
return;
|
|
}
|
|
|
|
#if DEBUG
|
|
list_append(&env, "-DHOTRELOAD=1");
|
|
#else
|
|
list_append(&env, "-DHOTRELOAD=0");
|
|
#endif
|
|
|
|
bool ok = gpp_run(path, &env, &out);
|
|
sb_finish(&out);
|
|
|
|
if (!ok) {
|
|
mg_http_reply(conn, 500, "Content-Type: text/plain\r\n", INTERNAL_SERVER_ERROR_MSG);
|
|
} else {
|
|
mg_http_reply(conn, 200, "Content-Type: text/html\r\n", out.items);
|
|
}
|
|
}
|