UNPKG

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