59 lines
1.4 KiB
Python
59 lines
1.4 KiB
Python
# standard imports
|
|
import os
|
|
import urllib.parse
|
|
import logging
|
|
import re
|
|
|
|
# external imports
|
|
from leveldir.uuid import UUIDDir
|
|
|
|
# local imports
|
|
from gas3.base import do_yield
|
|
from gas3.register import do_register
|
|
import gas3.error
|
|
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
logg = logging.getLogger()
|
|
|
|
data_path = os.environ.get('GAS3_DATA_DIR', 'data')
|
|
data_dir = UUIDDir(data_path)
|
|
|
|
re_yield = re.compile('^/yield')
|
|
re_register = re.compile('^/register')
|
|
|
|
|
|
def application(env, start_response):
|
|
req = urllib.parse.urlparse(env['REQUEST_URI'])
|
|
scode = 200
|
|
stext = 'OK'
|
|
|
|
r = ''
|
|
if re.match(re_yield, req.path):
|
|
r = do_yield(req, env, data_dir)
|
|
elif re.match(re_register, req.path):
|
|
try:
|
|
r = do_register(req, env, data_dir)
|
|
except gas3.error.Used:
|
|
scode = 410
|
|
stext = 'Code already used'
|
|
except gas3.error.InvalidInput as e:
|
|
scode = 400
|
|
stext = 'Malformed request url'
|
|
r = str(e)
|
|
except FileNotFoundError:
|
|
scode = 403
|
|
stext = 'No such code'
|
|
except Exception as e:
|
|
scode = 500
|
|
stext = 'Server error'
|
|
r = str(e)
|
|
else:
|
|
r = ''
|
|
|
|
|
|
l = len(r)
|
|
status = str(scode) + ' ' + stext
|
|
start_response(status, [('Content-Length', str(l))])
|
|
return [r.encode('utf-8')]
|