Related `mod_zip` issue https://github.com/evanmiller/mod_zip/issues/90
has been fixed in commit
288d66541c
By upgrading `mod_zip` to include this fix, we can remove the workaround
that involved using a separate internal location and server to serve
files for zipping.
The COEP (Cross-Origin Embedder Policy) and COOP (Cross-Origin Opener
Policy) headers are needed by EmulatorJS to use the `SharedArrayBuffer`
feature, and enable multi-threaded cores.
These headers are only being set by Nginx for responses to requests
made to the EmulatorJS player path. This is because cross-origin
isolation breaks other features in the application (such as YouTube
embeds), so we only want to enable it for the EmulatorJS player.
It's important to mention that this change does not work when `DEV_MODE`
is set, as we would need Vite server to also set these headers in that
case. This could be implemented separately, if needed.
These changes are not enough to enable multi-threaded cores in the
frontend. Because our application is a SPA, we also need to ensure
that navigation to the EmulatorJS player path results in a full page
reload, so that the cross-origin isolation can be applied. This will be
handled in a separate PR.
The nginx configuration was not updated to use the `ROMM_BASE_PATH`
environment variable when serving the library files.
We no longer hardcode the base path to `/romm`, but instead use the
provided path.
Using the `envsubst` command, we can replace environment variables in
the nginx template files. This allows for more flexibility when
configuring the nginx server.
The Docker image we use as base for Nginx does provide the
`20-envsubst-on-templates.sh` script that will replace environment
variables in the template files.
This change does not include any behavior change, but unblocks future
changes that require environment variables in the nginx configuration.
This change installs and configures the `mod_zip` nginx module [1],
which allows nginx to stream ZIP files directly.
It includes a workaround needed to correctly calculate CRC-32 values for
included files, by including a new `server` section listening at port
8081, only used for the file requests to be upstream subrequests that
correctly trigger the CRC-32 calculation logic.
Also, to be able to provide a `m3u` file generated on the fly, we add a
`/decode` endpoint fully implemented in nginx using NJS, which receives
a `value` URL param, and decodes it using base64. The decoded value is
returned as the response.
That way, the contents of the `m3u` file is base64-encoded, and set as
part of the response, for `mod_zip` to include it in the ZIP file.
[1] https://github.com/evanmiller/mod_zip
Instead of making FastAPI handle file download, which has serious
performance issues on big files [1], this change uses nginx's `X-Accel`
feature to delegate single-file downloads to nginx.
Partial fix for #1079, as it solves the CPU usage issue for single-file
downloads.
[1] https://github.com/fastapi/fastapi/discussions/6050
Fix FastAPI and nginx configuration, to make the application correctly
redirect URLs. This is specially useful when URLs ended with forward
slash are redirected to their stripped version.
Included changes:
* Stop removing the `/api` prefix in nginx rewrite rules, so FastAPI
knows what's the original URL path being requested.
* Use `$http_host` in nginx, so FastAPI receives both the original host
and port, to build the redirect URL (as `$host` does not include the
port, if present).
* Make all FastAPI included routers know their prefix, to correctly
route incoming requests.
This fix was found based on a report that redirects from URLs ended with
forward slash were not working [1].
[1] https://github.com/rommapp/romm/issues/1051#issuecomment-2269049762