Skip to content

Commit

Permalink
Test app is rewritten on typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-melnik-cv committed Jul 16, 2018
1 parent aae6d92 commit a880981
Show file tree
Hide file tree
Showing 33 changed files with 770 additions and 505 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
node_modules
package-lock.json

#Configuration
config/

# Logs
npm-debug.log

# Credentials
credentials/

# idea dependencies
.idea
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# aws-learning
Playing around with Amazon Web Services
37 changes: 37 additions & 0 deletions Server.ts
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);
});
42 changes: 33 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"scripts": {
"test:integration": "NODE_ENV=test mocha --opts ./test/options/integration.opts"
"start": "npm run build:live",
"build:live": "nodemon --exec ./node_modules/.bin/ts-node -- ./Server.ts",
"lint:server": "tslint -c ./tslint.json './src/**/*.ts'",
"test:integration": "./node_modules/.bin/mocha --compilers ts:ts-node/register -- --recursive ./test/**/*.spec.ts"
},
"dependencies": {
"aws-sdk": ">= 2.0.9",
Expand All @@ -10,15 +13,36 @@
"chai": "^4.1.2",
"chai-http": "^3.0.0",
"cors": "^2.8.4",
"express": "^4.16.2",
"express-validator": "^4.3.0",
"fb": "^2.0.0",
"lodash": "^4.17.4",
"mocha": "^4.0.1",
"mongoose": "^4.13.6",
"express": "^4.16.3",
"is-my-json-valid": "^2.17.2",
"log4js": "^2.10.0",
"mocha": "^5.2.0",
"mongoose": "^5.2.1",
"morgan": "^1.9.0",
"multer": "^1.3.0",
"node-uuid": ">= 1.4.1",
"nodemon": "^1.12.5",
"nconf": "^0.10.0",
"request-promise": "^4.2.2",
"uuid": "^3.1.0"
},
"devDependencies": {
"@types/bluebird": "^3.5.21",
"@types/body-parser": "^1.17.0",
"@types/busboy": "^0.2.3",
"@types/chai": "^4.1.4",
"@types/cors": "^2.8.4",
"@types/lodash": "^4.14.110",
"@types/mocha": "^5.2.4",
"@types/mongoose": "^5.2.0",
"@types/morgan": "^1.7.35",
"@types/nconf": "0.0.37",
"@types/node": "^10.5.2",
"@types/request": "^2.47.1",
"@types/request-promise": "^4.1.42",
"@types/superagent": "^3.8.2",
"@types/uuid": "^3.4.3",
"nodemon": "^1.17.5",
"ts-node": "^7.0.0",
"tslint": "^5.10.0",
"typescript": "^2.9.2"
}
}
28 changes: 0 additions & 28 deletions server.js

This file was deleted.

24 changes: 0 additions & 24 deletions src/app.js

This file was deleted.

32 changes: 32 additions & 0 deletions src/app.ts
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;
106 changes: 106 additions & 0 deletions src/clients/AWSClient.ts
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());
22 changes: 22 additions & 0 deletions src/configuration.ts
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;
Loading

0 comments on commit a880981

Please sign in to comment.