Proper app.yaml handlers configuration for Google App Engine for a static html website

戏子无情 提交于 2019-12-06 12:54:05

问题


Here is the file structure of my website:

public
│   404.html
│   app.yaml
│   index.html
│   index.xml
│   prereqs.zip
│   sitemap.xml
│   sof2018.py
|
├───categories
│       index.html
│       index.xml
├───css
│       styles.css
├───home
│       index.html
├───js
│       scripts.js
├───prerequisites
│       index.html
├───scripts
│   │   index.html
│   │   index.xml
│   │
│   ├───page
│   │   └───1
│   │           index.html
│   │
│   └───sof2018
│           index.html
├───tags
│       index.html
│       index.xml
└───usage
        index.html

Basically I need to ensure the following:

  • if any folder is called with trailing slash (say /scripts/page/1/ or /home/), it should look for index.html inside that folder
  • if any folder is called without trailing slash (say /scripts/page/1 or /home), it should look for index.html inside that folder
  • if any file (say /css/styles.css or /prereqs.zip) is called, it should return the file

The way to differentiate between files and folders could be to simply to check if there is a period(.) since all my files have extensions. Though I guess someone else might benefit with a solution that handles extension-less files also.

I have tried a number of different regexes, all of them seem to missing out some case or the other. Also tried all the solutions proposed in previous questions on stackoverflow on pretty much the same thing.

I'm not looking for an app.yaml tailor-made for my website, that can be done by manually dealing with each of the folders, cases, etc.

I'm looking for a general solution that would work exactly how a traditional web host would work, since I'm guessing that would benefit others as well. Then I'll be free to add/change content/structure without having to update app.yaml every time.

P.S. In all honesty, google should have themselves provided the solution, but they haven't. I'm guessing even proper web devs aren't regex masters.

P.S.2 The website is made using the Hugo static site generator. I can confirm it is working on a traditional web host (GoDaddy). Also I don't know enough html/css/js to tell for sure what is the actual internal working of the site; if required I can provide the source files.


回答1:


You cannot achieve all of this on (standard environment) GAE with a completely static website. That's because you have no way of telling ahead of time (i.e. when creating the app.yaml config) if /some/path/item is a file or a folder and thus /some/path/item or /some/path/item/index.html should respectively be returned.

It's not a matter of writing the right regex, it's a matter of wanting 2 different outcomes for the same regex.

To achieve what you want you need the app to check the condition at runtime and make the decision - not a static site anymore.

If you drop your 2nd bullet you can achieve the rest with this:

- url: /(.*)/
  static_files: public/\1/index.html
  upload: public/.*/index.html

- url: /(.*)$
  static_files: public/\1
  upload: public/.*

If you have a way of differentiating files from folders you may be able to come up with a scheme. For example by specifying that all filenames must have extensions (and directories cannot have . in their names), then you can do something like this:

# /optional/path/folder/ - serve index.html inside
- url: /(.*)/
  static_files: public/\1/index.html
  upload: public/.*/index.html     

# /optional/path/name.extension => files - serve them
- url: /((.*\/)*[^\/]+\.[^\/]+)$
  static_files: public/\1
  upload: public/.*

# anything else is a folder - serve index.html inside
- url: /(.*)$
  static_files: public/\1/index.html
  upload: public/.*/index.html


来源:https://stackoverflow.com/questions/54616748/proper-app-yaml-handlers-configuration-for-google-app-engine-for-a-static-html-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!