문제

I am writing a web-app in web.py (a rewrite/extension of mongs) which I want to function both as a standalone application, and as a sub-app that requests can be forwarded to. The issue that I am having is that when it is used as a sub-app, static files cannot easily be served from its own static directory. Since I intend to distribute this (and not require users to combine the files into their project's static directory) I want the directory structure to be:

app_that_is_using_mongs (not mine)
    static (which holds the app's static files - also not mine)
    mongs (my subapp)
        main.py (the code for mongs)
        view (holds templates)
        static (the static folder for mongs)
    main.py (the code for the app that is using mongs)

...so that the entire mongs directory is separated from whatever app is using it.

I have considered a few possibilities for getting this to work:

  • Using a request handler that reads and outputs the files from the static directory, like:

    cwd = os.path.dirname(__file__) + '/'  # get current working directory
    
    class Static:
        def GET(self, filename):
            """searches for and returns a requested static file or 404s out"""
            try:
                return open(cwd + 'static/' + filename, 'r').read()
            except:
                web.application.notfound(app)  # file not found
    

I am not sure about the performance of this solution for large files, and it seems like this should be something web.py can do on its own.

  • Adding another static directory by accessing the cherry.py staticdir tool through web.py... I'm not sure how to do something like this (interacting directly with the server that web.py is running on), and I don't think it would still work if I switched to a Gunicorn server (or any server but cherry.py).

  • Fixing the way that web.py handles static files to make it more extendable... If there is no other way, then rewriting this portion of web.py and maybe getting it pushed into the main repo is probably the best way.

So, what is the best way to do this?

도움이 되었습니까?

해결책

In web.py the static assets aren't served through the application router. Instead the http server has a check weather the request url starts with /static. This means that weather you have a sub-application or not, the /static/... maps directly to the static directory in the root application.

Your first idea of building a static class will definitely work, but you are right that there is a definite performance implication - though, you'd have to benchmark it to really know how bad it is.

Another option, which is operationally worse, but is a temporary fix is to create a soft-link from the static directory of the parent app, into the sub-application's static directory. i.e.

parent_app/
    static/
        sub_app/ -> parent_app/sub_app/static/sub_app
        ...
    sub_app/
        static/
            sub_app/
                ...

Then, when you want to access a static asset from sub_app, you would hit a url like: /static/sub_app/asset. Since this url starts with /static, it will get caught by the http server and redirect to the static directory, following the soft link, and resolves to the actual asset. Because of the sub_app directory, this solution will work when running the sub_app directly, or running the parent_app. You will have to setup this soft link on every server you deploy to, and for every development environment, which makes this less than ideal.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top