Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,26 @@ func configure(_ app: Application) throws {
app.fcm.configuration = .envServiceAccountKey

/// case 2
/// with service account json string
/// put into your environment variable the following key:
/// FCM_SERVICE_ACCOUNT_KEY="{"prohect_id": "my_project123",...}"
app.fcm.configuration = .envServiceAccountKey

/// case 3
/// put into your environment variables the following keys:
/// FCM_EMAIL=... // `client_email` in service.json
/// FCM_PROJECT_ID=... // `project_id` in service.json
/// FCM_PRIVATE_KEY=... // `private_key` in service.json
app.fcm.configuration = .envServiceAccountKeyFields

/// case 3
/// case 4
/// put into your environment variables the following keys:
/// FCM_EMAIL=...
/// FCM_PROJECT_ID=...
/// FCM_KEY_PATH=path/to/key.pem
app.fcm.configuration = .envCredentials

/// case 4
/// case 5
/// manually
app.fcm.configuration = .init(email: "...", projectId: "...", key: "...")
}
Expand Down
24 changes: 21 additions & 3 deletions Sources/FCM/FCMConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ public struct FCMConfiguration {
self.senderId = s.sender_id ?? Environment.get("FCM_SENDER_ID")
}

public init (fromJSON json: String) {
let s = Self.parseServiceAccount(from: json)
self.email = s.client_email
self.projectId = s.project_id
self.key = s.private_key
self.serverKey = s.server_key ?? Environment.get("FCM_SERVER_KEY")
self.senderId = s.sender_id ?? Environment.get("FCM_SENDER_ID")
}

// MARK: Static initializers

/// It will try to read
Expand All @@ -60,9 +69,11 @@ public struct FCMConfiguration {

/// It will try to read path to service account key from environment variables
public static var envServiceAccountKey: FCMConfiguration {
guard let path = Environment.get("FCM_SERVICE_ACCOUNT_KEY_PATH")
else { fatalError("FCM envServiceAccountKey not set") }
return .init(pathToServiceAccountKey: path)
if let path = Environment.get("FCM_SERVICE_ACCOUNT_KEY_PATH") {
return .init(pathToServiceAccountKey: path)
} else if let jsonString = Environment.get("FCM_SERVICE_ACCOUNT_KEY") {
return .init(fromJSON: jsonString)
} else { fatalError("FCM envServiceAccountKey not set") }
}

/// It will try to read
Expand Down Expand Up @@ -109,4 +120,11 @@ public struct FCMConfiguration {
}
return serviceAccount
}

private static func parseServiceAccount(from json: String) -> ServiceAccount {
guard let data = json.data(using: .utf8), let serviceAccount = try? JSONDecoder().decode(ServiceAccount.self, from: data) else {
fatalError("FCM unable to decode serviceAccount from json string: \(json)")
}
return serviceAccount
}
}