diff --git a/db.sqlite3 b/db.sqlite3 deleted file mode 100644 index 75bb23b..0000000 Binary files a/db.sqlite3 and /dev/null differ diff --git a/mocha-server.py b/mocha-server.py index 3a80541..190f7fa 100755 --- a/mocha-server.py +++ b/mocha-server.py @@ -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): 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)))] start_response(status, response_headers) diff --git a/mocha.db b/mocha.db new file mode 100644 index 0000000..de31422 Binary files /dev/null and b/mocha.db differ