Skip to content
This repository was archived by the owner on Apr 20, 2024. It is now read-only.

Commit 6892a52

Browse files
authored
Merge pull request #15 from nodes-vapor/feature/migrate-to-vapor-3
Migration to Vapor 3
2 parents afac054 + 695602e commit 6892a52

16 files changed

+399
-234
lines changed

.circleci/config.yml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,25 @@ version: 2
22
jobs:
33
MacOS:
44
macos:
5-
xcode: "9.0"
5+
xcode: "10.0.0"
66
steps:
77
- checkout
88
- restore_cache:
99
keys:
1010
- v1-spm-deps-{{ checksum "Package.swift" }}
1111
- run:
12-
name: Install CMySQL and CTLS
12+
name: Install dependencies
1313
command: |
1414
brew tap vapor/homebrew-tap
1515
brew install cmysql
1616
brew install ctls
17+
brew install libressl
18+
brew install cstack
1719
- run:
1820
name: Build and Run Tests
1921
no_output_timeout: 1800
2022
command: |
21-
swift package generate-xcodeproj --enable-code-coverage
23+
swift package generate-xcodeproj --enable-code-coverage
2224
xcodebuild -scheme Gatekeeper-Package -enableCodeCoverage YES test | xcpretty
2325
- run:
2426
name: Report coverage to Codecov
@@ -30,25 +32,25 @@ jobs:
3032
- .build
3133
Linux:
3234
docker:
33-
- image: brettrtoomey/vapor-ci:0.0.1
35+
- image: nodesvapor/vapor-ci:swift-4.2
3436
steps:
3537
- checkout
3638
- restore_cache:
3739
keys:
38-
- v2-spm-deps-{{ checksum "Package.swift" }}
40+
- v1-spm-deps-{{ checksum "Package.swift" }}
3941
- run:
40-
name: Copy Package file
42+
name: Copy Package File
4143
command: cp Package.swift res
4244
- run:
4345
name: Build and Run Tests
4446
no_output_timeout: 1800
4547
command: |
4648
swift test -Xswiftc -DNOJSON
4749
- run:
48-
name: Restoring Package file
50+
name: Restoring Package File
4951
command: mv res Package.swift
5052
- save_cache:
51-
key: v2-spm-deps-{{ checksum "Package.swift" }}
53+
key: v1-spm-deps-{{ checksum "Package.swift" }}
5254
paths:
5355
- .build
5456
workflows:

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ xcuserdata
55
*.xcodeproj
66
Config/secrets/
77
.DS_Store
8-
node_modules/
9-
bower_components/
108
.swift-version
119
CMakeLists.txt
10+
Package.resolved

.swiftlint.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ included:
22
- Sources
33
function_body_length:
44
warning: 60
5-
variable_name:
5+
identifier_name:
66
min_length:
77
warning: 2
8-
line_length: 80
8+
line_length: 100
99
disabled_rules:
1010
- opening_brace
11+
- nesting
1112
colon:
12-
flexible_right_spacing: true
13+
flexible_right_spacing: true

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2017-2018 Nodes
3+
Copyright (c) 2017-2019 Nodes
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

Package.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
1+
// swift-tools-version:4.2
12
import PackageDescription
23

34
let package = Package(
45
name: "Gatekeeper",
6+
products: [
7+
.library(
8+
name: "Gatekeeper",
9+
targets: ["Gatekeeper"]),
10+
],
511
dependencies: [
6-
.Package(url: "https://github.com/vapor/vapor.git", majorVersion: 2)
12+
.package(url: "https://github.com/vapor/vapor.git", from: "3.0.0"),
13+
],
14+
targets: [
15+
.target(
16+
name: "Gatekeeper",
17+
dependencies: [
18+
"Vapor"
19+
]),
20+
.testTarget(
21+
name: "GatekeeperTests",
22+
dependencies: ["Gatekeeper"]),
723
]
824
)

README.md

Lines changed: 41 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,82 @@
11
# Gatekeeper 👮
2-
[![Swift Version](https://img.shields.io/badge/Swift-3-brightgreen.svg)](http://swift.org)
3-
[![Vapor Version](https://img.shields.io/badge/Vapor-2-F6CBCA.svg)](http://vapor.codes)
2+
[![Swift Version](https://img.shields.io/badge/Swift-4.2-brightgreen.svg)](http://swift.org)
3+
[![Vapor Version](https://img.shields.io/badge/Vapor-3-30B6FC.svg)](http://vapor.codes)
44
[![Circle CI](https://circleci.com/gh/nodes-vapor/gatekeeper/tree/master.svg?style=shield)](https://circleci.com/gh/nodes-vapor/gatekeeper)
55
[![codebeat badge](https://codebeat.co/badges/35c7b0bb-1662-44ae-b953-ab1d4aaf231f)](https://codebeat.co/projects/github.colasdn.workers.dev-nodes-vapor-gatekeeper-master)
66
[![codecov](https://codecov.io/gh/nodes-vapor/gatekeeper/branch/master/graph/badge.svg)](https://codecov.io/gh/nodes-vapor/gatekeeper)
77
[![Readme Score](http://readme-score-api.herokuapp.com/score.svg?url=https://github.com/nodes-vapor/gatekeeper)](http://clayallsopp.github.io/readme-score?url=https://github.com/nodes-vapor/gatekeeper)
88
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/nodes-vapor/gatekeeper/master/LICENSE)
99

1010
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.
1212

1313
**Please take into consideration that multiple clients can be using the same IP address. eg. public wifi**
1414

1515

1616
## 📦 Installation
1717

18-
Update your `Package.swift` file.
18+
Update your `Package.swift` dependencies:
19+
1920
```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"),
2122
```
2223

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"):
2725

2826
```swift
29-
let gatekeeper = GateKeeper(rate: Rate(10, per: .minute))
27+
targets: [
28+
.target(name: "App", dependencies: [..., "Gatekeeper", ...]),
29+
// ...
30+
]
3031
```
3132

32-
### Adding middleware
33-
You can add the middleware either globally or to a route group.
33+
## Getting started 🚀
3434

35-
#### Adding Middleware Globally
35+
### Configuration
3636

37-
#### `Sources/App/Config+Setup.swift`
37+
in configure.swift:
3838
```swift
3939
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`
5140

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+
)
6152
```
6253

54+
### Add to routes
6355

64-
#### Adding Middleware to a Route Group
56+
You can add the `GatekeeperMiddleware` to specific routes or to all.
6557

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!"
7164
}
7265
```
7366

74-
75-
### The `Rate.Interval` enumeration
76-
77-
The currently implemented intervals are:
67+
**For all requests**
68+
in configure.swift:
7869
```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)
8374
```
8475

8576
## Credits 🏆
8677

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).
9080

9181
## License 📄
9282

Sources/Gatekeeper.swift

Lines changed: 0 additions & 90 deletions
This file was deleted.

0 commit comments

Comments
 (0)