Compare commits

...

3 Commits

Author SHA1 Message Date
7a9d5aa371 Self-host marked.js 2025-06-26 00:11:14 +02:00
d9071a4947 Rename etc dump to bakedres dump 2025-06-26 00:07:12 +02:00
632d731118 Self-host highlight.js 2025-06-25 23:58:47 +02:00
12 changed files with 1383 additions and 65 deletions

View File

@ -20,6 +20,9 @@ INCBIN(favicon_ico, "./etc/favicon.ico");
INCBIN(hotreload_js, "./etc/hotreload.js");
#endif
INCBIN(theme_js, "./etc/theme.js");
INCBIN(highlight_js, "./etc/highlight.js");
INCBIN(hljs_rainbow_css, "./etc/hljs-rainbow.css");
INCBIN(marked_js, "./etc/marked.js");
INCBIN(me_jpg, "./etc/me.jpg");
INCBIN(tmoa_engine_jpg, "./etc/tmoa-engine.jpg");
INCBIN(tmoa_garbage_jpg, "./etc/tmoa-garbage.jpg");
@ -66,6 +69,9 @@ void init_baked_resources(void)
add_baked_resource("hotreload.js", hotreload_js_data, hotreload_js_size);
#endif
add_baked_resource("theme.js", theme_js_data, theme_js_size);
add_baked_resource("highlight.js", highlight_js_data, highlight_js_size);
add_baked_resource("hljs-rainbow.css", hljs_rainbow_css_data, hljs_rainbow_css_size);
add_baked_resource("marked.js", marked_js_data, marked_js_size);
add_baked_resource("me.jpg", me_jpg_data, me_jpg_size);
add_baked_resource("tmoa-engine.jpg", tmoa_engine_jpg_data, tmoa_engine_jpg_size);
add_baked_resource("tmoa-garbage.jpg", tmoa_garbage_jpg_data, tmoa_garbage_jpg_size);

View File

@ -16,6 +16,9 @@ INCBIN_EXTERN(favicon_ico);
INCBIN_EXTERN(hotreload_js);
#endif
INCBIN_EXTERN(theme_js);
INCBIN_EXTERN(highlight_js);
INCBIN_EXTERN(hljs_rainbow_css);
INCBIN_EXTERN(marked_js);
INCBIN_EXTERN(me_jpg);
INCBIN_EXTERN(tmoa_engine_jpg);
INCBIN_EXTERN(tmoa_garbage_jpg);

View File

@ -5,7 +5,7 @@ how the code is architectured and some cool tricks that are used throughout the
## Our "engine"
![the engine](/etc/tmoa-engine.jpg)
![the engine](/bakedres/tmoa-engine.jpg)
This image is a joke, obviously.
@ -133,7 +133,7 @@ void *route_thread_function(void *param)
// Unparsable HTTP request
}
if (mg_match(http_msg.uri, mg_str("/etc/*"), nil)) {
if (mg_match(http_msg.uri, mg_str("/bakedres/*"), nil)) {
// Request for static resource
}
@ -147,7 +147,7 @@ this functionality comes with mongoose built-in. God bless you mongoose!
## Dynamic pages and static assets.
![the assets](/etc/tmoa-garbage.jpg)
![the assets](/bakedres/tmoa-garbage.jpg)
Let's stop here now and talk about something different entirely (dont' worry, we'll come back later).
I'd like to show you how assets/pages are implemented in aboba, so we get the whole picture.

View File

@ -39,6 +39,9 @@ int main(int argc, char ** argv)
"./etc/hotreload.js",
"./etc/theme.js",
"./etc/simple.css",
"./etc/highlight.js",
"./etc/hljs-rainbow.css",
"./etc/marked.js",
"./etc/favicon.ico",
"./etc/me.jpg",
"./etc/tmoa-engine.jpg",

1244
etc/highlight.js Normal file

File diff suppressed because one or more lines are too long

1
etc/hljs-rainbow.css Normal file
View File

@ -0,0 +1 @@
pre code.hljs{display:block;overflow-x:auto;padding:1em}code.hljs{padding:3px 5px}.hljs{background:#474949;color:#d1d9e1}.hljs-comment,.hljs-quote{color:#969896;font-style:italic}.hljs-addition,.hljs-keyword,.hljs-literal,.hljs-selector-tag,.hljs-type{color:#c9c}.hljs-number,.hljs-selector-attr,.hljs-selector-pseudo{color:#f99157}.hljs-doctag,.hljs-regexp,.hljs-string{color:#8abeb7}.hljs-built_in,.hljs-name,.hljs-section,.hljs-title{color:#b5bd68}.hljs-class .hljs-title,.hljs-selector-id,.hljs-template-variable,.hljs-title.class_,.hljs-variable{color:#fc6}.hljs-name,.hljs-section,.hljs-strong{font-weight:700}.hljs-bullet,.hljs-link,.hljs-meta,.hljs-subst,.hljs-symbol{color:#f99157}.hljs-deletion{color:#dc322f}.hljs-formula{background:#eee8d5}.hljs-attr,.hljs-attribute{color:#81a2be}.hljs-emphasis{font-style:italic}

69
etc/marked.js Normal file

File diff suppressed because one or more lines are too long

84
main.c
View File

@ -18,7 +18,7 @@
#include "locked.h"
static locked(Route *) route_hashtable = locked_init(nil);
static locked(char *) etc_dump_path = locked_init(nil);
static locked(char *) baked_dump_path = locked_init(nil);
void run_in_thread(void *(*f)(void *), void *p)
{
@ -49,7 +49,7 @@ void *route_thread_function(void *param)
return nil;
}
if (mg_match(http_msg.uri, mg_str("/etc/*"), nil)) {
if (mg_match(http_msg.uri, mg_str("/bakedres/*"), nil)) {
Route_Result result = {0};
result.type = ROUTE_RESULT_STATIC;
@ -60,9 +60,9 @@ void *route_thread_function(void *param)
char *file = basename(p);
char *full_path = malloc(PATH_MAX);
lockx(&etc_dump_path);
snprintf(full_path, PATH_MAX, "%s/%s", etc_dump_path.value, file);
unlockx(&etc_dump_path);
lockx(&baked_dump_path);
snprintf(full_path, PATH_MAX, "%s/%s", baked_dump_path.value, file);
unlockx(&baked_dump_path);
sb_append_nstr(&result.body, full_path);
sb_finish(&result.body);
@ -165,23 +165,23 @@ void free_route_hashtable(void)
unlockx(&route_hashtable);
}
char *init_etc_dump(void)
char *init_baked_dump(void)
{
char template[] = "/tmp/aboba-etc.XXXXXX";
char *etc_dump1 = mkdtemp(template);
char *etc_dump = malloc(strlen(etc_dump1)+1);
strcpy(etc_dump, etc_dump1);
char template[] = "/tmp/aboba-bakedres.XXXXXX";
char *baked_dump1 = mkdtemp(template);
char *baked_dump = malloc(strlen(baked_dump1)+1);
strcpy(baked_dump, baked_dump1);
if (etc_dump == nil) {
LOGE("Could not create etc dump\n");
if (baked_dump == nil) {
LOGE("Could not create bakedres dump\n");
return nil;
}
LOGI("etc dump dir is %s\n", etc_dump);
LOGI("bakedres dump dir is %s\n", baked_dump);
return etc_dump;
return baked_dump;
}
int etc_dump_rm_cb(const char *path,
int baked_dump_rm_cb(const char *path,
discard const struct stat *st,
discard int type,
discard struct FTW *ftw)
@ -189,14 +189,14 @@ int etc_dump_rm_cb(const char *path,
return remove(path);
}
bool free_etc_dump(char *etc_dump)
bool free_baked_dump(char *baked_dump)
{
LOGI("Removing etc dump %s\n", etc_dump);
bool ok = nftw(etc_dump, etc_dump_rm_cb, FOPEN_MAX, FTW_DEPTH | FTW_MOUNT | FTW_PHYS) != -1;
LOGI("Removing bakedres dump %s\n", baked_dump);
bool ok = nftw(baked_dump, baked_dump_rm_cb, FOPEN_MAX, FTW_DEPTH | FTW_MOUNT | FTW_PHYS) != -1;
if (!ok) {
LOGE("Could not remove %s\n", etc_dump);
LOGE("Could not remove %s\n", baked_dump);
}
free(etc_dump);
free(baked_dump);
return ok;
}
@ -234,28 +234,20 @@ int cp(const char* source, const char* destination)
return result;
}
void populate_etc_dump(char *etc_dump)
void copy_baked_resources_to_baked_dump(Baked_Resource *resource, void *udata)
{
static char *files[] = {
"favicon.ico",
#if MY_DEBUG
"hotreload.js",
#endif
"theme.js",
"simple.css",
"me.jpg",
"tmoa-engine.jpg",
"tmoa-garbage.jpg",
};
char *baked_dump = (char *)udata;
char path[PATH_MAX];
get_baked_resource_path(resource->key, path, sizeof(path));
char dest[PATH_MAX];
snprintf(dest, sizeof(dest), "%s/%s", baked_dump, resource->key);
cp(path, dest);
}
void populate_baked_dump(char *baked_dump)
{
lock_baked_resources();
for (size_t i = 0; i < sizeof(files)/sizeof(files[0]); i++) {
char path[PATH_MAX];
get_baked_resource_path(files[i], path, sizeof(path));
char dest[PATH_MAX];
snprintf(dest, sizeof(dest), "%s/%s", etc_dump, files[i]);
cp(path, dest);
}
baked_resource_each(&copy_baked_resources_to_baked_dump, baked_dump);
unlock_baked_resources();
}
@ -269,12 +261,12 @@ int main(int argc, char ** argv)
start_timer();
init_baked_resources();
lockx(&etc_dump_path);
if ((etc_dump_path.value = init_etc_dump()) == nil) {
lockx(&baked_dump_path);
if ((baked_dump_path.value = init_baked_dump()) == nil) {
return 1;
}
populate_etc_dump(etc_dump_path.value);
unlockx(&etc_dump_path);
populate_baked_dump(baked_dump_path.value);
unlockx(&baked_dump_path);
mg_log_set(MG_LL_DEBUG);
struct mg_mgr mgr;
@ -292,9 +284,9 @@ int main(int argc, char ** argv)
mg_mgr_free(&mgr);
lockx(&etc_dump_path);
free_etc_dump(etc_dump_path.value);
unlockx(&etc_dump_path);
lockx(&baked_dump_path);
free_baked_dump(baked_dump_path.value);
unlockx(&baked_dump_path);
free_baked_resources();
return 0;

View File

@ -3,7 +3,7 @@
<head>
<title>Blog</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="/etc/simple.css" />
<link rel="stylesheet" href="/bakedres/simple.css" />
<#META_TAGS>
</head>
<body>
@ -24,8 +24,8 @@
</footer>
<#ifdef HOTRELOAD>
<script src="/etc/hotreload.js"></script>
<script src="/bakedres/hotreload.js"></script>
<#endif>
<script src="/etc/theme.js"></script>
<script src="/bakedres/theme.js"></script>
</body>
</html>

View File

@ -3,7 +3,7 @@
<head>
<title>Kamil's personal website</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="/etc/simple.css" />
<link rel="stylesheet" href="/bakedres/simple.css" />
<#META_TAGS>
</head>
<body>
@ -54,7 +54,7 @@
</ul>
</section>
<section>
<img src="/etc/me.jpg" alt="literally me" />
<img src="/bakedres/me.jpg" alt="literally me" />
</section>
<footer>
<div style="float: left;">
@ -65,8 +65,8 @@
</footer>
<#ifdef HOTRELOAD>
<script src="/etc/hotreload.js"></script>
<script src="/bakedres/hotreload.js"></script>
<#endif>
<script src="/etc/theme.js"></script>
<script src="/bakedres/theme.js"></script>
</body>
</html>

View File

@ -2,7 +2,7 @@
<html>
<head>
<title>404 - Page not found</title>
<link rel="stylesheet" href="/etc/simple.css" />
<link rel="stylesheet" href="/bakedres/simple.css" />
<#META_TAGS>
</head>
<body>
@ -19,8 +19,8 @@
</footer>
<#ifdef HOTRELOAD>
<script src="/etc/hotreload.js"></script>
<script src="/bakedres/hotreload.js"></script>
<#endif>
<script src="/etc/theme.js"></script>
<script src="/bakedres/theme.js"></script>
</body>
</html>

View File

@ -3,10 +3,10 @@
<head>
<meta charset="utf-8" />
<title><#BLOG_POST_TITLE></title>
<link rel="stylesheet" href="/etc/simple.css" />
<link rel="stylesheet" href="/bakedres/simple.css" />
<#META_TAGS>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.11.1/build/styles/rainbow.min.css">
<script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.11.1/build/highlight.min.js"></script>
<link rel="stylesheet" href="/bakedres/hljs-rainbow.css">
<script src="/bakedres/highlight.js"></script>
</head>
<body>
<div id="content"></div>
@ -17,15 +17,15 @@
<span>Running since: <#RUNNING_SINCE></span>
</div>
</footer>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<script src="/bakedres/marked.js"></script>
<script>
document.getElementById("content").innerHTML = marked.parse(`<#BLOG_POST_MARKDOWN>`);
</script>
<#ifdef HOTRELOAD>
<script src="/etc/hotreload.js"></script>
<script src="/bakedres/hotreload.js"></script>
<#endif>
<script src="/etc/theme.js"></script>
<script src="/bakedres/theme.js"></script>
<script>hljs.highlightAll();</script>
</body>
</html>