summaryrefslogtreecommitdiff
path: root/Flask_test/pbserver.py
diff options
context:
space:
mode:
Diffstat (limited to 'Flask_test/pbserver.py')
-rwxr-xr-xFlask_test/pbserver.py133
1 files changed, 133 insertions, 0 deletions
diff --git a/Flask_test/pbserver.py b/Flask_test/pbserver.py
new file mode 100755
index 0000000..321619f
--- /dev/null
+++ b/Flask_test/pbserver.py
@@ -0,0 +1,133 @@
+#!/usr/bin/python2.7
+from bottle import route, run, post, request, static_file
+import sys
+sys.path.append("./lib")
+#FIXME probably can get away with import * here
+from Pb.Break import PbBreak
+from Pb.Generate import PbGenerate
+from Pb.Gradient import PbGradient
+from Pb.Grid import PbGrid
+from Pb.Landscape import PbLandscape
+from Pb.Pattern import PbPattern
+
+from Config import AWS_SECRET_ACCESS_KEY, AWS_ACCESS_KEY_ID, BUCKET_NAME, BIN_IDENTIFY
+from Pb import Pb
+from Db import Db
+
+import os
+import sys
+
+import sha
+from subprocess import call, Popen, PIPE
+import simplejson as json
+
+from boto.s3.connection import S3Connection
+from boto.s3.key import Key
+#
+try:
+ db = Db();
+except Exception as e:
+ sys.stderr.write("Could not connect to db:\n{}".format(e))
+ sys.exit(1);
+
+BASE_URL = "http://i.asdf.us"
+
+def s3move(filename,objectname):
+ try:
+ conn = S3Connection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, is_secure=False)
+ b = conn.get_bucket(BUCKET_NAME)
+ k = Key(b)
+ k.key = objectname
+ k.set_contents_from_filename(filename)
+ k.set_acl('public-read')
+ k.storage_class = 'REDUCED_REDUNDANCY'
+ except Exception as e:
+ sys.stderr.write(str(e));
+ raise(e)
+
+
+
+def return_image(im, insert_url="NULL"):
+ return format_im_data(im, insert_url)
+
+
+def return_jsonp(im, insert_url="NULL"):
+ return "{}({})".format(im.get("callback"), format_im_data(im, insert_url))
+
+
+def _pb_post(pb_class, request):
+ try:
+ im = pb_class(**(dict(request.forms)))
+ im.create();
+ return return_image(im)
+ except Exception as e:
+ sys.stderr.write("%s failure" % pb_class.__name__)
+ sys.stderr.write("params:\n")
+ for i in request.forms:
+ sys.stderr.write("{}:{}\n".format(i, request.forms[i]))
+ raise;
+ return json.dumps({ 'error' : 'Request could not be processed' })
+
+@post('/im/api/imgradient')
+def gradient():
+ _pb_post(PbGradient, request)
+
+@post('/im/api/imgrid')
+def imgrid():
+ _pb_post(PbLandscape, request)
+
+@post('/im/api/generate')
+def generate():
+ _pb_post(PbGenerate, request)
+
+@post('/im/api/imbreak')
+ _pb_post(PbBreak, request)
+
+@post('/im/api/impattern')
+def pattern():
+ _pb_post(PbPattern, request)
+
+@post('/im/api/imlandscape')
+def imlandscape():
+ _pb_post(Imlandscape, request)
+
+
+
+#static routes
+@route('/im/<filename>')
+def server_static(filename):
+ return static_file(filename, root='frontend/im/')
+@route('/im')
+def server_static():
+ return static_file("index.html", root='frontend/im/')
+@route('/imgrid')
+def server_static():
+ return static_file("index.html", root='frontend/imgrid/')
+@route('/imgradient')
+def server_static():
+ return static_file("index.html", root='frontend/imgradient/')
+@route('/imlandscape')
+def server_static():
+ return static_file("index.html", root='frontend/imlandscape/')
+@route('/impattern')
+def server_static():
+ return static_file("index.html", root='frontend/impattern/')
+@route('/imbreak')
+def server_static():
+ return static_file("index.html", root='frontend/imbreak/')
+@route('/')
+def server_static():
+ return static_file("index.html", root='frontend/im/')
+@route('/css/<filename>')
+def server_static(filename):
+ return static_file(filename, root='frontend/css/')
+@route('/js/<filename>')
+def server_static(filename):
+ return static_file(filename, root='frontend/js/')
+@route('/img/<filename>')
+def server_static(filename):
+ return static_file(filename, root='frontend/img/')
+
+
+run(host='0.0.0.0', server='flup', port=8999, debug=True)
+#run(host='0.0.0.0', port=8999, debug=True)