-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aae6d92
commit a880981
Showing
33 changed files
with
770 additions
and
505 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# aws-learning | ||
Playing around with Amazon Web Services |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// configuration intializer for nconf | ||
import setupConfiguration from './src/configuration'; | ||
setupConfiguration(); | ||
|
||
import http from 'http'; | ||
import log4js from 'log4js'; | ||
import mongoose from 'mongoose'; | ||
import nconf from 'nconf'; | ||
|
||
import app from './src/app'; | ||
|
||
import setupLogger from './src/setup/Logger'; | ||
import setupMongoose from './src/setup/DB'; | ||
|
||
setupLogger(); | ||
setupMongoose(); | ||
|
||
const logger = log4js.getLogger('setup:server'); | ||
|
||
const PORT = nconf.get('configuration:server:port'); | ||
|
||
const server = http.createServer(app).listen(PORT, (error: Error) => { | ||
if (error) { | ||
logger.error(`Error occurs during server start up. ${error}`); | ||
} else { | ||
logger.info(`The server is listening on port: ${PORT}`); | ||
} | ||
}); | ||
|
||
server.on('close', async () => { | ||
await mongoose.connection.close(); | ||
}); | ||
|
||
process.on('SIGINT', async () => { | ||
await mongoose.connection.close(); | ||
process.exit(0); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import bodyParser from 'body-parser'; | ||
import cors from 'cors'; | ||
import express, { Request, Response } from 'express'; | ||
|
||
import createApplicationBucket from './setup/Storage'; | ||
|
||
import Media from './controllers/Media'; | ||
|
||
createApplicationBucket(); | ||
|
||
const app = express(); | ||
|
||
app.use(cors()); | ||
app.use(bodyParser.urlencoded({ | ||
extended: false, | ||
})); | ||
app.use(bodyParser.json()); | ||
|
||
app.use(Media); | ||
|
||
app.use('/', (request: Request, response: Response) => { | ||
response.status(404).send({ | ||
errors: [ | ||
{ | ||
status: 404, | ||
details: 'Endpoint not found', | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
export default app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import AWS from 'aws-sdk'; | ||
import Bluebird from 'bluebird'; | ||
import log4js from 'log4js'; | ||
import nconf from 'nconf'; | ||
import {PromiseResult} from 'aws-sdk/lib/request'; | ||
|
||
import toLower from 'lodash/toLower'; | ||
|
||
import {singleton} from '../helpers/initializers'; | ||
|
||
const logger = log4js.getLogger('clients:AWSClient'); | ||
|
||
logger.info('AWS credentials access key id: ', nconf.get('configuration:credentials:aws:accessKeyId')); | ||
|
||
AWS.config.setPromisesDependency(Bluebird); | ||
|
||
// Setup credentials for AWS s3 storage | ||
AWS.config.update({ | ||
accessKeyId: nconf.get('configuration:credentials:aws:accessKeyId'), | ||
secretAccessKey: nconf.get('configuration:credentials:aws:secretAccessKey'), | ||
}); | ||
|
||
const escapeAndToLowerBucketName = (bucketName: string): string => { | ||
return toLower(bucketName.replace(/ /g, '-')); | ||
}; | ||
|
||
// TODO: add more methods and refact return type | ||
class AWSClient { | ||
protected client: any; | ||
|
||
constructor() { | ||
this.client = new AWS.S3(); | ||
} | ||
|
||
/** | ||
* Create new bucker in AWS s3 storage | ||
* @param {String} bucketName - name of new bucket | ||
* @return {Promise<Object>} A promise that returns createBucket method | ||
*/ | ||
public createBucket(bucketName: string): Promise<PromiseResult<string, string>> { | ||
const options = { | ||
Bucket: escapeAndToLowerBucketName(bucketName), | ||
}; | ||
logger.debug('Create bucket options: ', options); | ||
return this.client.createBucket(options).promise(); | ||
} | ||
|
||
/** | ||
* Get media file from bucker in AWS s3 storage | ||
* @param {String} bucketName - name of bucket | ||
* @param {String} fileKey - key of media file | ||
* @return {Promise<Object>} An object that returns getObject method of AWS SDK | ||
*/ | ||
public getObject(bucketName: string, fileKey: string): Promise<{}> { | ||
const options = { | ||
Bucket: escapeAndToLowerBucketName(bucketName), | ||
Key: fileKey, | ||
}; | ||
logger.debug('Get object options: ', options); | ||
return this.client.getObject(options).promise(); | ||
} | ||
|
||
/** | ||
* Add an object to a bucket | ||
* @param {Object} params - list of available params see at official documentation of AWS sdk | ||
* @return {Promise<Object>} A promise that returns putObject method | ||
*/ | ||
public put(params: { Bucket: string }): Promise<{}> { | ||
const options = { | ||
...params, | ||
Bucket: escapeAndToLowerBucketName(params.Bucket), | ||
}; | ||
logger.debug('Put object options: ', options); | ||
return this.client.putObject(options).promise(); | ||
} | ||
|
||
/** | ||
* Uploads an arbitrarily sized buffer, blob, or stream | ||
* @param {Object} params - list of available params see at official documentation of AWS sdk | ||
* @return {Promise} A promise that returns upload method | ||
*/ | ||
public upload(params: { Bucket: string }): Promise<{}> { | ||
const options = { | ||
...params, | ||
Bucket: escapeAndToLowerBucketName(params.Bucket), | ||
}; | ||
logger.debug('Upload options: ', options); | ||
return this.client.upload(options).promise(); | ||
} | ||
|
||
/** | ||
* The HEAD operation retrieves metadata from an object without returning the object itself | ||
* @param {Object} params - list of available params see at official documentation of AWS sdk | ||
* @return {Promise} A promise that returns forceDeleteBucket s3 client method | ||
*/ | ||
public head(params: { Bucket: string }) { | ||
const options = { | ||
...params, | ||
Bucket: escapeAndToLowerBucketName(params.Bucket), | ||
}; | ||
logger.debug('Head object options: ', options); | ||
return this.client.headObject(options).promise(); | ||
} | ||
} | ||
|
||
export default singleton(() => new AWSClient()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* tslint:disable no-var-requires */ | ||
|
||
import fs from 'fs'; | ||
import nconf from 'nconf'; | ||
import path from 'path'; | ||
|
||
import merge from 'lodash/merge'; | ||
|
||
const CUSTOM_CONFIG_PATH = path.join(__dirname, '..', 'config', 'configuration.ts'); | ||
|
||
const setupConfiguration = () => { | ||
const defaultConfiguration = require('./defaultConfiguration'); | ||
const customConfiguration = fs.existsSync(CUSTOM_CONFIG_PATH) | ||
? require(CUSTOM_CONFIG_PATH) | ||
: {}; | ||
nconf.use('memory'); | ||
nconf | ||
.overrides(merge({}, defaultConfiguration, customConfiguration)) | ||
.env(); | ||
}; | ||
|
||
export default setupConfiguration; |
Oops, something went wrong.