Skip to content

Add config #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 1, 2023
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
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ end
mix generate.vapid.keys
```

3. Set environment variables for your generated keys:
3. Set config for your generated keys:

```yaml
environment:
- VAPID_PUBLIC_KEY=someVapidPublicKey
- VAPID_PRIVATE_KEY=someVapidPrivateKey
- VAPID_SUBJECT=mailto:[email protected]
```elixir
config :web_push_elixir,
vapid_public_key: "someVapidPublicKey",
vapid_private_key: "someVapidPrivateKey",
vapid_subject: "mailto:[email protected]"
```

## Usage

`WebPushElixir` provides a simple `send_notification/2` function that accepts 2 arguments:
`WebPushElixir` provides a simple `send_notification/2` that takes 2 arguments:

* `subscription`: the subscription information received from the client - [example demo](https://midarrlabs.github.io/web-push-elixir/)
* `message`: the message string.
Expand Down
5 changes: 5 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Config

if Mix.env() == :test do
import_config "test.exs"
end
6 changes: 6 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Config

config :web_push_elixir,
vapid_public_key: "someVapidPublicKey",
vapid_private_key: "someVapidPrivateKey",
vapid_subject: "mailto:[email protected]"
6 changes: 3 additions & 3 deletions lib/web_push_elixir.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ defmodule WebPushElixir do
JOSE.JWT.from_map(%{
aud: URI.parse(endpoint).scheme <> "://" <> URI.parse(endpoint).host,
exp: DateTime.to_unix(DateTime.utc_now()) + 12 * 3600,
sub: System.get_env("VAPID_SUBJECT")
sub: Application.get_env(:web_push_elixir, :vapid_subject)
})

json_web_key =
Expand Down Expand Up @@ -108,8 +108,8 @@ defmodule WebPushElixir do
Returns the result of `HTTPoison.post`
"""
def send_notification(subscription, message) do
vapid_public_key = url_decode(System.get_env("VAPID_PUBLIC_KEY"))
vapid_private_key = url_decode(System.get_env("VAPID_PRIVATE_KEY"))
vapid_public_key = url_decode(Application.get_env(:web_push_elixir, :vapid_public_key))
vapid_private_key = url_decode(Application.get_env(:web_push_elixir, :vapid_private_key))

%{endpoint: endpoint, keys: %{p256dh: p256dh, auth: auth}} =
Jason.decode!(subscription, keys: :atoms)
Expand Down
6 changes: 3 additions & 3 deletions test/web_push_elixir_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ defmodule WebPushElixirTest do
vapid_subject: vapid_subject
} = Mix.Tasks.Generate.Vapid.Keys.run([])

System.put_env("VAPID_PUBLIC_KEY", vapid_public_key)
Application.put_env(:web_push_elixir, :vapid_public_key, vapid_public_key)

System.put_env("VAPID_PRIVATE_KEY", vapid_private_key)
Application.put_env(:web_push_elixir, :vapid_private_key, vapid_private_key)

System.put_env("VAPID_SUBJECT", vapid_subject)
Application.put_env(:web_push_elixir, :vapid_subject, vapid_subject)

{:ok, response} = WebPushElixir.send_notification(@subscription, "some message")

Expand Down