Skip to content

Commit

Permalink
Initial version of the base template for construction of micro-servic…
Browse files Browse the repository at this point in the history
…e in TypeScript following principles of clean architecture.
  • Loading branch information
douglasrafael committed Nov 7, 2018
1 parent 542f78b commit 494aa26
Show file tree
Hide file tree
Showing 121 changed files with 7,122 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# ENVIRONMENT [development, test, production]
NODE_ENV=development

# SERVER
PORT_HTTP=80
PORT_HTTPS=443

# DATABASE
MONGODB_URI=mongodb://127.0.0.1:27017/template-base-ts
MONGODB_URI_TEST=mongodb://127.0.0.1:27017/template-base-ts-test
MONGODB_CON_RETRY_COUNT = 0
MONGODB_CON_RETRY_INTERVAL = 1000

# RABBITMQ
RABBITMQ_AMQP_URI=amqp://127.0.0.1:5672
RABBITMQ_BROKER_NAME=template-base-ts
RABBITMQ_QUEUE_NAME=task_queue
RABBITMQ_CON_RETRY_COUNT = 0
RABBITMQ_CON_RETRY_INTERVAL = 1000

#LOG
LOG_DIR=logs
71 changes: 71 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/
package-lock.json

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# next.js build output
.next

# IDE
.idea

# Directory files dist
dist

# Docs
docs
67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Template Base TypeScript
[![license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/LIBE-NUTES/template-base-ts/blob/master/LICENSE) [![node](https://img.shields.io/badge/node-v11.1.0-red.svg)](https://nodejs.org/) [![npm](https://img.shields.io/badge/npm-v6.4.1-red.svg)](https://nodejs.org/) [![Swagger](https://img.shields.io/badge/swagger-v3.0-green.svg?longCache=true&style=flat)](https://swagger.io/) [![TypeScript](https://badges.frapsoft.com/typescript/love/typescript.png?v=101)](https://www.typescriptlang.org/)
--
Basic model for implementation of micro-services in typewritten text following the principles of clean architecture (proposed by Robert C. Martin), decoupled and testable.

This project follows with an implementation example containing integration with Swagger for API design, MongoDB for data storage and RabbitMQ as a messaging channel for information exchange between micro-services.

Just clone the project and start coding it :)

See the [example](https://github.com/LIBE-NUTES/template-base-ts/tree/master/example) of a micro-service to accompany physical activity using this model and be happy. (0/

#### Top Libraries Used
- [**amqp-ts**](https://github.com/abreits/amqp-ts) - Simplifies communication with RabbitMQ. As for example: connection/reconnection, queue consumption, publication and more.
- [**Chai.js**](https://www.chaijs.com/) - Assertion library that has several interfaces that allow the developer to choose the style that makes it more comfortable (BDD/TDD). It can be easily combined with any test framework for javascript code.
- [**Express.js**](https://expressjs.com) - Framework for Node.js. Minimalist, flexible and contains a robust set of features to develop web applications.
- [**Greenlock Express.js**](https://github.com/Daplie/greenlock-express) - Automates the generation of HTTPS certificates issued by Let's Encrypt v2 via ACME.
- [**Gulp.js**](https://gulpjs.com/) - Toolkit to automate tasks. As for example, do the transpiler of the TypeScript code for ECMA.
- [**InversifyJS**](http://inversify.io/) - Control inversion library (IoC) for TypeScript and JavaScript applications. An IoC container uses a class constructor to identify and inject its dependencies. It has a friendly API and encourages the use of OOP and IoC best practices.
- [**inversify-express-utils**](https://github.com/inversify/inversify-express-utils) - Provides utilities for developing applications on express.js with InversifyJS. For example, by annotating a class as controller (defining routes). Likewise, decorate class methods to serve as HTTP request handlers (GET, POST...).
- [**Mocha**](https://mochajs.org/) - Resource-rich JavaScript test framework, making asynchronous testing simple and fun. Responsible for running the tests.
- [**Mongoose**](https://mongoosejs.com/) - Provides a straightforward, schema-based solution to model your application's data (MongoDB). It includes built-in type conversion, validation, query creation, business logic hooks and more.
- [**nyc**](https://github.com/istanbuljs/nyc) - Command Line Interface for [Istanbul](https://istanbul.js.org/) (Code Coverage), with support for: applications that generate subprocesses, ES6/ES2015 using babel-plugin-istanbul. collection of reporters, providing terminal output and HTML.
- [**query-strings-parser**](https://www.npmjs.com/package/query-strings-parser) - Middleware for query string handling. Performs the transformation of the query string into a format that is compatible with the MongoDB database.
- [**Sinon.JS**](https://sinonjs.org/) - Library for creating spies, stubs and independent test simulations for JavaScript. Works with any unit test framework.
- [**sinon-mongoose**](https://www.npmjs.com/package/sinon-mongoose) - Extends the Sinon stubs to Mongoose methods, so you can easily test chained methods.
- [**SuperTest**](https://github.com/visionmedia/supertest) - Provides high-level abstraction for HTTP tests, while still allowing you to descend to the lower-level API provided by the [SuperAgent](https://github.com/visionmedia/superagent) (HTTP Request Library).
- [**Swagger UI Express**](https://www.npmjs.com/package/swagger-ui-express) - Application middleware in express.js to route the Swagger user interface to your Swagger document (.yaml or .json). This acts as a documentation of your hosted API in your application.
- [**TSLint**](https://palantir.github.io/tslint/) - Extensible static analysis tool that checks the TypeScript code for readability, maintainability and functionality errors. It is widely supported by modern editors and building systems and can be customized with its own rules, settings and formatters.
- [**TSNode**](https://github.com/TypeStrong/ts-node) - Execution of TypeScript and REPL for node.js
- [**TypeDoc**](https://typedoc.org/) - Documentation generator for TypeScript projects.
- [**TypeScript**](https://www.typescriptlang.org/) - JavaScript superset developed by Microsoft that adds typing, OO features and many other language.
- [**winston**](https://github.com/winstonjs/winston) - Simple and universal log library with support for various transports. A transport is essentially a storage device for your logs. Each instance of a winston logger can have multiple transports configured at different levels. For example, you might want error logs to be stored in a persistent remote location (such as a database), but all logs are sent to the console or to a local file.

## Installation and Development server
Requires [Node.js](https://nodejs.org/) v6+ and [MongoDB](https://www.mongodb.com) to run.
Install the dependencies, start the local MongoDB, and start the server.
```sh
$ npm install
$ mongod
$ npm run start:dev
```
Navigate to `http://127.0.0.1/api/v1`.

## Build
- Run `npm run build` to build the project. The build artifacts will be stored in the `dist/` directory.

## Run Server
- Run `npm start` to run the project in production mode.
- Run `npm run start:dev` to run the project in development mode.


## Running unit tests
- Run `npm run test:unit` to run unit tests by [Mocha](https://mochajs.org/).

## Running integration tests
- Run `mongod`
- Run `npm run test:integration` to run integration tests by [Mocha](https://mochajs.org/).

## Running test coverage
- Run `npm run test:cov` to run code coverage tests by [Instanbul](https://istanbul.js.org/).

## Running all tests
- Run `mongod`
- Run `npm run test` to run unit testing, integration and coverage by [Mocha](https://mochajs.org/) and [Instanbul](https://istanbul.js.org/).

## Generating code documentation
- Run `npm run build:doc` the html documentation will be generated in the /docs directory by [typedoc](https://typedoc.org/).
22 changes: 22 additions & 0 deletions example/activity-tracking/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# ENVIRONMENT [development, test, production]
NODE_ENV=development

# SERVER
PORT_HTTP=80
PORT_HTTPS=443

# DATABASE
MONGODB_URI=mongodb://127.0.0.1:27017/activity-tracking
MONGODB_URI_TEST=mongodb://127.0.0.1:27017/activity-tracking-test
MONGODB_CON_RETRY_COUNT = 0
MONGODB_CON_RETRY_INTERVAL = 1000

# RABBITMQ
RABBITMQ_AMQP_URI=amqp://127.0.0.1:5672
RABBITMQ_BROKER_NAME=activity-tracking-service
RABBITMQ_QUEUE_NAME=task_queue
RABBITMQ_CON_RETRY_COUNT = 0
RABBITMQ_CON_RETRY_INTERVAL = 1000

#LOG
LOG_DIR=logs
71 changes: 71 additions & 0 deletions example/activity-tracking/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/
package-lock.json

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# next.js build output
.next

# IDE
.idea

# Directory files dist
dist

# Docs
docs
67 changes: 67 additions & 0 deletions example/activity-tracking/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Template Base TypeScript
[![license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/LIBE-NUTES/template-base-ts/blob/master/LICENSE) [![node](https://img.shields.io/badge/node-v11.1.0-red.svg)](https://nodejs.org/) [![npm](https://img.shields.io/badge/npm-v6.4.1-red.svg)](https://nodejs.org/) [![Swagger](https://img.shields.io/badge/swagger-v3.0-green.svg?longCache=true&style=flat)](https://swagger.io/) [![TypeScript](https://badges.frapsoft.com/typescript/love/typescript.png?v=101)](https://www.typescriptlang.org/)
--
Basic model for implementation of micro-services in typewritten text following the principles of clean architecture (proposed by Robert C. Martin), decoupled and testable.

This project follows with an implementation example containing integration with Swagger for API design, MongoDB for data storage and RabbitMQ as a messaging channel for information exchange between micro-services.

Just clone the project and start coding it :)

See the [example](https://github.com/LIBE-NUTES/template-base-ts/tree/master/example) of a micro-service to accompany physical activity using this model and be happy. (0/

#### Top Libraries Used
- [**amqp-ts**](https://github.com/abreits/amqp-ts) - Simplifies communication with RabbitMQ. As for example: connection/reconnection, queue consumption, publication and more.
- [**Chai.js**](https://www.chaijs.com/) - Assertion library that has several interfaces that allow the developer to choose the style that makes it more comfortable (BDD/TDD). It can be easily combined with any test framework for javascript code.
- [**Express.js**](https://expressjs.com) - Framework for Node.js. Minimalist, flexible and contains a robust set of features to develop web applications.
- [**Greenlock Express.js**](https://github.com/Daplie/greenlock-express) - Automates the generation of HTTPS certificates issued by Let's Encrypt v2 via ACME.
- [**Gulp.js**](https://gulpjs.com/) - Toolkit to automate tasks. As for example, do the transpiler of the TypeScript code for ECMA.
- [**InversifyJS**](http://inversify.io/) - Control inversion library (IoC) for TypeScript and JavaScript applications. An IoC container uses a class constructor to identify and inject its dependencies. It has a friendly API and encourages the use of OOP and IoC best practices.
- [**inversify-express-utils**](https://github.com/inversify/inversify-express-utils) - Provides utilities for developing applications on express.js with InversifyJS. For example, by annotating a class as controller (defining routes). Likewise, decorate class methods to serve as HTTP request handlers (GET, POST...).
- [**Mocha**](https://mochajs.org/) - Resource-rich JavaScript test framework, making asynchronous testing simple and fun. Responsible for running the tests.
- [**Mongoose**](https://mongoosejs.com/) - Provides a straightforward, schema-based solution to model your application's data (MongoDB). It includes built-in type conversion, validation, query creation, business logic hooks and more.
- [**nyc**](https://github.com/istanbuljs/nyc) - Command Line Interface for [Istanbul](https://istanbul.js.org/) (Code Coverage), with support for: applications that generate subprocesses, ES6/ES2015 using babel-plugin-istanbul. collection of reporters, providing terminal output and HTML.
- [**query-strings-parser**](https://www.npmjs.com/package/query-strings-parser) - Middleware for query string handling. Performs the transformation of the query string into a format that is compatible with the MongoDB database.
- [**Sinon.JS**](https://sinonjs.org/) - Library for creating spies, stubs and independent test simulations for JavaScript. Works with any unit test framework.
- [**sinon-mongoose**](https://www.npmjs.com/package/sinon-mongoose) - Extends the Sinon stubs to Mongoose methods, so you can easily test chained methods.
- [**SuperTest**](https://github.com/visionmedia/supertest) - Provides high-level abstraction for HTTP tests, while still allowing you to descend to the lower-level API provided by the [SuperAgent](https://github.com/visionmedia/superagent) (HTTP Request Library).
- [**Swagger UI Express**](https://www.npmjs.com/package/swagger-ui-express) - Application middleware in express.js to route the Swagger user interface to your Swagger document (.yaml or .json). This acts as a documentation of your hosted API in your application.
- [**TSLint**](https://palantir.github.io/tslint/) - Extensible static analysis tool that checks the TypeScript code for readability, maintainability and functionality errors. It is widely supported by modern editors and building systems and can be customized with its own rules, settings and formatters.
- [**TSNode**](https://github.com/TypeStrong/ts-node) - Execution of TypeScript and REPL for node.js
- [**TypeDoc**](https://typedoc.org/) - Documentation generator for TypeScript projects.
- [**TypeScript**](https://www.typescriptlang.org/) - JavaScript superset developed by Microsoft that adds typing, OO features and many other language.
- [**winston**](https://github.com/winstonjs/winston) - Simple and universal log library with support for various transports. A transport is essentially a storage device for your logs. Each instance of a winston logger can have multiple transports configured at different levels. For example, you might want error logs to be stored in a persistent remote location (such as a database), but all logs are sent to the console or to a local file.

## Installation and Development server
Requires [Node.js](https://nodejs.org/) v6+ and [MongoDB](https://www.mongodb.com) to run.
Install the dependencies, start the local MongoDB, and start the server.
```sh
$ npm install
$ mongod
$ npm run start:dev
```
Navigate to `http://127.0.0.1/api/v1`.

## Build
- Run `npm run build` to build the project. The build artifacts will be stored in the `dist/` directory.

## Run Server
- Run `npm start` to run the project in production mode.
- Run `npm run start:dev` to run the project in development mode.


## Running unit tests
- Run `npm run test:unit` to run unit tests by [Mocha](https://mochajs.org/).

## Running integration tests
- Run `mongod`
- Run `npm run test:integration` to run integration tests by [Mocha](https://mochajs.org/).

## Running test coverage
- Run `npm run test:cov` to run code coverage tests by [Instanbul](https://istanbul.js.org/).

## Running all tests
- Run `mongod`
- Run `npm run test` to run unit testing, integration and coverage by [Mocha](https://mochajs.org/) and [Instanbul](https://istanbul.js.org/).

## Generating code documentation
- Run `npm run build:doc` the html documentation will be generated in the /docs directory by [typedoc](https://typedoc.org/).
61 changes: 61 additions & 0 deletions example/activity-tracking/gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict'

// DEPENDENCIES
const gulp = require('gulp'),
tslint = require('gulp-tslint'),
ts = require('gulp-typescript'),
nodemon = require('gulp-nodemon')

// TSLIST
gulp.task('lint', () => {
const config = {formatter: 'verbose'}
return gulp.src(['src/**/*.ts'])
.pipe(tslint(config))
.pipe(tslint.report({
reportLimit: 5
}))
})

// BUILD
gulp.task('build:ts', ['copy-files'], () => {
const tsProject = ts.createProject('tsconfig.json', {typescript: require('typescript')})
return tsProject.src()
.pipe(tsProject())
.on('error', (err) => {
console.log('build error:', err.message)
process.exit(1)
})
.js.pipe(gulp.dest('dist/'))
})

// COPY FILES
gulp.task('copy-files', ['copy-yaml'], () => {
const COPY_FILES = ['package.json']
return gulp.src(COPY_FILES)
.pipe(gulp.dest('dist'))
})

gulp.task('copy-yaml', () => {
const COPY_YAML = ['src/ui/swagger/*.yaml']
return gulp.src(COPY_YAML)
.pipe(gulp.dest('dist/src/ui/swagger'))
})

// WATCH
gulp.task('watch', () => {
gulp.watch(['./**/*.ts', './src/utils/swagger/*.yaml', '.env'], ['build'])
})

// BUILD DEFAULT
gulp.task('build', ['lint', 'build:ts'])

// BUILD DEV
gulp.task('dev', ['build', 'watch'], () => {
return nodemon({
script: 'dist/server.js',
watch: 'dist/server.js',
ignore: ['node_modules/']
}).on('restart', () => {
console.log('restarted!')
})
})
Loading

0 comments on commit 494aa26

Please sign in to comment.