Serving static files on Google AppEngine with a prefixed url

40 Views Asked by At

I have an existing GAE service (default) and I am adding a new service (admin). This is using python3.8, gunicorn and flask.

I have a dispatch.yaml file which redirects requests with /admin to the new service. And I write the app.yaml in the new service to only respond to urls with this prefix.

The directory structure is a bit messy, but it is as follows:

<root>
| app.yaml
| dispatch.yaml
| main.py
| static
| | css
| | | x.css
| | | y.css
| admin
| | admin_service.yaml
| | main.py
| | static
| | | main.css

The admin_service.yaml looks like this:

runtime: python38
service: admin

handlers:
- url: admin/static
  static_dir: static

- url: /admin/.*
  script: auto

The problem is: when I run the admin_service, either locally or on GAE, it cannot find my static resources. Requests for <server>/admin/static/main.css return 404, but <server>/static/main.css return my files. This does not help, though, because all requests for this service must be under the /admin/ prefix.

I tried many variations, including using static_files, with the same issue.

I expected that /admin/static would be interpreted as a prefix and that everything after this would be appended to static_dir to generate a resource path.

Instead, only the actual directory name matters.

1

There are 1 best solutions below

0
NoCommandLine On

You have a file <server>/admin/static/main.css and you want anything that starts with /admin/static to be handled by the static path handler you've defined. Your url path should thus be /admin/static and your directory should be static/

- url: /admin/static
  static_dir: static/

In addition, you should ensure that if you're using relative urls on your admin pages for the static files, they end up with a full url of <server>/admin/static/main.css and not <server>/static/main.css cos the latter is for your default service.