Mangatsu / server

🌕 Media server for storing, tagging and viewing doujinshi, manga, art collections and other galleries with API and user control. Written in Go.
GNU General Public License v3.0
43 stars 6 forks source link

Backend+Frontend all-in-one binary #19

Open CrescentKohana opened 2 years ago

CrescentKohana commented 2 years ago

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.

karmek-k commented 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:

  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 commented 2 years ago

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