You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is absolutely possible with the help of the embed package. However, the method I used doesn't support Next.js server-side rendering, this would have to be treated a different way, as it just takes a client built by a bundler and embeds them into a binary.
The client can however use an API communication library like axios to fetch data.
Here is the reference project. It's quite old, unfinished and uses some bad practices, but it's perfect for demonstrating what has to be done:
Create a variable of type embed.FS and on the line above it, add a comment that looks like this (main.go, lines 12-13): //go:embed relative/path/to/the/built/webapp
Generate a http.FileSystem object - router.go, lines 13-21. Make sure to pass the same path to fs.Sub as above - the server will "change directory" to it.
Serve the filesystem with your router - I used Gin, here the method for serving is StaticFS() (router.go, line 29).
However, Gin (or rather the underlying router - julienschmidt/httprouter) won't allow for hosting a static filesystem on / and an API on /api.
For this reason, I decided to redirect all GET requests from / to /web (router.go, lines 31-35) and to change the base path in Vite.js config in order to prevent front-end routing issues.
I can see that the server uses gorilla/mux. I haven't ever used this, so I have no idea if the problem above is going to arise.
Mangatsu/server and Mangatsu/web as single (or one for each OS: Windows, Linux, MacOS if needed) all-in-one binary which could be run as-is.
The text was updated successfully, but these errors were encountered: