Skip to content

Commit 494aa26

Browse files
committed
Initial version of the base template for construction of micro-service in TypeScript following principles of clean architecture.
1 parent 542f78b commit 494aa26

File tree

121 files changed

+7122
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

121 files changed

+7122
-0
lines changed

.env.example

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# ENVIRONMENT [development, test, production]
2+
NODE_ENV=development
3+
4+
# SERVER
5+
PORT_HTTP=80
6+
PORT_HTTPS=443
7+
8+
# DATABASE
9+
MONGODB_URI=mongodb://127.0.0.1:27017/template-base-ts
10+
MONGODB_URI_TEST=mongodb://127.0.0.1:27017/template-base-ts-test
11+
MONGODB_CON_RETRY_COUNT = 0
12+
MONGODB_CON_RETRY_INTERVAL = 1000
13+
14+
# RABBITMQ
15+
RABBITMQ_AMQP_URI=amqp://127.0.0.1:5672
16+
RABBITMQ_BROKER_NAME=template-base-ts
17+
RABBITMQ_QUEUE_NAME=task_queue
18+
RABBITMQ_CON_RETRY_COUNT = 0
19+
RABBITMQ_CON_RETRY_INTERVAL = 1000
20+
21+
#LOG
22+
LOG_DIR=logs

.gitignore

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
*.pid.lock
13+
14+
# Directory for instrumented libs generated by jscoverage/JSCover
15+
lib-cov
16+
17+
# Coverage directory used by tools like istanbul
18+
coverage
19+
20+
# nyc test coverage
21+
.nyc_output
22+
23+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24+
.grunt
25+
26+
# Bower dependency directory (https://bower.io/)
27+
bower_components
28+
29+
# node-waf configuration
30+
.lock-wscript
31+
32+
# Compiled binary addons (https://nodejs.org/api/addons.html)
33+
build/Release
34+
35+
# Dependency directories
36+
node_modules/
37+
jspm_packages/
38+
package-lock.json
39+
40+
# TypeScript v1 declaration files
41+
typings/
42+
43+
# Optional npm cache directory
44+
.npm
45+
46+
# Optional eslint cache
47+
.eslintcache
48+
49+
# Optional REPL history
50+
.node_repl_history
51+
52+
# Output of 'npm pack'
53+
*.tgz
54+
55+
# Yarn Integrity file
56+
.yarn-integrity
57+
58+
# dotenv environment variables file
59+
.env
60+
61+
# next.js build output
62+
.next
63+
64+
# IDE
65+
.idea
66+
67+
# Directory files dist
68+
dist
69+
70+
# Docs
71+
docs

README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Template Base TypeScript
2+
[![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/)
3+
--
4+
Basic model for implementation of micro-services in typewritten text following the principles of clean architecture (proposed by Robert C. Martin), decoupled and testable.
5+
6+
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.
7+
8+
Just clone the project and start coding it :)
9+
10+
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/
11+
12+
#### Top Libraries Used
13+
- [**amqp-ts**](https://github.com/abreits/amqp-ts) - Simplifies communication with RabbitMQ. As for example: connection/reconnection, queue consumption, publication and more.
14+
- [**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.
15+
- [**Express.js**](https://expressjs.com) - Framework for Node.js. Minimalist, flexible and contains a robust set of features to develop web applications.
16+
- [**Greenlock Express.js**](https://github.com/Daplie/greenlock-express) - Automates the generation of HTTPS certificates issued by Let's Encrypt v2 via ACME.
17+
- [**Gulp.js**](https://gulpjs.com/) - Toolkit to automate tasks. As for example, do the transpiler of the TypeScript code for ECMA.
18+
- [**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.
19+
- [**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...).
20+
- [**Mocha**](https://mochajs.org/) - Resource-rich JavaScript test framework, making asynchronous testing simple and fun. Responsible for running the tests.
21+
- [**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.
22+
- [**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.
23+
- [**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.
24+
- [**Sinon.JS**](https://sinonjs.org/) - Library for creating spies, stubs and independent test simulations for JavaScript. Works with any unit test framework.
25+
- [**sinon-mongoose**](https://www.npmjs.com/package/sinon-mongoose) - Extends the Sinon stubs to Mongoose methods, so you can easily test chained methods.
26+
- [**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).
27+
- [**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.
28+
- [**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.
29+
- [**TSNode**](https://github.com/TypeStrong/ts-node) - Execution of TypeScript and REPL for node.js
30+
- [**TypeDoc**](https://typedoc.org/) - Documentation generator for TypeScript projects.
31+
- [**TypeScript**](https://www.typescriptlang.org/) - JavaScript superset developed by Microsoft that adds typing, OO features and many other language.
32+
- [**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.
33+
34+
## Installation and Development server
35+
Requires [Node.js](https://nodejs.org/) v6+ and [MongoDB](https://www.mongodb.com) to run.
36+
Install the dependencies, start the local MongoDB, and start the server.
37+
```sh
38+
$ npm install
39+
$ mongod
40+
$ npm run start:dev
41+
```
42+
Navigate to `http://127.0.0.1/api/v1`.
43+
44+
## Build
45+
- Run `npm run build` to build the project. The build artifacts will be stored in the `dist/` directory.
46+
47+
## Run Server
48+
- Run `npm start` to run the project in production mode.
49+
- Run `npm run start:dev` to run the project in development mode.
50+
51+
52+
## Running unit tests
53+
- Run `npm run test:unit` to run unit tests by [Mocha](https://mochajs.org/).
54+
55+
## Running integration tests
56+
- Run `mongod`
57+
- Run `npm run test:integration` to run integration tests by [Mocha](https://mochajs.org/).
58+
59+
## Running test coverage
60+
- Run `npm run test:cov` to run code coverage tests by [Instanbul](https://istanbul.js.org/).
61+
62+
## Running all tests
63+
- Run `mongod`
64+
- Run `npm run test` to run unit testing, integration and coverage by [Mocha](https://mochajs.org/) and [Instanbul](https://istanbul.js.org/).
65+
66+
## Generating code documentation
67+
- Run `npm run build:doc` the html documentation will be generated in the /docs directory by [typedoc](https://typedoc.org/).
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# ENVIRONMENT [development, test, production]
2+
NODE_ENV=development
3+
4+
# SERVER
5+
PORT_HTTP=80
6+
PORT_HTTPS=443
7+
8+
# DATABASE
9+
MONGODB_URI=mongodb://127.0.0.1:27017/activity-tracking
10+
MONGODB_URI_TEST=mongodb://127.0.0.1:27017/activity-tracking-test
11+
MONGODB_CON_RETRY_COUNT = 0
12+
MONGODB_CON_RETRY_INTERVAL = 1000
13+
14+
# RABBITMQ
15+
RABBITMQ_AMQP_URI=amqp://127.0.0.1:5672
16+
RABBITMQ_BROKER_NAME=activity-tracking-service
17+
RABBITMQ_QUEUE_NAME=task_queue
18+
RABBITMQ_CON_RETRY_COUNT = 0
19+
RABBITMQ_CON_RETRY_INTERVAL = 1000
20+
21+
#LOG
22+
LOG_DIR=logs
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
*.pid.lock
13+
14+
# Directory for instrumented libs generated by jscoverage/JSCover
15+
lib-cov
16+
17+
# Coverage directory used by tools like istanbul
18+
coverage
19+
20+
# nyc test coverage
21+
.nyc_output
22+
23+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24+
.grunt
25+
26+
# Bower dependency directory (https://bower.io/)
27+
bower_components
28+
29+
# node-waf configuration
30+
.lock-wscript
31+
32+
# Compiled binary addons (https://nodejs.org/api/addons.html)
33+
build/Release
34+
35+
# Dependency directories
36+
node_modules/
37+
jspm_packages/
38+
package-lock.json
39+
40+
# TypeScript v1 declaration files
41+
typings/
42+
43+
# Optional npm cache directory
44+
.npm
45+
46+
# Optional eslint cache
47+
.eslintcache
48+
49+
# Optional REPL history
50+
.node_repl_history
51+
52+
# Output of 'npm pack'
53+
*.tgz
54+
55+
# Yarn Integrity file
56+
.yarn-integrity
57+
58+
# dotenv environment variables file
59+
.env
60+
61+
# next.js build output
62+
.next
63+
64+
# IDE
65+
.idea
66+
67+
# Directory files dist
68+
dist
69+
70+
# Docs
71+
docs
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Template Base TypeScript
2+
[![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/)
3+
--
4+
Basic model for implementation of micro-services in typewritten text following the principles of clean architecture (proposed by Robert C. Martin), decoupled and testable.
5+
6+
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.
7+
8+
Just clone the project and start coding it :)
9+
10+
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/
11+
12+
#### Top Libraries Used
13+
- [**amqp-ts**](https://github.com/abreits/amqp-ts) - Simplifies communication with RabbitMQ. As for example: connection/reconnection, queue consumption, publication and more.
14+
- [**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.
15+
- [**Express.js**](https://expressjs.com) - Framework for Node.js. Minimalist, flexible and contains a robust set of features to develop web applications.
16+
- [**Greenlock Express.js**](https://github.com/Daplie/greenlock-express) - Automates the generation of HTTPS certificates issued by Let's Encrypt v2 via ACME.
17+
- [**Gulp.js**](https://gulpjs.com/) - Toolkit to automate tasks. As for example, do the transpiler of the TypeScript code for ECMA.
18+
- [**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.
19+
- [**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...).
20+
- [**Mocha**](https://mochajs.org/) - Resource-rich JavaScript test framework, making asynchronous testing simple and fun. Responsible for running the tests.
21+
- [**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.
22+
- [**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.
23+
- [**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.
24+
- [**Sinon.JS**](https://sinonjs.org/) - Library for creating spies, stubs and independent test simulations for JavaScript. Works with any unit test framework.
25+
- [**sinon-mongoose**](https://www.npmjs.com/package/sinon-mongoose) - Extends the Sinon stubs to Mongoose methods, so you can easily test chained methods.
26+
- [**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).
27+
- [**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.
28+
- [**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.
29+
- [**TSNode**](https://github.com/TypeStrong/ts-node) - Execution of TypeScript and REPL for node.js
30+
- [**TypeDoc**](https://typedoc.org/) - Documentation generator for TypeScript projects.
31+
- [**TypeScript**](https://www.typescriptlang.org/) - JavaScript superset developed by Microsoft that adds typing, OO features and many other language.
32+
- [**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.
33+
34+
## Installation and Development server
35+
Requires [Node.js](https://nodejs.org/) v6+ and [MongoDB](https://www.mongodb.com) to run.
36+
Install the dependencies, start the local MongoDB, and start the server.
37+
```sh
38+
$ npm install
39+
$ mongod
40+
$ npm run start:dev
41+
```
42+
Navigate to `http://127.0.0.1/api/v1`.
43+
44+
## Build
45+
- Run `npm run build` to build the project. The build artifacts will be stored in the `dist/` directory.
46+
47+
## Run Server
48+
- Run `npm start` to run the project in production mode.
49+
- Run `npm run start:dev` to run the project in development mode.
50+
51+
52+
## Running unit tests
53+
- Run `npm run test:unit` to run unit tests by [Mocha](https://mochajs.org/).
54+
55+
## Running integration tests
56+
- Run `mongod`
57+
- Run `npm run test:integration` to run integration tests by [Mocha](https://mochajs.org/).
58+
59+
## Running test coverage
60+
- Run `npm run test:cov` to run code coverage tests by [Instanbul](https://istanbul.js.org/).
61+
62+
## Running all tests
63+
- Run `mongod`
64+
- Run `npm run test` to run unit testing, integration and coverage by [Mocha](https://mochajs.org/) and [Instanbul](https://istanbul.js.org/).
65+
66+
## Generating code documentation
67+
- Run `npm run build:doc` the html documentation will be generated in the /docs directory by [typedoc](https://typedoc.org/).
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict'
2+
3+
// DEPENDENCIES
4+
const gulp = require('gulp'),
5+
tslint = require('gulp-tslint'),
6+
ts = require('gulp-typescript'),
7+
nodemon = require('gulp-nodemon')
8+
9+
// TSLIST
10+
gulp.task('lint', () => {
11+
const config = {formatter: 'verbose'}
12+
return gulp.src(['src/**/*.ts'])
13+
.pipe(tslint(config))
14+
.pipe(tslint.report({
15+
reportLimit: 5
16+
}))
17+
})
18+
19+
// BUILD
20+
gulp.task('build:ts', ['copy-files'], () => {
21+
const tsProject = ts.createProject('tsconfig.json', {typescript: require('typescript')})
22+
return tsProject.src()
23+
.pipe(tsProject())
24+
.on('error', (err) => {
25+
console.log('build error:', err.message)
26+
process.exit(1)
27+
})
28+
.js.pipe(gulp.dest('dist/'))
29+
})
30+
31+
// COPY FILES
32+
gulp.task('copy-files', ['copy-yaml'], () => {
33+
const COPY_FILES = ['package.json']
34+
return gulp.src(COPY_FILES)
35+
.pipe(gulp.dest('dist'))
36+
})
37+
38+
gulp.task('copy-yaml', () => {
39+
const COPY_YAML = ['src/ui/swagger/*.yaml']
40+
return gulp.src(COPY_YAML)
41+
.pipe(gulp.dest('dist/src/ui/swagger'))
42+
})
43+
44+
// WATCH
45+
gulp.task('watch', () => {
46+
gulp.watch(['./**/*.ts', './src/utils/swagger/*.yaml', '.env'], ['build'])
47+
})
48+
49+
// BUILD DEFAULT
50+
gulp.task('build', ['lint', 'build:ts'])
51+
52+
// BUILD DEV
53+
gulp.task('dev', ['build', 'watch'], () => {
54+
return nodemon({
55+
script: 'dist/server.js',
56+
watch: 'dist/server.js',
57+
ignore: ['node_modules/']
58+
}).on('restart', () => {
59+
console.log('restarted!')
60+
})
61+
})

0 commit comments

Comments
 (0)