Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Commit

Permalink
init: remove bad connections at start up
Browse files Browse the repository at this point in the history
  • Loading branch information
n-riesco committed Feb 23, 2018
1 parent cbc7113 commit f3df19f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 12 deletions.
25 changes: 25 additions & 0 deletions backend/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Logger from './logger';

import {
deleteAllConnections,
deleteBadConnections
} from './persistent/Connections.js';
import {getSetting} from './settings.js';

const setCSVStorageSize = require('./persistent/datastores/csv.js').setStorageSize;

export default function init() {
try {
deleteBadConnections();
} catch (error) {
Logger.log(`Failed to delete bad connections: ${error.message}`);
deleteAllConnections();
}

try {
setCSVStorageSize(getSetting('CSV_STORAGE_SIZE'));
} catch (error) {
Logger.log(`Failed to get setting CSV_STORAGE_SIZE: ${error.message}`);
setCSVStorageSize(0);
}
}
24 changes: 23 additions & 1 deletion backend/persistent/Connections.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {assoc, dissoc, findIndex} from 'ramda';
import uuid from 'uuid';
import YAML from 'yamljs';
import * as Datastores from './datastores/Datastores.js';
import {DIALECTS} from '../../app/constants/constants.js';

import {getSetting} from '../settings';

Expand Down Expand Up @@ -39,11 +40,32 @@ export function deleteConnectionById(id) {
const connections = getConnections();
const index = findIndex(connection => connection.id === id, connections);
if (index > -1) {
Datastores.disconnect(connections[index]);
connections.splice(index, 1);
fs.writeFileSync(getSetting('CONNECTIONS_PATH'), YAML.stringify(connections, 4));
}
}

export function deleteBadConnections() {
getConnections().forEach(connection => {
const {id, dialect} = connection;

const dialects = Object.getOwnPropertyNames(DIALECTS).map(k => DIALECTS[k]);

const isUnknownDialect = (dialects.indexOf(dialect) === -1);
if (isUnknownDialect) {
deleteConnectionById(id);
}
});
}

export function deleteAllConnections() {
if (!fs.existsSync(getSetting('STORAGE_PATH'))) {
createStoragePath();
}
fs.writeFileSync(getSetting('CONNECTIONS_PATH'), YAML.stringify([], 4));
}

export function getSanitizedConnections() {
const connections = getConnections();
return connections.map(cred => sanitize(cred));
Expand All @@ -60,7 +82,7 @@ export function saveConnection(connectionObject) {
return connectionId;
}

export function validateConnection (connectionObject) {
export function validateConnection(connectionObject) {
return Datastores.connect(connectionObject).then(() => {
return {};
}).catch(err => {
Expand Down
17 changes: 6 additions & 11 deletions backend/routes.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var restify = require('restify');
var CookieParser = require('restify-cookies');
const restify = require('restify');
const CookieParser = require('restify-cookies');
const fetch = require('node-fetch');

import * as fs from 'fs';
import path from 'path';

import * as Datastores from './persistent/datastores/Datastores.js';
const setCSVStorageSize = require('./persistent/datastores/csv.js').setStorageSize;

import {PlotlyOAuth} from './plugins/authorization.js';
import {getQueries, getQuery, deleteQuery} from './persistent/Queries';
import {
Expand All @@ -27,7 +27,7 @@ import {checkWritePermissions, newDatacache} from './persistent/PlotlyAPI.js';
import {contains, keys, isEmpty, merge, pluck} from 'ramda';
import {getCerts, timeoutFetchAndSaveCerts, setRenewalJob} from './certificates';
import Logger from './logger';
import fetch from 'node-fetch';
import init from './init.js';

export default class Servers {
/*
Expand All @@ -36,12 +36,7 @@ export default class Servers {
* The httpsServer starts when certificates have been created.
*/
constructor(args = {createCerts: true, startHttps: true, isElectron: false}) {
try {
setCSVStorageSize(getSetting('CSV_STORAGE_SIZE'));
} catch (error) {
Logger.log(`Failed to get setting CSV_STORAGE_SIZE: ${error.message}`);
setCSVStorageSize(0);
}
init();

this.httpServer = {
port: null,
Expand Down

0 comments on commit f3df19f

Please sign in to comment.