Skip to content

Commit

Permalink
Refactored verifyDb
Browse files Browse the repository at this point in the history
  • Loading branch information
macterra committed Mar 20, 2024
1 parent e4ded4b commit 43dac9a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
16 changes: 16 additions & 0 deletions gatekeeper.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function writeDb(db) {

export async function verifyDb() {
const db = loadDb();
const backup = JSON.parse(JSON.stringify(db));

if (!db.anchors) {
return 0;
Expand All @@ -52,9 +53,24 @@ export async function verifyDb() {
catch (error) {
console.log(`${n} ${did} ${error}`);
invalid += 1;

if (db.anchors[did]) {
const anchor = db.anchors[did];
const registry = anchor.mdip.registry;
delete db[registry][did];
delete db.anchors[did];
}
}
}

if (invalid > 0) {
const today = new Date();
const dateString = today.toISOString().split('T')[0];
const backupName = `${dataFolder}/mdip.backup.${dateString}.json`;
fs.writeFileSync(backupName, JSON.stringify(backup, null, 4));
writeDb(db);
}

return invalid;
}

Expand Down
14 changes: 6 additions & 8 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,13 @@ const port = 3000;
app.use('/api/v1', v1router);

gatekeeper.verifyDb().then((invalid) => {
if (invalid === 0) {
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
}
else {
console.log(`${invalid} invalid DIDs in MDIP db`);
process.exit();
if (invalid > 0) {
console.log(`${invalid} invalid DIDs removed from MDIP db`);
}

app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
});

process.on('uncaughtException', (error) => {
Expand Down

0 comments on commit 43dac9a

Please sign in to comment.