tom-microservice 3.7.1 → 3.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -0
- package/bin/listen.js +3 -2
- package/package.json +1 -1
- package/src/routes.js +8 -6
package/README.md
CHANGED
|
@@ -62,6 +62,10 @@ To view details for a command at any time use `tom --help`
|
|
|
62
62
|
|
|
63
63
|
### from Node.js
|
|
64
64
|
|
|
65
|
+
#### as process
|
|
66
|
+
|
|
67
|
+
You can interact with **tom** 🐶 from Node.js user code:
|
|
68
|
+
|
|
65
69
|
```js
|
|
66
70
|
// First of all you need to declare a configuration file.
|
|
67
71
|
const config = {/* See configuration section */}
|
|
@@ -73,6 +77,37 @@ const tom = require('tom-microservice')(config)
|
|
|
73
77
|
const { payment, email } = tom
|
|
74
78
|
```
|
|
75
79
|
|
|
80
|
+
#### as HTTP router
|
|
81
|
+
|
|
82
|
+
Additionally, you can get the **tom** 🐶 HTTP router in standalone way:
|
|
83
|
+
|
|
84
|
+
```js
|
|
85
|
+
const { createRoutes } = require('tom-microservice')
|
|
86
|
+
const config = {/* See configuration section */}
|
|
87
|
+
|
|
88
|
+
const router = createRoutes(config, ({ tom, router, send }) => {
|
|
89
|
+
router
|
|
90
|
+
.get('/ping', (req, res) => send(res, 200, 'healthy'))
|
|
91
|
+
})
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
#### as HTTP server
|
|
95
|
+
|
|
96
|
+
You can also intialize **tom** 🐶 HTTP server from code:
|
|
97
|
+
|
|
98
|
+
```js
|
|
99
|
+
const { listen, createRoutes } = require('tom-microservice')
|
|
100
|
+
|
|
101
|
+
const config = {/* See configuration section */}
|
|
102
|
+
|
|
103
|
+
const router = createRoutes(config, ({ tom, router, send }) => {
|
|
104
|
+
router
|
|
105
|
+
.get('/ping', (req, res) => send(res, 200, 'healthy'))
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
listen(config, { routes })
|
|
109
|
+
```
|
|
110
|
+
|
|
76
111
|
## Configuration
|
|
77
112
|
|
|
78
113
|
!> Combine with [miconfig](https://www.npmjs.com/package/miconfig) for loading different settings based on environment.
|
package/bin/listen.js
CHANGED
|
@@ -7,8 +7,9 @@ const { createServer } = require('http')
|
|
|
7
7
|
const PORT =
|
|
8
8
|
process.env.PORT || process.env.port || process.env.TOM_PORT || 3000
|
|
9
9
|
|
|
10
|
-
module.exports = async (tomConfig, { port = PORT } = {}) => {
|
|
11
|
-
|
|
10
|
+
module.exports = async (tomConfig, { port = PORT, routes } = {}) => {
|
|
11
|
+
if (!routes) routes = require('../src/routes')(tomConfig)
|
|
12
|
+
|
|
12
13
|
const server = createServer(routes)
|
|
13
14
|
|
|
14
15
|
server.listen(port, () => {
|
package/package.json
CHANGED
package/src/routes.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const { get, eq, forEach } = require('lodash')
|
|
3
|
+
const { get, eq, forEach, noop } = require('lodash')
|
|
4
4
|
const { buffer, text } = require('http-body')
|
|
5
5
|
const requestIp = require('request-ip')
|
|
6
6
|
const toQuery = require('to-query')()
|
|
@@ -92,15 +92,15 @@ const createRouter = () => {
|
|
|
92
92
|
}
|
|
93
93
|
|
|
94
94
|
router
|
|
95
|
-
.get('/', (
|
|
96
|
-
.get('/robots.txt', (
|
|
97
|
-
.get('/favicon.ico', (
|
|
98
|
-
.get('/ping', (
|
|
95
|
+
.get('/', (_, res) => send(res, 204))
|
|
96
|
+
.get('/robots.txt', (_, res) => send(res, 204))
|
|
97
|
+
.get('/favicon.ico', (_, res) => send(res, 204))
|
|
98
|
+
.get('/ping', (_, res) => send(res, 200, 'pong'))
|
|
99
99
|
|
|
100
100
|
return router
|
|
101
101
|
}
|
|
102
102
|
|
|
103
|
-
module.exports = tomConfig => {
|
|
103
|
+
module.exports = (tomConfig, fn = noop) => {
|
|
104
104
|
if (!tomConfig) throw TypeError('You need to provide tom configuration file.')
|
|
105
105
|
|
|
106
106
|
const router = createRouter()
|
|
@@ -116,5 +116,7 @@ module.exports = tomConfig => {
|
|
|
116
116
|
})
|
|
117
117
|
})
|
|
118
118
|
|
|
119
|
+
fn({ tom, router, send })
|
|
120
|
+
|
|
119
121
|
return router
|
|
120
122
|
}
|