python - Serve static files from sub-app in web.py -
i writing web-app in web.py (a rewrite/extension of mongs) want function both standalone application, , sub-app requests can forwarded to. issue having when used sub-app, static files cannot served own static directory. since intend distribute (and not require users combine files project's static directory) want directory structure be:
app_that_is_using_mongs (not mine) static (which holds app's static files - not mine) mongs (my subapp) main.py (the code mongs) view (holds templates) static (the static folder mongs) main.py (the code app using mongs) ...so entire mongs directory separated whatever app using it.
i have considered few possibilities getting work:
using request handler reads , outputs files static directory, like:
cwd = os.path.dirname(__file__) + '/' # current working directory class static: def get(self, filename): """searches , returns requested static file or 404s out""" try: return open(cwd + 'static/' + filename, 'r').read() except: web.application.notfound(app) # file not found
i not sure performance of solution large files, , seems should web.py can on own.
adding static directory accessing cherry.py staticdir tool through web.py... i'm not sure how (interacting directly server web.py running on), , don't think still work if switched gunicorn server (or server cherry.py).
fixing way web.py handles static files make more extendable... if there no other way, rewriting portion of web.py , maybe getting pushed main repo best way.
so, best way this?
in web.py static assets aren't served through application router. instead http server has check weather request url starts /static. means weather have sub-application or not, /static/... maps directly static directory in root application.
your first idea of building static class work, right there definite performance implication - though, you'd have benchmark know how bad is.
another option, operationally worse, temporary fix create soft-link static directory of parent app, 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 want access static asset sub_app, hit url like: /static/sub_app/asset. since url starts /static, caught http server , redirect static directory, following soft link, , resolves actual asset. because of sub_app directory, solution work when running sub_app directly, or running parent_app. have setup soft link on every server deploy to, , every development environment, makes less ideal.
Comments
Post a Comment