mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
parent
de04d7a854
commit
f571b19d8a
1 changed files with 17 additions and 0 deletions
17
app.py
17
app.py
|
|
@ -2,6 +2,7 @@ from sanic import Sanic
|
||||||
from sanic import response
|
from sanic import response
|
||||||
from sanic_jinja2 import SanicJinja2
|
from sanic_jinja2 import SanicJinja2
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
from functools import wraps
|
||||||
import json
|
import json
|
||||||
|
|
||||||
app = Sanic(__name__)
|
app = Sanic(__name__)
|
||||||
|
|
@ -12,7 +13,21 @@ conn = sqlite3.connect('file:northwind.db?immutable=1', uri=True)
|
||||||
conn.row_factory = sqlite3.Row
|
conn.row_factory = sqlite3.Row
|
||||||
|
|
||||||
|
|
||||||
|
def sqlerrors(fn):
|
||||||
|
@wraps(fn)
|
||||||
|
async def inner(*args, **kwargs):
|
||||||
|
try:
|
||||||
|
return await fn(*args, **kwargs)
|
||||||
|
except sqlite3.OperationalError as e:
|
||||||
|
return response.json({
|
||||||
|
'ok': False,
|
||||||
|
'error': str(e),
|
||||||
|
})
|
||||||
|
return inner
|
||||||
|
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
|
@sqlerrors
|
||||||
async def index(request, sql=None):
|
async def index(request, sql=None):
|
||||||
sql = sql or request.args.get('sql', '')
|
sql = sql or request.args.get('sql', '')
|
||||||
if not sql:
|
if not sql:
|
||||||
|
|
@ -26,6 +41,7 @@ async def index(request, sql=None):
|
||||||
|
|
||||||
|
|
||||||
@app.route('/<table:[a-zA-Z0-9].*>.json')
|
@app.route('/<table:[a-zA-Z0-9].*>.json')
|
||||||
|
@sqlerrors
|
||||||
async def table_json(request, table):
|
async def table_json(request, table):
|
||||||
sql = 'select * from {} limit 20'.format(table)
|
sql = 'select * from {} limit 20'.format(table)
|
||||||
return response.json([
|
return response.json([
|
||||||
|
|
@ -34,6 +50,7 @@ async def table_json(request, table):
|
||||||
|
|
||||||
|
|
||||||
@app.route('/<table:[a-zA-Z0-9].*>')
|
@app.route('/<table:[a-zA-Z0-9].*>')
|
||||||
|
@sqlerrors
|
||||||
async def table(request, table):
|
async def table(request, table):
|
||||||
sql = 'select * from {} limit 20'.format(table)
|
sql = 'select * from {} limit 20'.format(table)
|
||||||
return await index(request, sql)
|
return await index(request, sql)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue