Improves loading of Push Adapter, fix loading of S3Adapter

- Adds environment variables to configure S3Adapter
This commit is contained in:
Florent Vilmart
2016-03-04 10:54:32 -05:00
parent 3e6c53c3e4
commit 069605e9c3
3 changed files with 20 additions and 25 deletions

View File

@@ -28,13 +28,6 @@ export function loadAdapter(adapter, defaultAdapter, options) {
return loadAdapter(adapter.class, undefined, adapter.options); return loadAdapter(adapter.class, undefined, adapter.options);
} else if (adapter.adapter) { } else if (adapter.adapter) {
return loadAdapter(adapter.adapter, undefined, adapter.options); return loadAdapter(adapter.adapter, undefined, adapter.options);
} else {
// Try to load the defaultAdapter with the options
// The default adapter should throw if the options are
// incompatible
try {
return loadAdapter(defaultAdapter, undefined, adapter);
} catch (e) {};
} }
// return the adapter as is as it's unusable otherwise // return the adapter as is as it's unusable otherwise
return adapter; return adapter;

View File

@@ -8,19 +8,20 @@ import requiredParameter from '../../requiredParameter';
const DEFAULT_S3_REGION = "us-east-1"; const DEFAULT_S3_REGION = "us-east-1";
function parseS3AdapterOptions(...options) { function requiredOrFromEnvironment(env, name) {
if (options.length === 1 && typeof options[0] == "object") { let environmentVariable = process.env[env];
return options; if (!environmentVariable) {
requiredParameter(`S3Adapter requires an ${name}`);
} }
return environmentVariable;
const additionalOptions = options[3] || {}; }
return { function fromEnvironmentOrDefault(env, defaultValue) {
accessKey: options[0], let environmentVariable = process.env[env];
secretKey: options[1], if (environmentVariable) {
bucket: options[2], return environmentVariable;
region: additionalOptions.region
} }
return defaultValue;
} }
export class S3Adapter extends FilesAdapter { export class S3Adapter extends FilesAdapter {
@@ -28,12 +29,12 @@ export class S3Adapter extends FilesAdapter {
// Providing AWS access and secret keys is mandatory // Providing AWS access and secret keys is mandatory
// Region and bucket will use sane defaults if omitted // Region and bucket will use sane defaults if omitted
constructor( constructor(
accessKey = requiredParameter('S3Adapter requires an accessKey'), accessKey = requiredOrFromEnvironment('S3_ACCESS_KEY', 'accessKey'),
secretKey = requiredParameter('S3Adapter requires a secretKey'), secretKey = requiredOrFromEnvironment('S3_SECRET_KEY', 'secretKey'),
bucket, bucket = fromEnvironmentOrDefault('S3_BUCKET', undefined),
{ region = DEFAULT_S3_REGION, { region = fromEnvironmentOrDefault('S3_REGION', DEFAULT_S3_REGION),
bucketPrefix = '', bucketPrefix = fromEnvironmentOrDefault('S3_BUCKET_PREFIX', ''),
directAccess = false } = {}) { directAccess = fromEnvironmentOrDefault('S3_DIRECT_ACCESS', false) } = {}) {
super(); super();
this._region = region; this._region = region;

View File

@@ -133,7 +133,8 @@ function ParseServer({
const filesControllerAdapter = loadAdapter(filesAdapter, () => { const filesControllerAdapter = loadAdapter(filesAdapter, () => {
return new GridStoreAdapter(databaseURI); return new GridStoreAdapter(databaseURI);
}); });
const pushControllerAdapter = loadAdapter(push, ParsePushAdapter); // Pass the push options too as it works with the default
const pushControllerAdapter = loadAdapter(push && push.adapter, ParsePushAdapter, push);
const loggerControllerAdapter = loadAdapter(loggerAdapter, FileLoggerAdapter); const loggerControllerAdapter = loadAdapter(loggerAdapter, FileLoggerAdapter);
const emailControllerAdapter = loadAdapter(emailAdapter); const emailControllerAdapter = loadAdapter(emailAdapter);
// We pass the options and the base class for the adatper, // We pass the options and the base class for the adatper,