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

Commit

Permalink
Merge pull request #15 from nodes-vapor/feature/migrate-to-vapor-3
Browse files Browse the repository at this point in the history
Migration to Vapor 3
  • Loading branch information
cweinberger committed May 6, 2019
2 parents afac054 + 695602e commit 6892a52
Show file tree
Hide file tree
Showing 16 changed files with 399 additions and 234 deletions.
18 changes: 10 additions & 8 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@ version: 2
jobs:
MacOS:
macos:
xcode: "9.0"
xcode: "10.0.0"
steps:
- checkout
- restore_cache:
keys:
- v1-spm-deps-{{ checksum "Package.swift" }}
- run:
name: Install CMySQL and CTLS
name: Install dependencies
command: |
brew tap vapor/homebrew-tap
brew install cmysql
brew install ctls
brew install libressl
brew install cstack
- run:
name: Build and Run Tests
no_output_timeout: 1800
command: |
swift package generate-xcodeproj --enable-code-coverage
swift package generate-xcodeproj --enable-code-coverage
xcodebuild -scheme Gatekeeper-Package -enableCodeCoverage YES test | xcpretty
- run:
name: Report coverage to Codecov
Expand All @@ -30,25 +32,25 @@ jobs:
- .build
Linux:
docker:
- image: brettrtoomey/vapor-ci:0.0.1
- image: nodesvapor/vapor-ci:swift-4.2
steps:
- checkout
- restore_cache:
keys:
- v2-spm-deps-{{ checksum "Package.swift" }}
- v1-spm-deps-{{ checksum "Package.swift" }}
- run:
name: Copy Package file
name: Copy Package File
command: cp Package.swift res
- run:
name: Build and Run Tests
no_output_timeout: 1800
command: |
swift test -Xswiftc -DNOJSON
- run:
name: Restoring Package file
name: Restoring Package File
command: mv res Package.swift
- save_cache:
key: v2-spm-deps-{{ checksum "Package.swift" }}
key: v1-spm-deps-{{ checksum "Package.swift" }}
paths:
- .build
workflows:
Expand Down
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ xcuserdata
*.xcodeproj
Config/secrets/
.DS_Store
node_modules/
bower_components/
.swift-version
CMakeLists.txt
Package.resolved
7 changes: 4 additions & 3 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ included:
- Sources
function_body_length:
warning: 60
variable_name:
identifier_name:
min_length:
warning: 2
line_length: 80
line_length: 100
disabled_rules:
- opening_brace
- nesting
colon:
flexible_right_spacing: true
flexible_right_spacing: true
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017-2018 Nodes
Copyright (c) 2017-2019 Nodes

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
18 changes: 17 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
// swift-tools-version:4.2
import PackageDescription

let package = Package(
name: "Gatekeeper",
products: [
.library(
name: "Gatekeeper",
targets: ["Gatekeeper"]),
],
dependencies: [
.Package(url: "https://github.com/vapor/vapor.git", majorVersion: 2)
.package(url: "https://github.com/vapor/vapor.git", from: "3.0.0"),
],
targets: [
.target(
name: "Gatekeeper",
dependencies: [
"Vapor"
]),
.testTarget(
name: "GatekeeperTests",
dependencies: ["Gatekeeper"]),
]
)
92 changes: 41 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,92 +1,82 @@
# Gatekeeper 👮
[![Swift Version](https://img.shields.io/badge/Swift-3-brightgreen.svg)](http://swift.org)
[![Vapor Version](https://img.shields.io/badge/Vapor-2-F6CBCA.svg)](http://vapor.codes)
[![Swift Version](https://img.shields.io/badge/Swift-4.2-brightgreen.svg)](http://swift.org)
[![Vapor Version](https://img.shields.io/badge/Vapor-3-30B6FC.svg)](http://vapor.codes)
[![Circle CI](https://circleci.com/gh/nodes-vapor/gatekeeper/tree/master.svg?style=shield)](https://circleci.com/gh/nodes-vapor/gatekeeper)
[![codebeat badge](https://codebeat.co/badges/35c7b0bb-1662-44ae-b953-ab1d4aaf231f)](https://codebeat.co/projects/github-com-nodes-vapor-gatekeeper-master)
[![codecov](https://codecov.io/gh/nodes-vapor/gatekeeper/branch/master/graph/badge.svg)](https://codecov.io/gh/nodes-vapor/gatekeeper)
[![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)
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/nodes-vapor/gatekeeper/master/LICENSE)

Gatekeeper is a middleware that restricts the number of requests from clients, based on their IP address.
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
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.

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


## 📦 Installation

Update your `Package.swift` file.
Update your `Package.swift` dependencies:

```swift
.Package(url: "https://github.com/nodes-vapor/gatekeeper", majorVersion: 0)
.package(url: "https://github.com/nodes-vapor/gatekeeper.git", from: "3.0.0"),
```


## Getting started 🚀

`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.
as well as to your target (e.g. "App"):

```swift
let gatekeeper = GateKeeper(rate: Rate(10, per: .minute))
targets: [
.target(name: "App", dependencies: [..., "Gatekeeper", ...]),
// ...
]
```

### Adding middleware
You can add the middleware either globally or to a route group.
## Getting started 🚀

#### Adding Middleware Globally
### Configuration

#### `Sources/App/Config+Setup.swift`
in configure.swift:
```swift
import Gatekeeper
```

```swift
public func setup() throws {
// ...

addConfigurable(middleware: Gatekeeper(rate: Rate(10, per: .minute)), name: "gatekeeper")
}
```

#### `Config/droplet.json`

Add `gatekeeper` to the middleware array

```json
"middleware": [
"error",
"date",
"file",
"gatekeeper"
]
// [...]

// Register providers first
try services.register(
GatekeeperProvider(
config: GatekeeperConfig(maxRequests: 10, per: .second),
cacheFactory: { container -> KeyedCache in
return try container.make()
}
)
)
```

### Add to routes

#### Adding Middleware to a Route Group
You can add the `GatekeeperMiddleware` to specific routes or to all.

```Swift
let gatekeeper = Gatekeeper(rate: Rate(10, per: .minute))

drop.group(gatekeeper) { group in
// Routes
**Specific routes**
in routes.swift:
```swift
let protectedRoutes = router.grouped(GatekeeperMiddleware.self)
protectedRoutes.get("protected/hello") { req in
return "Protected Hello, World!"
}
```


### The `Rate.Interval` enumeration

The currently implemented intervals are:
**For all requests**
in configure.swift:
```swift
case .second
case .minute
case .hour
case .day
// Register middleware
var middlewares = MiddlewareConfig() // Create _empty_ middleware config
middlewares.use(GatekeeperMiddleware.self)
services.register(middlewares)
```

## Credits 🏆

This package is developed and maintained by the Vapor team at [Nodes](https://www.nodes.dk).
The package owner for this project is [Tom](https://github.com/tomserowka).

This package is developed and maintained by the Vapor team at [Nodes](https://www.nodesagency.com).
The package owner for this project is [Christian](https://github.com/cweinberger).

## License 📄

Expand Down
90 changes: 0 additions & 90 deletions Sources/Gatekeeper.swift

This file was deleted.

Loading

0 comments on commit 6892a52

Please sign in to comment.