Skip to content

Commit 3179a94

Browse files
committed
Updated express and koa
Signed-off-by: Vishal Rana <vr@labstack.com>
1 parent 5842ac4 commit 3179a94

File tree

8 files changed

+95
-87
lines changed

8 files changed

+95
-87
lines changed

README.md

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,4 @@
77
NPM `npm install --save labstack`<br>
88
Yarn `yarn add labstack`
99

10-
## Quick Start
11-
12-
[Sign up](https://labstack.com/signup) to get an API key
13-
14-
Create a file `app.js` with the following content:
15-
16-
```js
17-
const {Client, APIError} = require('labstack')
18-
19-
const client = new Client('<ACCOUNT_ID>', '<API_KEY>')
20-
21-
client.geocodeAddress({
22-
location: 'eiffel tower'
23-
}).then(response => {
24-
console.info(response)
25-
})
26-
.catch(error => {
27-
console.error(error)
28-
})
29-
```
30-
31-
From terminal run your app:
32-
33-
```sh
34-
node app.js
35-
```
36-
3710
## [Docs](https://labstack.com/docs) | [Forum](https://forum.labstack.com)

lib/cube.js

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,49 @@
1+
const os = require('os')
2+
const pusage = require('pidusage')
13
const got = require('got')
24

35
class Cube {
46
constructor(key, options) {
5-
this._started = false
67
this.url = 'https://api.labstack.com/cube'
78
this.key = key
8-
options = {} || options
9+
options = options || {}
10+
this.node = options.node || os.hostname()
911
this.batchSize = options.batchSize || 60
1012
this.dispatchInterval = options.dispatchInterval || 60
13+
this.tags = options.tags
1114
this.activeRequests = 0
1215
this.requests = []
16+
17+
// Dispatch daemon
18+
setInterval(() => {
19+
this._dispatch()
20+
}, this.dispatchInterval * 1000)
21+
22+
// System daemon
23+
setInterval(() => {
24+
pusage.stat(process.pid, (err, stat) => {
25+
this.uptime = Math.floor(process.uptime())
26+
this.cpu = stat.cpu
27+
this.memory = stat.memory
28+
})
29+
}, 1000)
1330
}
1431

1532
_requestID(request, response) {
33+
console.log(request.headers, response)
1634
return request.headers['x-request-id'] ||
17-
response._headers['x-request-id']
35+
response.headers['x-request-id']
1836
}
1937

2038
_realIP(request) {
21-
let ip = request.headers['x-forwarded-for'] ||
22-
request.headers['x-real-ip'] ||
23-
request.connection.remoteAddress
24-
if (Array.isArray(ip)) {
25-
return ip.split(', ')[0]
26-
} else {
27-
return ip
28-
}
39+
// let ip = request.headers['x-forwarded-for'] ||
40+
// request.headers['x-real-ip'] ||
41+
// request.connection.remoteAddress
42+
// if (Array.isArray(ip)) {
43+
// return ip.split(', ')[0]
44+
// } else {
45+
// return ip
46+
// }
2947
}
3048

3149
_resetRequests() {
@@ -37,6 +55,8 @@ class Cube {
3755
return
3856
}
3957

58+
console.log(this.requests)
59+
4060
// TODO: handler error
4161
got.post(this.url, {
4262
headers: {
@@ -58,40 +78,25 @@ class Cube {
5878
// Reset requests
5979
this.requests.length = 0
6080
}
61-
62-
start(request, response) {
81+
82+
start(request) {
6383
this.activeRequests++
6484

65-
// Daemon
66-
if (!this.started) {
67-
setInterval(() => {
68-
this._dispatch()
69-
}, this.dispatchInterval * 1000);
70-
this.started = true
71-
}
85+
request.time = Date.now()
86+
request.active = this.activeRequests
87+
request.node = this.node
88+
request.uptime = this.uptime
89+
request.cpu = this.cpu
90+
request.memory = this.memory
91+
this.requests.push(request)
7292

73-
const req = {}
74-
req.id = this._requestID(request, response)
75-
req.time = Date.now()
76-
req.host = request.headers.host
77-
req.path = request.url
78-
req.method = request.method
79-
req.bytes_in = parseInt(request.headers['content-length'])
80-
if (!req.bytes_in) {
81-
req.bytes_in = 0
82-
}
83-
req.remote_ip = this._realIP(request)
84-
req.client_id = req.remote_ip
85-
req.user_agent = request.headers['user-agent']
86-
this.requests.push(req)
87-
return req
93+
return request
8894
}
8995

90-
stop(request, response) {
96+
stop(request) {
9197
this.activeRequests--
92-
93-
request.status = response.statusCode
94-
request.bytes_out = response._headers['content-length']
98+
99+
// r.id = this._realIP(response)
95100
request.latency = Date.now() - request.time
96101

97102
// Dispatch batch
@@ -113,4 +118,3 @@ class Cube {
113118
module.exports = {
114119
Cube
115120
}
116-

lib/express.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
const {Cube} = require('./cube')
22

33
function cube(key, options) {
4+
const c = new Cube(key, options)
5+
46
return (request, response, next) => {
5-
const cube = new Cube(key, options)
6-
response.headers = response._headers
7-
const req = cube.start(request, response)
7+
const req = c.start({
8+
id: request.get('x-request-id'),
9+
host: request.hostname,
10+
path: request.path,
11+
method: request.method,
12+
bytes_in: parseInt(request.get('content-length')) || 0,
13+
remote_ip: request.ip,
14+
client_id: request.ip,
15+
user_agent: request.get('user-agent')
16+
})
17+
818
next()
9-
cube.stop(req, response)
19+
20+
req.id = req.id || response.get('x-request-id')
21+
req.status = response.statusCode
22+
req.bytes_out = parseInt(response.get('content-length')) || 0,
23+
c.stop(req)
1024
}
1125
}
1226

lib/koa.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
const {Cube} = require('./cube')
22

33
function cube(key, options) {
4-
return (context, next) => {
5-
const cube = new Cube(key, options)
6-
const {request, response} = context
7-
const req = cube.start(request, response)
4+
const c = new Cube(key, options)
5+
6+
return (ctx, next) => {
7+
const req = c.start({
8+
id: ctx.request.get('x-request-id'),
9+
host: ctx.hostname,
10+
path: ctx.path,
11+
method: ctx.method,
12+
bytes_in: parseInt(ctx.request.get('content-length')) || 0,
13+
remote_ip: ctx.ip,
14+
client_id: ctx.ip,
15+
user_agent: ctx.request.get('user-agent')
16+
})
17+
818
next()
9-
response.statusCode = response.status
10-
cube.stop(req, response)
19+
20+
req.id = req.id || ctx.response.get('x-request-id')
21+
req.status = ctx.status
22+
req.bytes_out = ctx.length
23+
c.stop(req)
1124
}
1225
}
1326

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"homepage": "https://github.com/labstack/labstack-node#readme",
2020
"dependencies": {
2121
"form-data": "^2.3.1",
22-
"got": "^7.1.0"
22+
"got": "^7.1.0",
23+
"pidusage": "^1.2.0"
2324
},
2425
"engines": {
2526
"node": ">= 6"

test/express.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ app.use(cube(process.env.LABSTACK_KEY, {
88
batchSize: 1
99
}))
1010
app.get('/', (req, res) => {
11-
console.log(res.statusCode)
1211
res.send('Hello World!')
1312
})
1413

15-
app.listen(3001, () => console.log('Example app listening on port 3000!'))
14+
app.listen(3001, () => console.log('Example app listening on port 3001!'))

test/koa.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ const Koa = require('koa')
22
const Router = require('koa-router')
33
const labstack = require('..')
44

5-
const koa = new Koa();
6-
const app = new Router();
5+
const koa = new Koa()
6+
const app = new Router()
77
const {cube} = labstack.koa
88

9-
app.get('/:name', async (ctx) => {
10-
ctx.body = `Hello, ${ctx.params.name}!\n`;
11-
});
9+
app.get('/', ctx => {
10+
ctx.body = 'Hello, World!'
11+
})
1212

1313
koa.use(cube(process.env.LABSTACK_KEY, {
1414
batchSize: 1
1515
}))
16-
koa.use(app.routes());
17-
koa.listen(3001);
16+
koa.use(app.routes())
17+
koa.listen(3002)

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,10 @@ path-to-regexp@^1.1.1:
449449
dependencies:
450450
isarray "0.0.1"
451451

452+
pidusage@^1.2.0:
453+
version "1.2.0"
454+
resolved "https://registry.yarnpkg.com/pidusage/-/pidusage-1.2.0.tgz#65ee96ace4e08a4cd3f9240996c85b367171ee92"
455+
452456
prepend-http@^1.0.1:
453457
version "1.0.4"
454458
resolved "https://registry.yarnpkg.com/prepend-http/-/prepend-http-1.0.4.tgz#d4f4562b0ce3696e41ac52d0e002e57a635dc6dc"

0 commit comments

Comments
 (0)