Skip to content

Commit

Permalink
Merge branch 'develop' into stable
Browse files Browse the repository at this point in the history
  • Loading branch information
mrzapp committed Jan 17, 2020
2 parents 3fba658 + 5e84b7d commit 8e6231a
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/Client/Service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ namespace('Service')
.add(require('./SchemaService'))
.add(require('./SettingsService'))
.add(require('./UIService'))
.add(require('./EventService'));
.add(require('../../Common/Service/EventService'));
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* A helper class for triggering and registering EVENTS
*
* @memberof HashBrown.Client.Service
* @memberof HashBrown.Common.Service
*/
class EventService {
/**
Expand Down Expand Up @@ -47,7 +47,7 @@ class EventService {
*/
static trigger(type, value) {
checkParam(type, 'type', String);

if(!this.events || !this.events[type]) { return; }

for(let id in this.events[type]) {
Expand Down
24 changes: 14 additions & 10 deletions src/Server/Controller/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class UserController extends HashBrown.Controller.ApiController {
res.status(200).cookie('token', token).send(token);

} catch(e) {
res.status(403).send(UserController.printError(e));
res.status(e.code || 403).send(e.message);

}
}
Expand All @@ -71,7 +71,7 @@ class UserController extends HashBrown.Controller.ApiController {
res.status(200).cookie('token', '').redirect('/');

} catch(e) {
res.status(403).send(UserController.printError(e));
res.status(e.code || 403).send(e.message);

}
}
Expand All @@ -90,7 +90,7 @@ class UserController extends HashBrown.Controller.ApiController {
res.status(200).send(user);

} catch(e) {
res.status(403).send(UserController.printError(e));
res.status(e.code || 403).send(e.message);

}
}
Expand All @@ -105,7 +105,7 @@ class UserController extends HashBrown.Controller.ApiController {
res.send(user.scopes);

} catch(e) {
res.status(403).send(UserController.printError(e));
res.status(e.code || 403).send(e.message);

}
}
Expand All @@ -128,7 +128,7 @@ class UserController extends HashBrown.Controller.ApiController {
res.status(200).send(users);

} catch(e) {
res.status(403).send(UserController.printError(e));
res.status(e.code || 403).send(e.message);

}
}
Expand All @@ -142,6 +142,10 @@ class UserController extends HashBrown.Controller.ApiController {
try {
let user = await HashBrown.Service.UserService.getUserById(id);

if(!user) {
throw new Error(`User by id ${id} not found`);
}

user.clearSensitiveData();

user = user.getObject();
Expand All @@ -151,7 +155,7 @@ class UserController extends HashBrown.Controller.ApiController {
res.status(200).send(user);

} catch(e) {
res.status(404).send(UserController.printError(e));
res.status(e.code || 404).send(e.message);

}
}
Expand Down Expand Up @@ -188,7 +192,7 @@ class UserController extends HashBrown.Controller.ApiController {
res.status(200).send(user);

} catch(e) {
res.status(400).send(UserController.printError(e));
res.status(e.code || 400).send(e.message);

}
}
Expand All @@ -205,7 +209,7 @@ class UserController extends HashBrown.Controller.ApiController {
res.status(200).send('OK');

} catch(e) {
res.status(502).send(UserController.printError(e));
res.status(e.code || 502).send(e.message);

}
}
Expand All @@ -230,7 +234,7 @@ class UserController extends HashBrown.Controller.ApiController {
res.status(200).cookie('token', token).send(token);

} catch(e) {
res.status(403).send(UserController.printError(e));
res.status(e.code || 403).send(e.message);

}
}
Expand All @@ -250,7 +254,7 @@ class UserController extends HashBrown.Controller.ApiController {
res.status(200).send(user);

} catch(e) {
res.status(400).send(UserController.printError(e));
res.status(e.code || 400).send(e.message);

}
}
Expand Down
52 changes: 27 additions & 25 deletions src/Server/Service/DatabaseService.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,39 @@ class DatabaseService {
* @return {Promise} Client
*/
static async connect(databaseName) {
return await MongoClient.connect(
if(this.clients && this.clients[databaseName]) { return this.clients[databaseName]; }

debug.log(`Connecting to database "${databaseName}"...`, this);

if(!this.clients) { this.clients = {}; }

this.clients[databaseName] = await MongoClient.connect(
this.getConnectionString(databaseName),
{
useNewUrlParser: true,
useUnifiedTopology: true
}
);

HashBrown.Service.EventService.on('stop', 'dbclient', () => {
if(this.clients) {
debug.log('Database connections closed', this);

for(let databaseName in this.clients) {
this.clients[databaseName].close();
}

this.clients = null;

} else {
debug.log('No database connections to close', this, true);

}
});

debug.log('...connection established', this);

return this.clients[databaseName];
}

/**
Expand Down Expand Up @@ -246,8 +272,6 @@ class DatabaseService {
collections = await client.db().listCollections().toArray();
} catch(e) {
debug.error(e, this);
} finally {
client.close();
}

return collections;
Expand All @@ -267,8 +291,6 @@ class DatabaseService {
result = await client.db('admin').admin().listDatabases();
} catch(e) {
debug.error(e, this);
} finally {
client.close();
}

if(!result) { return []; }
Expand Down Expand Up @@ -348,8 +370,6 @@ class DatabaseService {
doc = await client.db().collection(collectionName).findOne(query, projection);
} catch(e) {
debug.error(e, this);
} finally {
client.close();
}

if(doc && doc['_id']) {
Expand Down Expand Up @@ -386,8 +406,6 @@ class DatabaseService {

} catch(e) {
debug.error(e, this);
} finally {
if(client) { client.close(); }
}

return docs;
Expand All @@ -411,8 +429,6 @@ class DatabaseService {
result = await client.db().collection(collectionName).count(query);
} catch(e) {
debug.error(e, this);
} finally {
client.close();
}

return result;
Expand Down Expand Up @@ -461,8 +477,6 @@ class DatabaseService {
await client.db().collection(collectionName).updateOne(query, { $set: doc }, options || {});
} catch(e) {
debug.error(e, this);
} finally {
client.close();
}
}

Expand All @@ -488,8 +502,6 @@ class DatabaseService {
await client.db().collection(collectionName).updateMany(query, { $set: doc }, options || {});
} catch(e) {
debug.error(e, this);
} finally {
client.close();
}
}

Expand All @@ -513,8 +525,6 @@ class DatabaseService {
await client.db().collection(collectionName).insertOne(doc);
} catch(e) {
debug.error(e, this);
} finally {
client.close();
}
}

Expand All @@ -535,8 +545,6 @@ class DatabaseService {
await client.db().collection(collectionName).deleteMany(query, true);
} catch(e) {
debug.error(e, this);
} finally {
client.close();
}
}

Expand All @@ -557,8 +565,6 @@ class DatabaseService {
await client.db().collection(collectionName).removeOne(query, true);
} catch(e) {
debug.error(e, this);
} finally {
client.close();
}
}

Expand All @@ -578,8 +584,6 @@ class DatabaseService {
await client.db().dropCollection(collectionName);
} catch(e) {
debug.error(e, this);
} finally {
client.close();
}
}

Expand All @@ -598,8 +602,6 @@ class DatabaseService {
await client.db().dropDatabase();
} catch(e) {
debug.error(e, this);
} finally {
client.close();
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/Server/Service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ namespace('Service')
.add(require('./SyncService'))
.add(require('./TestService'))
.add(require('./UpdateService'))
.add(require('./UserService'));
.add(require('./UserService'))
.add(require('../../Common/Service/EventService'));

17 changes: 11 additions & 6 deletions src/Server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ global.HttpError = class HttpError extends Error {
async function main() {
// Check CLI input
await HashBrown.Service.AppService.processInput();


// Register system cleanup event
for(let signal of [ 'SIGINT', 'SIGTERM', 'SIGUSR1', 'SIGUSR2', 'uncaughtException', 'exit' ]) {
process.on(signal, () => { HashBrown.Service.EventService.trigger('stop'); });
}

// Init plugins
await HashBrown.Service.PluginService.init();

Expand All @@ -73,16 +78,16 @@ async function main() {
HashBrown.Controller[name].init(app);
}

// Start watching for file changes
if(process.env.WATCH) {
HashBrown.Service.DebugService.startWatching();
}

// Start watching schedule
HashBrown.Service.ScheduleService.startWatching();

// Start watching media cache
HashBrown.Service.MediaService.startWatchingCache();

// Start watching for file changes
if(process.env.WATCH) {
HashBrown.Service.DebugService.startWatching();
}
}

main();

0 comments on commit 8e6231a

Please sign in to comment.