summaryrefslogtreecommitdiff
path: root/share/Flask_test/pbserver.py
blob: 3d0caa877f427ee3a523c3af8312d9ca90961c46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/python2.7
from bottle import route, run, post, request, static_file
sys.path.append("./lib")
from Pb import *
import sys, os

from subprocess import call, Popen, PIPE

#FIXME add
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 im.file_json();
  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)