3 ms·
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
by andreygrehov 4y 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?
- leetrout 4y agoThat 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.
- capableweb 4y ago> 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-tauri/src/api.rs#L326 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.
- tusharsoni 4y agoThank 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 https://pkg.go.dev/embed
- binwiederhier 4y agoembed.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 https://github.com/binwiederhier/ntfy/blob/main/Makefile#L77 [1] https://ntfy.sh/docs/ https://ntfy.sh/docs/ [2] https://ntfy.sh/app https://ntfy.sh/app [3] https://github.com/binwiederhier/ntfy/blob/main/server/server.go#L79-L88 https://github.com/binwiederhier/ntfy/blob/main/server/serve... [4] https://github.com/binwiederhier/ntfy/blob/main/util/embedfs.go https://github.com/binwiederhier/ntfy/blob/main/util/embedfs...