UNPKG

3.75 kBMarkdownView Raw
1Enterprise 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
37npm install @e2/core --save
38```
39
40OR
41
42```bash
43yarn add @e2/core
44```
45
46### Show me the code
47
48```js
49
50// TypeScript 2.2+
51import { Application } from '@e2/core';
52
53// Node 6+
54var Application = require('@e2/core').Application;
55
56
57const app = new Application();
58
59// enable config, logger support
60app.init(); // default, same with `app.init({ confDir: 'conf', root: process.cwd() })`
61// or
62app.init({ confDir: 'settings' }); // if your configuration folder is not 'conf'
63// or
64app.init({ confDir: 'settings', root: '/usr/share/myapp' }); // if your root folder is not current working folder
65
66console.log(app.settings);
67app.logger.info('Hi, I am agent stack'); // the log will be automatically save in `LOG_DIR` folder
68
69```
70
71or if you prefer using factory function instead of 'new'
72
73```js
74// ES6 or TypeScript
75import { CreateApplication } from '@e2/core';
76
77// ES5
78var CreateApplication = require('@e2/core').CreateApplication;
79
80var app = CreateApplication();
81// or
82var app = CreateApplication({ confDir: 'settings' });
83// or
84var app = CreateApplication({ confDir: 'settings', root: '/usr/share/myapp' });
85
86console.log(app.settings);
87app.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
95Consider we output our logs to `/usr/share/myapp/logs/agent.log`
96
97We 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```