Add minimal read-only sqlite database
This commit is contained in:
parent
042c334447
commit
61002616b0
3 changed files with 42 additions and 2 deletions
BIN
db.sqlite3
BIN
db.sqlite3
Binary file not shown.
|
@ -1,8 +1,48 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
import json
|
||||||
|
import sqlite3
|
||||||
|
|
||||||
|
|
||||||
|
DATABASE = 'mocha.db'
|
||||||
|
|
||||||
|
|
||||||
|
def fetch(user_id):
|
||||||
|
conn = sqlite3.connect(DATABASE)
|
||||||
|
conn.row_factory = sqlite3.Row
|
||||||
|
c = conn.cursor()
|
||||||
|
|
||||||
|
if user_id != '*':
|
||||||
|
c.execute("SELECT * FROM users WHERE user_id=?", user_id)
|
||||||
|
else:
|
||||||
|
c.execute("SELECT * FROM users")
|
||||||
|
|
||||||
|
output = c.fetchall()
|
||||||
|
output = json.dumps([dict(row) for row in output])
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
if output is None:
|
||||||
|
output = ('Error',)
|
||||||
|
return output
|
||||||
|
|
||||||
|
|
||||||
|
def process_request(uri):
|
||||||
|
parts = uri.split('/')[1:]
|
||||||
|
assert parts[0] == 'mocha'
|
||||||
|
|
||||||
|
output = ''
|
||||||
|
|
||||||
|
if parts[1] == 'users':
|
||||||
|
output = fetch(parts[2])
|
||||||
|
return output
|
||||||
|
|
||||||
|
print(parts)
|
||||||
|
|
||||||
|
|
||||||
def application(environ, start_response):
|
def application(environ, start_response):
|
||||||
status = '200 OK'
|
status = '200 OK'
|
||||||
output = environ['REQUEST_URI'].encode('UTF-8')
|
output = process_request(environ['REQUEST_URI']).encode('UTF-8')
|
||||||
|
|
||||||
response_headers = [('Content-type', 'text/plain'),
|
response_headers = [('Content-type', 'application/json'),
|
||||||
('Content-Length', str(len(output)))]
|
('Content-Length', str(len(output)))]
|
||||||
start_response(status, response_headers)
|
start_response(status, response_headers)
|
||||||
|
|
||||||
|
|
BIN
mocha.db
Normal file
BIN
mocha.db
Normal file
Binary file not shown.
Reference in a new issue