naturalcrit / homebrewery

Create authentic looking D&D homebrews using only markdown
https://homebrewery.naturalcrit.com
MIT License
1.07k stars 327 forks source link

[LOCAL]: Enable serving local images #1958

Open G-Ambatte opened 2 years ago

G-Ambatte commented 2 years ago

By modifying server.js slightly, we can allow users with local installs to serve images from their local directory, meaning that they will no long need to rely on services outside of their local machine (e.g. Imgur.com).

Steps:

  1. Create a directory "staticImages" in the project root directory.

  2. Add the following lines to server.js:

    if(process.env.NODE_ENV == 'local') {
    app.use(express.static(`./staticImages`));
    }
  3. Restart the Homebrewery server.


As you will see in the following screenshot, my Ubuntu local install can now access images hosted in the staticImages folder. image

EDIT: In the above image, my specific configuration is visible - I have symlinked /home/hb-test/images to /usr/local/homebrewery/staticImages, which is why two different paths are shown - I can upload files via WinSCP to /home/hb-test/images while Homebrewery can serve the files locally from /usr/local/homebrewery/staticImages. The above change does not include any such folder linking and putting it in place it is left as an exercise for the individual user, if that is what they should desire.

G-Ambatte commented 2 years ago

A slightly better approach may be along the lines of:

const localEnvironments = ['local','docker','other'];
if(localEnvironments.includes(process.env.NODE_ENV) {
    [...]
}

This allows the same static directory logic to be applied to other environments than just local, as deemed appropriate, and is easily extended to any additional NODE_ENV settings that might be developed in the future.

It also might be appropriate to store the content of localEnvironments in a config file.