-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
example-knex.js
118 lines (107 loc) · 2.72 KB
/
example-knex.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
'use strict'
// Example of a Custom Store using Knex.js ORM for SQLite database
// Below is an example table to store rate limits that must be created
// in the database first
//
// CREATE TABLE "RateLimits" (
// "Route" TEXT,
// "Source" TEXT,
// "Count" INTEGER,
// "TTL" NUMERIC,
// PRIMARY KEY("Source")
// );
//
// CREATE UNIQUE INDEX "idx_uniq_route_source" ON "RateLimits" (Route, Source);
//
const Knex = require('knex')
const fastify = require('fastify')()
const knex = Knex({
client: 'sqlite3',
connection: {
filename: './db.sqlite'
}
})
function KnexStore (options) {
this.options = options
this.route = ''
}
KnexStore.prototype.routeKey = function (route) {
if (route) {
this.route = route
} else {
return route
}
}
KnexStore.prototype.incr = function (key, cb) {
const now = (new Date()).getTime()
const ttl = now + this.options.timeWindow
knex.transaction(function (trx) {
trx
.where({ Route: this.route, Source: key })
.then(d => {
if (d.TTL > now) {
trx
.raw(`UPDATE RateLimits SET Count = 1 WHERE Route='${this.route}' AND Source='${key}'`)
.then(() => {
cb(null, { current: 1, ttl: d.TTL })
})
.catch(err => {
cb(err, { current: 0 })
})
} else {
trx
.raw(`INSERT INTO RateLimits(Route, Source, Count, TTL) VALUES('${this.route}', '${key}',1,${d.TTL || ttl}) ON CONFLICT(Route, Source) DO UPDATE SET Count=Count+1,TTL=${ttl}`)
.then(() => {
cb(null, { current: d.Count ? d.Count + 1 : 1, ttl: d.TTL || ttl })
})
.catch(err => {
cb(err, { current: 0 })
})
}
})
.catch(err => {
cb(err, { current: 0 })
})
})
}
KnexStore.prototype.child = function (routeOptions) {
const options = Object.assign(this.options, routeOptions)
const store = new KnexStore(options)
store.routeKey(routeOptions.routeInfo.method + routeOptions.routeInfo.url)
return store
}
fastify.register(require('../../fastify-rate-limit'),
{
global: false,
max: 10,
store: KnexStore,
skipOnError: false
}
)
fastify.get('/', {
config: {
rateLimit: {
max: 10,
timeWindow: '1 minute'
}
}
}, (req, reply) => {
reply.send({ hello: 'from ... root' })
})
fastify.get('/private', {
config: {
rateLimit: {
max: 3,
timeWindow: '1 minute'
}
}
}, (req, reply) => {
reply.send({ hello: 'from ... private' })
})
fastify.get('/public', (req, reply) => {
reply.send({ hello: 'from ... public' })
})
fastify.listen({ port: 3000 }, err => {
if (err) throw err
console.log('Server listening at http://localhost:3000')
})