Open CrescentKohana opened 2 years ago
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:
frontend
project to repo.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
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.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.
Thanks for the suggestion! I'll keep this in mind.
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.