From 2bfbe7568a8c6477de60a676d9027dcb9714af42 Mon Sep 17 00:00:00 2001 From: Kristofer Hallin Date: Fri, 29 Oct 2021 09:29:16 +0200 Subject: Use FastAPI + JWT instead of Falcon. --- src/db.py | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) (limited to 'src/db.py') diff --git a/src/db.py b/src/db.py index 2308e8c..7e83d96 100755 --- a/src/db.py +++ b/src/db.py @@ -7,28 +7,41 @@ # value if you're too quick with generating the timestamps, ie # invoking time.time() several times quickly enough. +import os +import sys import time import couch -import index + +from index import CouchIindex class DictDB(): - def __init__(self, database, hostname, username, password): + def __init__(self): """ Check if the database exists, otherwise we will create it together with the indexes specified in index.py. """ + try: + self.database = os.environ['COUCHDB_NAME'] + self.hostname = os.environ['COUCHDB_HOSTNAME'] + self.username = os.environ['COUCHDB_USER'] + self.password = os.environ['COUCHDB_PASSWORD'] + except KeyError: + print('The environment variables COUCHDB_NAME, COUCHDB_HOSTNAME,' + + ' COUCHDB_USER and COUCHDB_PASSWORD must be set.') + sys.exit(-1) + self.server = couch.client.Server( - f"http://{username}:{password}@{hostname}:5984/") + f"http://{self.username}:{self.password}@{self.hostname}:5984/") try: - self.couchdb = self.server.database(database) + self.couchdb = self.server.database(self.database) except couch.exceptions.NotFound: print("Creating database and indexes.") - self.couchdb = self.server.create(database) + self.couchdb = self.server.create(self.database) - for i in index.indexes: + for i in CouchIindex(): self.couchdb.index(i) self._ts = time.time() @@ -54,12 +67,12 @@ class DictDB(): if type(data) is list: for item in data: item['_id'] = str(self.unique_key()) - self.couchdb.save_bulk(data) + ret = self.couchdb.save_bulk(data) else: data['_id'] = str(self.unique_key()) - self.couchdb.save(data) + ret = self.couchdb.save(data) - return True + return ret def get(self, key): """ -- cgit v1.1