|
1 | 1 | # Gatekeeper 👮
|
2 |
| -[](http://swift.org) |
3 |
| -[](http://vapor.codes) |
| 2 | +[](http://swift.org) |
| 3 | +[](http://vapor.codes) |
4 | 4 | [](https://circleci.com/gh/nodes-vapor/gatekeeper)
|
5 | 5 | [](https://codebeat.co/projects/github.colasdn.workers.dev-nodes-vapor-gatekeeper-master)
|
6 | 6 | [](https://codecov.io/gh/nodes-vapor/gatekeeper)
|
7 | 7 | [](http://clayallsopp.github.io/readme-score?url=https://github.com/nodes-vapor/gatekeeper)
|
8 | 8 | [](https://raw.githubusercontent.com/nodes-vapor/gatekeeper/master/LICENSE)
|
9 | 9 |
|
10 | 10 | Gatekeeper is a middleware that restricts the number of requests from clients, based on their IP address.
|
11 |
| -It works by adding the clients IP address to the cache and count how many requests the clients can make during the Gatekeeper's defined lifespan and give back an HTTP 429(Too Many Requests) if the limit has been reached. The number of requests left will be reset when the defined timespan has been reached |
| 11 | +It works by adding the clients IP address to the cache and count how many requests the clients can make during the Gatekeeper's defined lifespan and give back an HTTP 429(Too Many Requests) if the limit has been reached. The number of requests left will be reset when the defined timespan has been reached. |
12 | 12 |
|
13 | 13 | **Please take into consideration that multiple clients can be using the same IP address. eg. public wifi**
|
14 | 14 |
|
15 | 15 |
|
16 | 16 | ## 📦 Installation
|
17 | 17 |
|
18 |
| -Update your `Package.swift` file. |
| 18 | +Update your `Package.swift` dependencies: |
| 19 | + |
19 | 20 | ```swift
|
20 |
| -.Package(url: "https://github.com/nodes-vapor/gatekeeper", majorVersion: 0) |
| 21 | +.package(url: "https://github.com/nodes-vapor/gatekeeper.git", from: "3.0.0"), |
21 | 22 | ```
|
22 | 23 |
|
23 |
| - |
24 |
| -## Getting started 🚀 |
25 |
| - |
26 |
| -`Gatekeeper` has two configurable fields: the maximum rate and the cache to use. If you don't supply your own cache the limiter will create its own, in-memory cache. |
| 24 | +as well as to your target (e.g. "App"): |
27 | 25 |
|
28 | 26 | ```swift
|
29 |
| -let gatekeeper = GateKeeper(rate: Rate(10, per: .minute)) |
| 27 | +targets: [ |
| 28 | + .target(name: "App", dependencies: [..., "Gatekeeper", ...]), |
| 29 | +// ... |
| 30 | +] |
30 | 31 | ```
|
31 | 32 |
|
32 |
| -### Adding middleware |
33 |
| -You can add the middleware either globally or to a route group. |
| 33 | +## Getting started 🚀 |
34 | 34 |
|
35 |
| -#### Adding Middleware Globally |
| 35 | +### Configuration |
36 | 36 |
|
37 |
| -#### `Sources/App/Config+Setup.swift` |
| 37 | +in configure.swift: |
38 | 38 | ```swift
|
39 | 39 | import Gatekeeper
|
40 |
| -``` |
41 |
| - |
42 |
| -```swift |
43 |
| -public func setup() throws { |
44 |
| - // ... |
45 |
| - |
46 |
| - addConfigurable(middleware: Gatekeeper(rate: Rate(10, per: .minute)), name: "gatekeeper") |
47 |
| -} |
48 |
| -``` |
49 |
| - |
50 |
| -#### `Config/droplet.json` |
51 | 40 |
|
52 |
| -Add `gatekeeper` to the middleware array |
53 |
| - |
54 |
| -```json |
55 |
| -"middleware": [ |
56 |
| - "error", |
57 |
| - "date", |
58 |
| - "file", |
59 |
| - "gatekeeper" |
60 |
| -] |
| 41 | +// [...] |
| 42 | + |
| 43 | +// Register providers first |
| 44 | +try services.register( |
| 45 | + GatekeeperProvider( |
| 46 | + config: GatekeeperConfig(maxRequests: 10, per: .second), |
| 47 | + cacheFactory: { container -> KeyedCache in |
| 48 | + return try container.make() |
| 49 | + } |
| 50 | + ) |
| 51 | +) |
61 | 52 | ```
|
62 | 53 |
|
| 54 | +### Add to routes |
63 | 55 |
|
64 |
| -#### Adding Middleware to a Route Group |
| 56 | +You can add the `GatekeeperMiddleware` to specific routes or to all. |
65 | 57 |
|
66 |
| -```Swift |
67 |
| -let gatekeeper = Gatekeeper(rate: Rate(10, per: .minute)) |
68 |
| - |
69 |
| -drop.group(gatekeeper) { group in |
70 |
| - // Routes |
| 58 | +**Specific routes** |
| 59 | +in routes.swift: |
| 60 | +```swift |
| 61 | +let protectedRoutes = router.grouped(GatekeeperMiddleware.self) |
| 62 | +protectedRoutes.get("protected/hello") { req in |
| 63 | + return "Protected Hello, World!" |
71 | 64 | }
|
72 | 65 | ```
|
73 | 66 |
|
74 |
| - |
75 |
| -### The `Rate.Interval` enumeration |
76 |
| - |
77 |
| -The currently implemented intervals are: |
| 67 | +**For all requests** |
| 68 | +in configure.swift: |
78 | 69 | ```swift
|
79 |
| -case .second |
80 |
| -case .minute |
81 |
| -case .hour |
82 |
| -case .day |
| 70 | +// Register middleware |
| 71 | +var middlewares = MiddlewareConfig() // Create _empty_ middleware config |
| 72 | +middlewares.use(GatekeeperMiddleware.self) |
| 73 | +services.register(middlewares) |
83 | 74 | ```
|
84 | 75 |
|
85 | 76 | ## Credits 🏆
|
86 | 77 |
|
87 |
| -This package is developed and maintained by the Vapor team at [Nodes](https://www.nodes.dk). |
88 |
| -The package owner for this project is [Tom](https://github.com/tomserowka). |
89 |
| - |
| 78 | +This package is developed and maintained by the Vapor team at [Nodes](https://www.nodesagency.com). |
| 79 | +The package owner for this project is [Christian](https://github.com/cweinberger). |
90 | 80 |
|
91 | 81 | ## License 📄
|
92 | 82 |
|
|
0 commit comments