webpush.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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: "webpush",
  13. Usage: "Generate keys, in the future manage web push subscriptions",
  14. UsageText: "ntfy webpush [keys]",
  15. Category: categoryServer,
  16. Subcommands: []*cli.Command{
  17. {
  18. Action: generateWebPushKeys,
  19. Name: "keys",
  20. Usage: "Generate VAPID keys to enable browser background push notifications",
  21. UsageText: "ntfy webpush 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. _, err = fmt.Fprintf(c.App.ErrWriter, `Web Push keys generated. Add the following lines to your config file:
  32. web-push-public-key: %s
  33. web-push-private-key: %s
  34. web-push-file: /var/cache/ntfy/webpush.db # or similar
  35. web-push-email-address: <email address>
  36. See https://ntfy.sh/docs/config/#web-push for details.
  37. `, publicKey, privateKey)
  38. return err
  39. }