Skip to content

Commit

Permalink
Upgrade to vapor 4
Browse files Browse the repository at this point in the history
  • Loading branch information
code28 committed Aug 25, 2020
1 parent 7e523aa commit e37cdd5
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 107 deletions.
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) 2019 Simon Kempendorf
Copyright (c) 2020 TICE Software UG (haftungsbeschränkt)

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
132 changes: 57 additions & 75 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2,156 +2,138 @@
"object": {
"pins": [
{
"package": "Console",
"repositoryURL": "https://github.com/vapor/console.git",
"package": "async-http-client",
"repositoryURL": "https://github.com/swift-server/async-http-client.git",
"state": {
"branch": null,
"revision": "74cfbea629d4aac34a97cead2447a6870af1950b",
"version": "3.1.1"
"revision": "4b4d6605aa2e4f0c2ae3c7563795ae3bec259fff",
"version": "1.2.1"
}
},
{
"package": "Core",
"repositoryURL": "https://github.com/vapor/core.git",
"package": "async-kit",
"repositoryURL": "https://github.com/vapor/async-kit.git",
"state": {
"branch": null,
"revision": "10d33362a47fab03a067e78fb0791341d9c634fa",
"version": "3.9.3"
"revision": "7457413e57dbfac762b32dd30c1caf2c55a02a3d",
"version": "1.2.0"
}
},
{
"package": "Crypto",
"repositoryURL": "https://github.com/vapor/crypto.git",
"package": "console-kit",
"repositoryURL": "https://github.com/vapor/console-kit.git",
"state": {
"branch": null,
"revision": "df8eb7d8ae51787b3a0628aa3975e67666da936c",
"version": "3.3.3"
"revision": "7cf8185ad62d50ae9777ce78bcfde8f5c9f900e2",
"version": "4.2.1"
}
},
{
"package": "DatabaseKit",
"repositoryURL": "https://github.com/vapor/database-kit.git",
"package": "routing-kit",
"repositoryURL": "https://github.com/vapor/routing-kit.git",
"state": {
"branch": null,
"revision": "8f352c8e66dab301ab9bfef912a01ce1361ba1e4",
"version": "1.3.3"
"revision": "4cf052b78aebaf1b23f2264ce04d57b4b6eb5254",
"version": "4.2.0"
}
},
{
"package": "HTTP",
"repositoryURL": "https://github.com/vapor/http.git",
"package": "swift-backtrace",
"repositoryURL": "https://github.com/swift-server/swift-backtrace.git",
"state": {
"branch": null,
"revision": "3808ed0401379b6e9f4a053f03090ea9d658caa9",
"version": "3.2.1"
"revision": "f2fd8c4845a123419c348e0bc4b3839c414077d5",
"version": "1.2.0"
}
},
{
"package": "Multipart",
"repositoryURL": "https://github.com/vapor/multipart.git",
"package": "swift-crypto",
"repositoryURL": "https://github.com/apple/swift-crypto.git",
"state": {
"branch": null,
"revision": "f063180d0b84832accd33194e06ed3c41f8609ac",
"version": "3.1.1"
"revision": "9b9d1868601a199334da5d14f4ab2d37d4f8d0c5",
"version": "1.0.2"
}
},
{
"package": "Routing",
"repositoryURL": "https://github.com/vapor/routing.git",
"package": "swift-log",
"repositoryURL": "https://github.com/apple/swift-log.git",
"state": {
"branch": null,
"revision": "d76f339c9716785e5079af9d7075d28ff7da3d92",
"version": "3.1.0"
"revision": "173f567a2dfec11d74588eea82cecea555bdc0bc",
"version": "1.4.0"
}
},
{
"package": "Service",
"repositoryURL": "https://github.com/vapor/service.git",
"package": "swift-metrics",
"repositoryURL": "https://github.com/apple/swift-metrics.git",
"state": {
"branch": null,
"revision": "fa5b5de62bd68bcde9a69933f31319e46c7275fb",
"version": "1.0.2"
"revision": "708b960b4605abb20bc55d65abf6bad607252200",
"version": "2.0.0"
}
},
{
"package": "swift-nio",
"repositoryURL": "https://github.com/apple/swift-nio.git",
"state": {
"branch": null,
"revision": "ba7970fe396e8198b84c6c1b44b38a1d4e2eb6bd",
"version": "1.14.1"
}
},
{
"package": "swift-nio-ssl",
"repositoryURL": "https://github.com/apple/swift-nio-ssl.git",
"state": {
"branch": null,
"revision": "0f3999f3e3c359cc74480c292644c3419e44a12f",
"version": "1.4.0"
"revision": "cf6f2b1807219643270616e03de697b96c46384c",
"version": "2.21.0"
}
},
{
"package": "swift-nio-ssl-support",
"repositoryURL": "https://github.com/apple/swift-nio-ssl-support.git",
"package": "swift-nio-extras",
"repositoryURL": "https://github.com/apple/swift-nio-extras.git",
"state": {
"branch": null,
"revision": "c02eec4e0e6d351cd092938cf44195a8e669f555",
"version": "1.0.0"
"revision": "0b9eb87b227b37544e5b79090bd69e2156efcd24",
"version": "1.6.1"
}
},
{
"package": "swift-nio-zlib-support",
"repositoryURL": "https://github.com/apple/swift-nio-zlib-support.git",
"package": "swift-nio-http2",
"repositoryURL": "https://github.com/apple/swift-nio-http2.git",
"state": {
"branch": null,
"revision": "37760e9a52030bb9011972c5213c3350fa9d41fd",
"version": "1.0.0"
"revision": "9f4eff06038a9d1301e939a55a0db935634a2f6e",
"version": "1.14.0"
}
},
{
"package": "TemplateKit",
"repositoryURL": "https://github.com/vapor/template-kit.git",
"state": {
"branch": null,
"revision": "51405c83e95e8adb09565278a5e9b959c605e56c",
"version": "1.4.0"
}
},
{
"package": "URLEncodedForm",
"repositoryURL": "https://github.com/vapor/url-encoded-form.git",
"package": "swift-nio-ssl",
"repositoryURL": "https://github.com/apple/swift-nio-ssl.git",
"state": {
"branch": null,
"revision": "82d8d63bdb76b6dd8febe916c639ab8608dbbaed",
"version": "1.0.6"
"revision": "8a137b72a9339f295bc8bb95cd2fafe207f1df0d",
"version": "2.9.0"
}
},
{
"package": "Validation",
"repositoryURL": "https://github.com/vapor/validation.git",
"package": "swift-nio-transport-services",
"repositoryURL": "https://github.com/apple/swift-nio-transport-services.git",
"state": {
"branch": null,
"revision": "4de213cf319b694e4ce19e5339592601d4dd3ff6",
"version": "2.1.1"
"revision": "d40a5e34e5b35f4f961cb34aeb2e0a02f42a945f",
"version": "1.8.0"
}
},
{
"package": "Vapor",
"package": "vapor",
"repositoryURL": "https://github.com/vapor/vapor.git",
"state": {
"branch": null,
"revision": "92a58a9a84e4330500b99fe355a94d29f67abe58",
"version": "3.3.1"
"revision": "d910be46835c4c0c5a432de5012b61bdea4695bb",
"version": "4.29.0"
}
},
{
"package": "WebSocket",
"repositoryURL": "https://github.com/vapor/websocket.git",
"package": "websocket-kit",
"repositoryURL": "https://github.com/vapor/websocket-kit.git",
"state": {
"branch": null,
"revision": "d85e5b6dce4d04065865f77385fc3324f98178f6",
"version": "1.1.2"
"revision": "b0736014be634475dac4c23843811257d86dcdc1",
"version": "2.1.1"
}
}
]
Expand Down
5 changes: 4 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import PackageDescription

let package = Package(
name: "Gorush",
platforms: [
.macOS(.v10_15)
],
products: [
.library(name: "Gorush", targets: ["Gorush"])
],
dependencies: [
.package(url: "https://github.com/vapor/vapor.git", from: "3.0.0")
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0")
],
targets: [
.target(name: "Gorush", dependencies: ["Vapor"]),
Expand Down
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ This service allows to easily integrate [Gorush](https://github.com/appleboy/gor
## Installation
### Add the package to the `Package.swift`
```swift
.package(url: "https://github.com/code28/vapor-gorush.git", from: "0.3.0")
.package(url: "https://github.com/TICESoftware/vapor-gorush.git", from: "1.0.0")
// ...
.target(name: "App", dependencies: ["Vapor", "Gorush"])
```
From version 1.0.0, this uses Vapor 4. If you want to use Gorush in Vapor 3, you can use version 0.9.0.

### Register Gorush in `configure.swift`
```swift
let hostname = Environment.get("GORUSH_HOSTNAME")
let port = Environment.get("GORUSH_PORT")
services.register(Gorush.self) { container in
return Gorush(hostname: hostname, port: port)
app.gorush.use { req in
Gorush(client: req.client, hostname: hostname)
}
```
Per default, HTTPS will be used. To use HTTP instead, just add `httpScheme: .http` into that initialization.
Expand All @@ -26,8 +26,7 @@ import Gorush
// ...
let notification = GorushNotification(tokens: ["deviceToken"], platform: .ios, message: "Push message", title: "Push title")

let gorush = container.make(Gorush.self)
gorush.dispatch(notification, on: worker).map { (response: GorushResponse) in
req.gorush.dispatch(notification, on: req.eventLoop).map { (response: GorushResponse) in
if !response.logs.isEmpty {
// This means an error occurred, if Gorush was configured to return errors
}
Expand Down
42 changes: 18 additions & 24 deletions Sources/Gorush/Gorush.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,30 @@
import Foundation
import Vapor

public final class Gorush: Service {
let httpScheme: HTTPScheme
let hostname: String
let url: String
let port: Int?
let encoder: DataEncoder

public init(httpScheme: HTTPScheme = .https, hostname: String, port: Int? = nil, url: String = "/api/push", encoder: DataEncoder = JSONEncoder()) {
self.httpScheme = httpScheme
self.hostname = hostname
self.port = port
self.url = url
public struct Gorush {
let client: Client
let url: URI
let encoder: ContentEncoder

public init(client: Client, hostname: String, path: String = "/api/push", encoder: ContentEncoder = JSONEncoder()) {
self.client = client
let scheme = hostname.hasPrefix("http") ? "" : "https"
self.url = URI(string: "\(scheme)\(hostname)\(path)")
self.encoder = encoder
}

public func dispatch(_ gorushMessage: GorushMessage, on worker: Worker) -> Future<GorushResponse> {
return HTTPClient.connect(scheme: httpScheme, hostname: hostname, port: port, on: worker).flatMap { client in
let headers = [("Content-Type", "application/json")]
let body = try self.encoder.encode(gorushMessage)
let request = HTTPRequest(method: .POST, url: self.url, headers: HTTPHeaders(headers), body: body)

return client.send(request).map { response in
return try JSONDecoder().decode(GorushResponse.self, from: response.body.data!)
}
public func dispatch(_ gorushMessage: GorushMessage, on eventLoop: EventLoop) -> EventLoopFuture<GorushResponse> {
return client.post(url) { (request: inout ClientRequest) in
var body = ByteBuffer()
try self.encoder.encode(gorushMessage, to: &body, headers: &request.headers)
request.body = body
}.flatMapThrowing { (response: ClientResponse) in
return try response.content.decode(GorushResponse.self)
}
}

// MARK: Convenience method

public func dispatch(_ gorushNotification: GorushNotification, on worker: Worker) -> Future<GorushResponse> {
return dispatch(GorushMessage(notifications: [gorushNotification]), on: worker)
public func dispatch(_ gorushNotification: GorushNotification, on eventLoop: EventLoop) -> EventLoopFuture<GorushResponse> {
return dispatch(GorushMessage(notifications: [gorushNotification]), on: eventLoop)
}
}
37 changes: 37 additions & 0 deletions Sources/Gorush/GorushFactory.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// GorushFactory.swift
// Gorush
//
// Created by Simon Kempendorf on 25.08.20.
//

import Foundation
import Vapor

public struct GorushFactory {
var make: ((Request) -> Gorush)?
public mutating func use(_ make: @escaping ((Request) -> Gorush)) {
self.make = make
}
}

extension Application {
private struct GorushKey: StorageKey {
typealias Value = GorushFactory
}

public var gorush: GorushFactory {
get {
self.storage[GorushKey.self] ?? .init()
}
set {
self.storage[GorushKey.self] = newValue
}
}
}

extension Request {
public var gorush: Gorush {
self.application.gorush.make!(self)
}
}

0 comments on commit e37cdd5

Please sign in to comment.