1 | Enterprise Application Stack
|
2 | =================================
|
3 |
|
4 | ### What is this?
|
5 | - Application Stack for Enterprise Applications
|
6 |
|
7 | ### What is this actually???
|
8 |
|
9 | - [x] Read configuration from `conf/settings.yaml`
|
10 | - [x] Merge configuration from `conf/${NODE_ENV}.yaml`, `conf/${NODE_ENV}.local.yaml` and environment variables. (if have)
|
11 | - [x] Merge configuration from `data/settings.json` (only when DATA_DIR was defined )
|
12 | - [x] Print merged configuration on application start
|
13 | - [x] Do not print sensitive configuration name with `_KEY`, `_PASSWORD`, `_SECRET` or `_TOKEN` postfix
|
14 | - [x] Auto create missing dir if the configuration name ends with `_DIR`. (e.g. LOG_DIR folder)
|
15 | - [x] Configuration with `_DIR` or `_FILE` will resolve to absolute path automatically
|
16 | - [x] Provide a `app.settings` object for application to access the final configuration
|
17 | - [x] Provide a `app.logger` object for application to use, available transporters: Console, File or Webhook
|
18 | - [x] Provide nested error object `Exception` which will concat all the stacktrace for nested errors
|
19 | - [x] When transporter itself got errors. It will automatically fallback to another available transporter
|
20 | - [x] Trap linux signals `SIGHUP`, `SIGINT`, `SIGQUIT`, `SIGTERM`
|
21 | - [x] Log nodejs process events `uncaughtException`
|
22 | - [x] Provide universal application event `beforeExit`, `exiting`, `exit`
|
23 | - [x] The original log called on failed transporter will logged using fallback logger
|
24 | - [x] Add routing utils: `router`, `route`, `param`, `middleware`
|
25 | - [x] Add container registry utils: `router`, `route`, `param`, `middleware`
|
26 |
|
27 | ### Why doing this?
|
28 |
|
29 | - Because the things above is required for most enterprise applications. It should be shared across projects.
|
30 | - It's very important. But it's not as important as application logic. It can save your time to bootstrap a new project.
|
31 | - The same structure will make DevOps happy when doing deployment/monitoring on many projects. It should be standardized.
|
32 | - Together with Agent Framework; it can be automatically inject into other classes when required. It should be easy to use.
|
33 |
|
34 | ### Installation
|
35 |
|
36 | ```bash
|
37 | npm install @e2/core --save
|
38 | ```
|
39 |
|
40 | OR
|
41 |
|
42 | ```bash
|
43 | yarn add @e2/core
|
44 | ```
|
45 |
|
46 | ### Show me the code
|
47 |
|
48 | ```js
|
49 |
|
50 | // TypeScript 2.2+
|
51 | import { Application } from '@e2/core';
|
52 |
|
53 | // Node 6+
|
54 | var Application = require('@e2/core').Application;
|
55 |
|
56 |
|
57 | const app = new Application();
|
58 |
|
59 | // enable config, logger support
|
60 | app.init(); // default, same with `app.init({ confDir: 'conf', root: process.cwd() })`
|
61 | // or
|
62 | app.init({ confDir: 'settings' }); // if your configuration folder is not 'conf'
|
63 | // or
|
64 | app.init({ confDir: 'settings', root: '/usr/share/myapp' }); // if your root folder is not current working folder
|
65 |
|
66 | console.log(app.settings);
|
67 | app.logger.info('Hi, I am agent stack'); // the log will be automatically save in `LOG_DIR` folder
|
68 |
|
69 | ```
|
70 |
|
71 | or if you prefer using factory function instead of 'new'
|
72 |
|
73 | ```js
|
74 | // ES6 or TypeScript
|
75 | import { CreateApplication } from '@e2/core';
|
76 |
|
77 | // ES5
|
78 | var CreateApplication = require('@e2/core').CreateApplication;
|
79 |
|
80 | var app = CreateApplication();
|
81 | // or
|
82 | var app = CreateApplication({ confDir: 'settings' });
|
83 | // or
|
84 | var app = CreateApplication({ confDir: 'settings', root: '/usr/share/myapp' });
|
85 |
|
86 | console.log(app.settings);
|
87 | app.logger.info('Hi, I am agent stack'); // the log will be automatically save in `LOG_DIR` folder in JSON format
|
88 |
|
89 | ```
|
90 |
|
91 | ### Others
|
92 |
|
93 | #### How do I rotate log files?
|
94 |
|
95 | Consider we output our logs to `/usr/share/myapp/logs/agent.log`
|
96 |
|
97 | We would rotate our log files with logrotate, by adding the following to `/etc/logrotate.d/myapp`:
|
98 |
|
99 | ```
|
100 | /usr/share/myapp/logs/agent.log {
|
101 | su root
|
102 | daily
|
103 | rotate 7
|
104 | delaycompress
|
105 | compress
|
106 | notifempty
|
107 | missingok
|
108 | copytruncate
|
109 | }
|
110 | ```
|