Created
October 2, 2014 18:30
-
-
Save ewdurbin/4959143a88eaa2349209 to your computer and use it in GitHub Desktop.
pipsy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
upstream s3proxy { | |
server localhost:18000; | |
} | |
upstream pipsy { | |
server localhost:5000; | |
} | |
server { | |
listen 4000; | |
access_log /var/log/nginx/s3proxy-access.log; | |
error_log /var/log/nginx/s3proxy-error.log; | |
keepalive_timeout 5; | |
client_max_body_size 40M; | |
location @s3proxy { | |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
proxy_set_header Host kissmetrics-python-pkgs.s3.amazonaws.com; | |
more_clear_headers Authorization; | |
proxy_redirect off; | |
proxy_pass http://s3proxy; | |
} | |
location /python { | |
server_name_in_redirect off; | |
port_in_redirect off; | |
proxy_pass http://pipsy; | |
proxy_intercept_errors on; | |
recursive_error_pages on; | |
error_page 404 = @s3proxy; | |
limit_except GET HEAD { | |
auth_basic 'Restricted'; | |
auth_basic_user_file /etc/nginx/python-users; | |
} | |
} | |
location / { | |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
proxy_set_header Host kissmetrics-python-pkgs.s3.amazonaws.com; | |
more_clear_headers Authorization; | |
proxy_redirect off; | |
proxy_pass http://s3proxy; | |
limit_except GET HEAD { | |
auth_basic 'Restricted'; | |
auth_basic_user_file /etc/nginx/python-users; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import logging | |
import boto | |
from flask import Flask | |
from flask import jsonify | |
from flask import redirect | |
from flask import url_for | |
from flask_sslify import SSLify | |
logging_handler = logging.StreamHandler() | |
APP = Flask(__name__) | |
sslify = SSLify(APP) | |
PIPSY_BUCKET = os.getenv('PIPSY_BUCKET') | |
PIPSY_SIMPLE_ROOT = os.getenv('PIPSY_SIMPLE_ROOT') | |
class FlaskException(Exception): | |
def __init__(self, message, status_code=None, payload=None): | |
Exception.__init__(self) | |
self.message = message | |
if status_code is not None: | |
self.status_code = status_code | |
self.payload = payload | |
def to_dict(self): | |
rv = dict(self.payload or ()) | |
rv['message'] = self.message | |
return rv | |
SIMPLE_TEMPLATE=""" | |
<html> | |
<head> | |
<title>Simple Index</title> | |
<meta name="api-version" value="2" /> | |
</head> | |
<body> | |
{body} | |
</body> | |
</html> | |
""" | |
PKG_TEMPLATE=""" | |
<html> | |
<head> | |
<title>Links for {pkg_name}</title> | |
<meta name="api-version" value="2" /> | |
</head> | |
<body> | |
{body} | |
</body> | |
</html> | |
""" | |
s3_conn = boto.connect_s3() | |
s3_bucket = s3_conn.get_bucket(PIPSY_BUCKET) | |
@APP.errorhandler(FlaskException) | |
def handle_flask_error(error): | |
response = jsonify(error.to_dict()) | |
response.status_code = error.status_code | |
return response | |
@APP.route('/', methods=['GET'], defaults={'path': ''}) | |
@APP.route('/<path:path>') | |
def root_route(path): | |
keys = [] | |
for key in s3_bucket.list(prefix=path,delimiter="/"): | |
keys.append(key.name) | |
if len(keys) == 1: | |
if keys[0] == path+"/": | |
return redirect(path+"/") | |
elif not keys[0].endswith("/"): | |
return "Not Found", 404 | |
if path == PIPSY_SIMPLE_ROOT+"/": | |
body = "" | |
for key in keys: | |
body+="<a href='%s'>%s</a><br/>\n" % (os.path.basename(key[:-1]), os.path.basename(key[:-1])) | |
return SIMPLE_TEMPLATE.format(body=body) | |
body = "" | |
pkg_name = os.path.basename(path[:-1]) | |
for key in keys: | |
if key != path: | |
body+="<a href='%s'>%s</a><br/>\n" % (os.path.basename(key), os.path.basename(key)) | |
return PKG_TEMPLATE.format(body=body, pkg_name=pkg_name) | |
if __name__ == "__main__": | |
APP.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment