-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·72 lines (55 loc) · 2.02 KB
/
server.py
File metadata and controls
executable file
·72 lines (55 loc) · 2.02 KB
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
#!/usr/bin/env python3
import asyncio
import logging
from aiohttp import web
import vyper
from vyper.compiler import compile_code
from vyper.exceptions import VyperException
from concurrent.futures import ThreadPoolExecutor
routes = web.RouteTableDef()
headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "X-Requested-With, Content-type"
}
executor_pool = ThreadPoolExecutor(max_workers=4)
@routes.get('/')
async def handle(request):
return web.Response(text='Vyper Compiler. Version: {} \n'.format(vyper.__version__))
def _compile(data):
code = data.get('code')
if not code:
return {'status': 'failed', 'message': 'No "code" key supplied'}, 400
if not isinstance(code, str):
return {'status': 'failed', 'message': '"code" must be a non-empty string'}, 400
try:
out_dict = compile_code(code, ['abi', 'bytecode', 'bytecode_runtime', 'ir', 'method_identifiers'])
out_dict['ir'] = str(out_dict['ir'])
except VyperException as e:
if e.col_offset and e.lineno:
col_offset, lineno = e.col_offset, e.lineno
elif e.annotations and len(e.annotations) > 0:
ann = e.annotations[0]
col_offset, lineno = ann.col_offset, ann.lineno
else:
col_offset, lineno = None, None
return {
'status': 'failed',
'message': str(e),
'column': col_offset,
'line': lineno
}, 400
out_dict.update({'status': "success"})
return out_dict, 200
@routes.route('OPTIONS', '/compile')
async def compile_it_options(request):
return web.json_response(status=200, headers=headers)
@routes.post('/compile')
async def compile_it(request):
json = await request.json()
loop = asyncio.get_event_loop()
out, status = await loop.run_in_executor(executor_pool, _compile, json)
return web.json_response(out, status=status, headers=headers)
app = web.Application()
app.add_routes(routes)
logging.basicConfig(level=logging.DEBUG)
web.run_app(app)