Skip to content

Commit

Permalink
cleanup + config improvements (#6)
Browse files Browse the repository at this point in the history
* cleanup + config improvements

* require newer vapor tag

* fix merge issue
  • Loading branch information
tanner0101 authored Dec 11, 2019
1 parent e531234 commit cec35a4
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 152 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/kylebrowning/APNSwift.git", from: "1.7.0"),
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0-beta.2"),
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0-beta.2.1"),
],
targets: [
.target(name: "APNS", dependencies: ["APNSwift", "Vapor"]),
Expand Down
149 changes: 0 additions & 149 deletions Sources/APNS/APNS.swift

This file was deleted.

26 changes: 26 additions & 0 deletions Sources/APNS/APNSConnectionSource.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import AsyncKit
import Logging

public final class APNSConnectionSource: ConnectionPoolSource {
private let configuration: APNSwiftConfiguration

public init(configuration: APNSwiftConfiguration) {
self.configuration = configuration
}
public func makeConnection(
logger: Logger,
on eventLoop: EventLoop
) -> EventLoopFuture<APNSwiftConnection> {
APNSwiftConnection.connect(configuration: self.configuration, on: eventLoop)
}
}

extension APNSwiftConnection: ConnectionPoolItem {
public var eventLoop: EventLoop {
self.channel.eventLoop
}

public var isClosed: Bool {
self.channel.isActive
}
}
52 changes: 52 additions & 0 deletions Sources/APNS/Application+APNS.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import Vapor

extension Application {
public var apns: APNS {
.init(application: self)
}

public struct APNS {
struct ConfigurationKey: StorageKey {
typealias Value = APNSwiftConfiguration
}

public var configuration: APNSwiftConfiguration? {
get {
self.application.storage[ConfigurationKey.self]
}
nonmutating set {
self.application.storage[ConfigurationKey.self] = newValue
}
}


struct PoolKey: StorageKey, LockKey {
typealias Value = EventLoopGroupConnectionPool<APNSConnectionSource>
}

internal var pool: EventLoopGroupConnectionPool<APNSConnectionSource> {
if let existing = self.application.storage[PoolKey.self] {
return existing
} else {
let lock = self.application.locks.lock(for: PoolKey.self)
lock.lock()
defer { lock.unlock() }
guard let configuration = self.configuration else {
fatalError("APNS not configured. Use app.apns.configuration = ...")
}
let new = EventLoopGroupConnectionPool(
source: APNSConnectionSource(configuration: configuration),
maxConnectionsPerEventLoop: 1,
logger: self.application.logger,
on: self.application.eventLoopGroup
)
self.application.storage.set(PoolKey.self, to: new) {
$0.shutdown()
}
return new
}
}

let application: Application
}
}
File renamed without changes.
48 changes: 48 additions & 0 deletions Sources/APNS/Request+APNS.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import Vapor

extension Request {
public var apns: APNS {
.init(request: self)
}

public struct APNS {
let request: Request
}
}

extension Request.APNS: APNSwiftClient {
public var logger: Logger? {
self.request.logger
}

public var eventLoop: EventLoop {
self.request.eventLoop
}

public func send(
rawBytes payload: ByteBuffer,
pushType: APNSwiftConnection.PushType,
to deviceToken: String,
expiration: Date?,
priority: Int?,
collapseIdentifier: String?,
topic: String?,
logger: Logger?
) -> EventLoopFuture<Void> {
self.request.application.apns.pool.withConnection(
logger: logger,
on: self.eventLoop
) {
$0.send(
rawBytes: payload,
pushType: pushType,
to: deviceToken,
expiration: expiration,
priority: priority,
collapseIdentifier: collapseIdentifier,
topic: topic,
logger: logger
)
}
}
}
4 changes: 2 additions & 2 deletions Tests/APNSTests/APNSTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ class APNSTests: XCTestCase {
let app = Application(.testing)
defer { app.shutdown() }

try app.apns.configure(.init(
app.apns.configuration = try .init(
keyIdentifier: "9UC9ZLQ8YW",
teamIdentifier: "ABBM6U9RM5",
signer: .init(buffer: ByteBufferAllocator().buffer(capacity: 1024)),
topic: "com.grasscove.Fern",
environment: .sandbox
))
)

app.get("test-push") { req -> EventLoopFuture<HTTPStatus> in
req.apns.send(
Expand Down

0 comments on commit cec35a4

Please sign in to comment.