Commit 5e0d3532 authored by Ivar Refsdal's avatar Ivar Refsdal

initial commit

parents
Pipeline #1298 skipped
**/*.sqlite
\ No newline at end of file
FROM python:2.7.11-onbuild
MAINTAINER ivar.refsdal<@nsd.uib.no>
# docker build --tag=nsd/minicache . && docker run -v $(pwd)/cache:/usr/src/app/cache --net=host nsd/minicache
CMD [ "python", "./minicache.py" ]
# Development HTTP cache
Doing `curl localhost:4000/xyz` will make this server fetch (and cache for subsequent requests) `localhost:3000/xyz`.
# Installation and running
docker build --tag=docker.nsd.no/programvareutvikling/minicache .
docker run -v $(pwd)/cache:/usr/src/app/cache --net=host docker.nsd.no/programvareutvikling/minicache
## Warning
The saved cache file `cache/cache.sqlite` is a binary blob (storing Python pickled objects) and you may not be able to read it from different environments.
[loggers]
keys=root, gunicorn.error, gunicorn.access
[handlers]
keys=console
[formatters]
keys=json
[logger_root]
level=INFO
handlers=console
[logger_gunicorn.error]
level=INFO
handlers=console
propagate=0
qualname=gunicorn.error
[logger_gunicorn.access]
level=INFO
handlers=console
propagate=0
qualname=gunicorn.access
[handler_console]
class=StreamHandler
formatter=json
args=(sys.stdout, )
[formatter_json]
class=pythonjsonlogger.jsonlogger.JsonFormatter
format='%(asctime) %(levelname) %(message)'
\ No newline at end of file
import falcon, requests, requests_cache
from wsgiref import simple_server
import gunicorn.app.base
from gunicorn.six import iteritems
import datetime
class SinkAdapter(object):
def __call__(self, req, resp):
print "..."
url = req.url.replace('localhost:4000', 'localhost:3000').split("?")[0]
print "[%s] serving %s, params = %s ..." % (str(datetime.datetime.now()), url, str(req.params))
result = requests.get(url, params=req.params)
resp.status = str(result.status_code) + ' ' + result.reason
for (k, v) in result.headers.items():
resp.set_header(k, v)
resp.body = result.text
app = falcon.API()
app.add_sink(SinkAdapter(), r'/')
class StandaloneApplication(gunicorn.app.base.BaseApplication):
def __init__(self, app, options=None):
self.options = options or {}
self.application = app
super(StandaloneApplication, self).__init__()
def load_config(self):
config = dict([(key, value) for key, value in iteritems(self.options)
if key in self.cfg.settings and value is not None])
for key, value in iteritems(config):
self.cfg.set(key.lower(), value)
def load(self):
return self.application
if __name__ == "__main__":
print '[%s] booting server' % (str(datetime.datetime.now()))
options = {
'workers' : '4',
'logconfig' : 'gunicorn_logging.conf',
'bind' : '127.0.0.1:4000',
}
requests_cache.install_cache('cache/cache')
StandaloneApplication(app, options).run()
#httpd = simple_server.make_server('127.0.0.1', 4000, app)
#httpd.serve_forever()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment