web_push.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //go:build !noserver
  2. package cmd
  3. import (
  4. "fmt"
  5. "github.com/SherClockHolmes/webpush-go"
  6. "github.com/urfave/cli/v2"
  7. )
  8. func init() {
  9. commands = append(commands, cmdWebPush)
  10. }
  11. var cmdWebPush = &cli.Command{
  12. Name: "web-push",
  13. Usage: "Generate keys, in the future manage web push subscriptions",
  14. UsageText: "ntfy web-push [generate-keys]",
  15. Category: categoryServer,
  16. Subcommands: []*cli.Command{
  17. {
  18. Action: generateWebPushKeys,
  19. Name: "generate-keys",
  20. Usage: "Generate VAPID keys to enable browser background push notifications",
  21. UsageText: "ntfy web-push generate-keys",
  22. Category: categoryServer,
  23. },
  24. },
  25. }
  26. func generateWebPushKeys(c *cli.Context) error {
  27. privateKey, publicKey, err := webpush.GenerateVAPIDKeys()
  28. if err != nil {
  29. return err
  30. }
  31. fmt.Fprintf(c.App.ErrWriter, `Keys generated.
  32. VAPID Public Key:
  33. %s
  34. VAPID Private Key:
  35. %s
  36. ---
  37. Add the following lines to your config file:
  38. web-push-enabled: true
  39. web-push-public-key: %s
  40. web-push-private-key: %s
  41. web-push-subscriptions-file: <filename>
  42. web-push-email-address: <email address>
  43. Look at the docs for other methods (e.g. command line flags & environment variables).
  44. You will also need to set a base-url.
  45. `, publicKey, privateKey, publicKey, privateKey)
  46. return nil
  47. }