Hacker News new | ask | show | jobs
by andreygrehov 1451 days ago
Congrats!

> One Binary

> Build frontend apps along with your backend and ship everything in a single binary.

I'm doing something similar and love it. Do you embed the entire `public` directory and then traverse the embed.FS to access the files in memory?

4 comments

> I'm doing something similar and love it. Do you embed the entire `public` directory and then traverse the embed.FS to access the files in memory?

I've done this in Rust before, I'm sharing it here as I'm assuming that Go has something similar.

I'm basically hard-coding the paths in the backend to also serve static assets, and embed the bytes of the asset at compile time, so when it runs, it's just serving it straight from memory. Here how it looks for style.css for example: https://codeberg.org/ditzes/ditzes/src/branch/master/src-tau...

It'd be trivial to move the structure to something like a map instead, where the URL is the key and another map where bytes, headers and such is stored. Mostly I didn't, because I'm just embedding few amount of files.

Thank you! Go supports embedding static files natively [1]. Copper builds on top of it and provides the tooling to do it seamlessly for web projects.

[1]: https://pkg.go.dev/embed

embed.FS is really cool. If you wanna see what it looks like, check out how I build ntfy [0]. It ships with the docs (built from mkdocs, see [1]) and the React web app [2] all in a single binary.

Here are the embed statements [3]. One thing to note is that embed.FS does not send 304 Not Modified status back, which is why I made a CachingEmbedFS [4].

[0] https://github.com/binwiederhier/ntfy/blob/main/Makefile#L77

[1] https://ntfy.sh/docs/

[2] https://ntfy.sh/app

[3] https://github.com/binwiederhier/ntfy/blob/main/server/serve...

[4] https://github.com/binwiederhier/ntfy/blob/main/util/embedfs...

That is how I am building mine and have thoroughly enjoyed it. Especially since 1.18 updated support to allow directories / files that start with an underscore.

I am using svelte for the frontend and that was biting me.