switch apimanager to singleton, only add apiKey if it exists and move core error handling to correct place

Signed-off-by: Alexis Tyler <xo@wvvw.me>
This commit is contained in:
Alexis Tyler
2019-06-14 14:36:32 +09:30
parent 3480d33d53
commit 5917d046d7
2 changed files with 18 additions and 15 deletions
+5 -4
View File
@@ -11,7 +11,6 @@
*/
module.exports = function ($injector, glob, get, graphql, graphqlDirective, mergeGraphqlSchemas, ApiManager, log, typeDefs, resolvers, loadState, AppError) {
const apiManager = new ApiManager();
const { buildSchema } = graphql;
const { addDirectiveResolveFunctionsToSchema } = graphqlDirective;
const { mergeTypes } = mergeGraphqlSchemas;
@@ -156,17 +155,19 @@
throw new AppError('Missing apikey.');
}
if (apiManager.expired('my_servers')) {
if (ApiManager.expired('my_servers')) {
try {
apiKey = loadState('/boot/config/plugins/dynamix/dynamix.cfg').remote.apikey;
} catch (error) {
throw new AppError('My servers api key is missing, did you register your server?');
}
apiManager.add('my_servers', apiKey);
if (apiKey) {
ApiManager.add('my_servers', apiKey);
}
}
if (!apiManager.isValid('my_servers', token)) {
if (!ApiManager.isValid('my_servers', token)) {
throw new AppError('Invalid apikey.');
}
}
+13 -11
View File
@@ -64,21 +64,23 @@ am(async () => {
const core = $injector.resolve('core');
// Load core
await core.load();
await core.load().catch(coreError => {
try {
// Handler non fatal errors
$injector.resolve('globalErrorHandler')(coreError);
} catch (error) {
throw coreError;
}
});
// Load server
await core.loadServer('graphql-api');
}, error => {
try {
// Run global error handler
$injector.resolve('globalErrorHandler')(error);
} catch (error) {
// We should only end here if core has an issue loading
// We should only end here if core has an issue loading
// Log last error
console.error(error);
// Log last error
console.error(error);
// Kill applicaiton
process.exit(1);
}
// Kill applicaiton
process.exit(1);
});