summaryrefslogtreecommitdiff
path: root/src/db.py
diff options
context:
space:
mode:
authorKristofer Hallin <kristofer@sunet.se>2021-10-29 09:29:16 +0200
committerKristofer Hallin <kristofer@sunet.se>2021-10-29 09:29:16 +0200
commit2bfbe7568a8c6477de60a676d9027dcb9714af42 (patch)
treef4da7aee25d4728659182a2cc19197f341d165d3 /src/db.py
parent34a353a539f71b6a87413b58ea483b36f94e3516 (diff)
Use FastAPI + JWT instead of Falcon.
Diffstat (limited to 'src/db.py')
-rwxr-xr-xsrc/db.py31
1 files changed, 22 insertions, 9 deletions
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):
"""