Page Hotreloading
This commit is contained in:
60
routes.c
60
routes.c
@ -1,5 +1,7 @@
|
||||
#include "gebs/gebs.h"
|
||||
#include "mongoose/mongoose.h"
|
||||
#include "cJSON/cJSON.h"
|
||||
#include "md5-c/md5.h"
|
||||
|
||||
#include "routes.h"
|
||||
#include "baked.h"
|
||||
@ -29,6 +31,31 @@ 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)
|
||||
{
|
||||
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};
|
||||
@ -44,6 +71,12 @@ void route_page_not_found(struct mg_connection *conn, struct mg_http_message *ms
|
||||
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);
|
||||
@ -93,6 +126,25 @@ void route_favicon(struct mg_connection *conn, struct mg_http_message *msg)
|
||||
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};
|
||||
@ -106,7 +158,13 @@ void route_home(struct mg_connection *conn, struct mg_http_message *msg)
|
||||
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);
|
||||
|
||||
|
Reference in New Issue
Block a user