Changing static directory path in webpy

I would really like to change the static Webpy directory without having to configure and run nginx locally. Right now, it seems that webpy will create a static directory if / static / exists. In my case, I want to use / foo / bar / as my static directory, but could not find any information related to setting this up (other than using apache or nginx locally).

This is used only for local use and not for production. Any ideas? Thanks

+6
source share
1 answer

If you need a different directory for the same path, you can subclass web.httpserver.StaticMiddleware or write your own middleware like this (these are StaticApp tricks by changing PATH_INFO):

import web import os import urllib import posixpath urls = ("/.*", "hello") app = web.application(urls, globals()) class hello: def GET(self): return 'Hello, world!' class StaticMiddleware: """WSGI middleware for serving static files.""" def __init__(self, app, prefix='/static/', root_path='/foo/bar/'): self.app = app self.prefix = prefix self.root_path = root_path def __call__(self, environ, start_response): path = environ.get('PATH_INFO', '') path = self.normpath(path) if path.startswith(self.prefix): environ["PATH_INFO"] = os.path.join(self.root_path, web.lstrips(path, self.prefix)) return web.httpserver.StaticApp(environ, start_response) else: return self.app(environ, start_response) def normpath(self, path): path2 = posixpath.normpath(urllib.unquote(path)) if path.endswith("/"): path2 += "/" return path2 if __name__ == "__main__": wsgifunc = app.wsgifunc() wsgifunc = StaticMiddleware(wsgifunc) wsgifunc = web.httpserver.LogMiddleware(wsgifunc) server = web.httpserver.WSGIServer(("0.0.0.0", 8080), wsgifunc) print "http://%s:%d/" % ("0.0.0.0", 8080) try: server.start() except KeyboardInterrupt: server.stop() 

Or you can create a symbolic link called "static" and point it to another directory.

+5
source

Source: https://habr.com/ru/post/894402/


All Articles