Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend+Frontend all-in-one binary #19

Open
CrescentKohana opened this issue Apr 6, 2022 · 2 comments
Open

Backend+Frontend all-in-one binary #19

CrescentKohana opened this issue Apr 6, 2022 · 2 comments
Labels
help wanted Extra attention is needed server Concerns Mangatsu server web Concerns Mangatsu web

Comments

@CrescentKohana
Copy link
Member

CrescentKohana commented Apr 6, 2022

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.

@CrescentKohana CrescentKohana added help wanted Extra attention is needed server Concerns Mangatsu server web Concerns Mangatsu web labels Apr 6, 2022
@CrescentKohana CrescentKohana changed the title Server and Web Backend+Frontend all-in-one binary Apr 6, 2022
@karmek-k
Copy link
Collaborator

karmek-k commented Apr 7, 2022

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:

  1. Move the frontend project to repo.
  2. 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
  3. 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.
  4. 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.

@CrescentKohana
Copy link
Member Author

Thanks for the suggestion! I'll keep this in mind.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed server Concerns Mangatsu server web Concerns Mangatsu web
Projects
None yet
Development

No branches or pull requests

2 participants