Skip to content

Commit

Permalink
fix: APNS Topic (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
HarryET authored Oct 26, 2022
1 parent 09dfc94 commit e1fffc1
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ FCM_API_KEY=
# APNS
APNS_SANDBOX=false
APNS_CERTIFICATE= # base64 encoded .p12 APNS Certificate
APNS_CERTIFICATE_PASSWORD= # Password for provided certificate
APNS_CERTIFICATE_PASSWORD= # Password for provided certificate
APNS_TOPIC= # bundle ID/app ID
2 changes: 2 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ jobs:
env:
TF_VAR_onepassword_vault_id: ${{ secrets.ONEPASSWORD_VAULT_ID }}
TF_VAR_fcm_api_key: ${{ secrets.FCM_API_KEY }}
TF_VAR_apns_topic: ${{ secrets.APNS_TOPIC }}
TF_VAR_apns_certificate: ${{ secrets.APNS_CERTIFICATE }}
TF_VAR_apns_certificate_password: ${{ secrets.APNS_CERTIFICATE_PASSWORD }}
TF_VAR_image_version: ${{ needs.get-version.outputs.version }}
Expand Down Expand Up @@ -155,6 +156,7 @@ jobs:
env:
TF_VAR_onepassword_vault_id: ${{ secrets.ONEPASSWORD_VAULT_ID }}
TF_VAR_fcm_api_key: ${{ secrets.FCM_API_KEY }}
TF_VAR_apns_topic: ${{ secrets.APNS_TOPIC }}
TF_VAR_apns_certificate: ${{ secrets.APNS_CERTIFICATE }}
TF_VAR_apns_certificate_password: ${{ secrets.APNS_CERTIFICATE_PASSWORD }}
TF_VAR_image_version: ${{ needs.get-version.outputs.version }}
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ci_terraform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ jobs:
env:
TF_VAR_onepassword_vault_id: ${{ secrets.ONEPASSWORD_VAULT_ID }}
TF_VAR_fcm_api_key: ${{ secrets.FCM_API_KEY }}
TF_VAR_apns_topic: ${{ secrets.APNS_TOPIC }}
TF_VAR_apns_certificate: ${{ secrets.APNS_CERTIFICATE }}
TF_VAR_apns_certificate_password: ${{ secrets.APNS_CERTIFICATE_PASSWORD }}
with:
Expand Down
1 change: 1 addition & 0 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct Config {
pub apns_sandbox: bool,
pub apns_certificate: Option<String>,
pub apns_certificate_password: Option<String>,
pub apns_topic: Option<String>,

// FCM
pub fcm_api_key: Option<String>,
Expand Down
6 changes: 5 additions & 1 deletion src/providers/apns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@ use tracing::span;
#[derive(Debug, Clone)]
pub struct ApnsProvider {
client: a2::Client,
topic: String,
}

impl ApnsProvider {
pub fn new_cert<R>(
cert: &mut R,
password: String,
endpoint: a2::Endpoint,
topic: String,
) -> crate::error::Result<Self>
where
R: Read,
{
Ok(ApnsProvider {
client: a2::Client::certificate(cert, password.as_str(), endpoint)?,
topic
})
}
}
Expand All @@ -35,7 +38,8 @@ impl PushProvider for ApnsProvider {
let s = span!(tracing::Level::DEBUG, "send_apns_notification");
let _ = s.enter();

let opt = a2::NotificationOptions::default();
let mut opt = a2::NotificationOptions::default();
opt.apns_topic = Some(&self.topic);

// TODO set title
let notification =
Expand Down
12 changes: 6 additions & 6 deletions src/providers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl PushProvider for Provider {
&mut self,
token: String,
payload: MessagePayload,
) -> crate::error::Result<()> {
) -> error::Result<()> {
let s = span!(tracing::Level::INFO, "send_notification");
let _ = s.enter();
match self {
Expand All @@ -104,7 +104,7 @@ pub struct Providers {
}

impl Providers {
pub fn new(config: &Config) -> crate::error::Result<Providers> {
pub fn new(config: &Config) -> error::Result<Providers> {
let supported = config.supported_providers();
let mut apns = None;
if supported.contains(&ProviderKind::Apns) {
Expand All @@ -113,13 +113,13 @@ impl Providers {
false => a2::Endpoint::Production,
};
apns = Some(
match (&config.apns_certificate, &config.apns_certificate_password) {
(Some(certificate), Some(password)) => {
match (&config.apns_certificate, &config.apns_certificate_password, &config.apns_topic) {
(Some(certificate), Some(password), Some(topic)) => {
let decoded = base64::decode(certificate)?;
let mut reader = BufReader::new(&*decoded);

let apns_client =
ApnsProvider::new_cert(&mut reader, password.clone(), endpoint)?;
ApnsProvider::new_cert(&mut reader, password.clone(), endpoint, topic)?;

Ok(apns_client)
}
Expand Down Expand Up @@ -147,7 +147,7 @@ impl Providers {
pub fn get_provider(
provider: ProviderKind,
state: &AppState<impl ClientStore, impl NotificationStore>,
) -> crate::error::Result<Provider> {
) -> error::Result<Provider> {
let name = provider.as_str();
let supported = state.config.supported_providers();

Expand Down
1 change: 1 addition & 0 deletions terraform/ecs/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ resource "aws_ecs_task_definition" "app_task_definition" {
{ name = "TELEMETRY_GRPC_URL", value = "http://localhost:4317" },
{ name = "FCM_API_KEY", value = var.fcm_api_key },
{ name = "APNS_SANDBOX", value = terraform.workspace == "dev" ? "true" : "false" },
{ name = "APNS_TOPIC", value = var.apns_topic },
{ name = "APNS_CERTIFICATE", value = var.apns_certificate },
{ name = "APNS_CERTIFICATE_PASSWORD", value = var.apns_certificate_password }
],
Expand Down
4 changes: 4 additions & 0 deletions terraform/ecs/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ variable "fcm_api_key" {
sensitive = true
}

variable "apns_topic" {
type = string
}

variable "apns_certificate" {
type = string
sensitive = true
Expand Down
1 change: 1 addition & 0 deletions terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ module "ecs" {
vpc_cidr = module.vpc.vpc_cidr_block
vpc_id = module.vpc.vpc_id
fcm_api_key = var.fcm_api_key
apns_topic = var.apns_topic
apns_certificate = var.apns_certificate
apns_certificate_password = var.apns_certificate_password
}
Expand Down
4 changes: 4 additions & 0 deletions terraform/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ variable "fcm_api_key" {
sensitive = true
}

variable "apns_topic" {
type = string
}

variable "apns_certificate" {
type = string
sensitive = true
Expand Down

1 comment on commit e1fffc1

@781191131
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work thanks

Please sign in to comment.