This commit is contained in:
Philipp Heckel 2021-12-18 14:43:27 -05:00
parent 5639cf7a0f
commit f266afa1de
12 changed files with 209 additions and 74 deletions

View file

@ -12,10 +12,6 @@ import (
"time"
)
const (
DefaultBaseURL = "https://ntfy.sh"
)
const (
MessageEvent = "message"
KeepaliveEvent = "keepalive"
@ -23,8 +19,8 @@ const (
)
type Client struct {
BaseURL string
Messages chan *Message
config *Config
subscriptions map[string]*subscription
mu sync.Mutex
}
@ -34,7 +30,6 @@ type Message struct {
Event string
Time int64
Topic string
BaseURL string
TopicURL string
Message string
Title string
@ -47,11 +42,10 @@ type subscription struct {
cancel context.CancelFunc
}
var DefaultClient = New()
func New() *Client {
func New(config *Config) *Client {
return &Client{
Messages: make(chan *Message),
config: config,
subscriptions: make(map[string]*subscription),
}
}
@ -73,11 +67,12 @@ func (c *Client) Publish(topicURL, message string, options ...PublishOption) err
return err
}
func (c *Client) Poll(topicURL string, options ...SubscribeOption) ([]*Message, error) {
func (c *Client) Poll(topic string, options ...SubscribeOption) ([]*Message, error) {
ctx := context.Background()
messages := make([]*Message, 0)
msgChan := make(chan *Message)
errChan := make(chan error)
topicURL := c.expandTopicURL(topic)
go func() {
err := performSubscribeRequest(ctx, msgChan, topicURL, options...)
close(msgChan)
@ -89,20 +84,23 @@ func (c *Client) Poll(topicURL string, options ...SubscribeOption) ([]*Message,
return messages, <-errChan
}
func (c *Client) Subscribe(topicURL string, options ...SubscribeOption) {
func (c *Client) Subscribe(topic string, options ...SubscribeOption) string {
c.mu.Lock()
defer c.mu.Unlock()
topicURL := c.expandTopicURL(topic)
if _, ok := c.subscriptions[topicURL]; ok {
return
return topicURL
}
ctx, cancel := context.WithCancel(context.Background())
c.subscriptions[topicURL] = &subscription{cancel}
go handleSubscribeConnLoop(ctx, c.Messages, topicURL, options...)
return topicURL
}
func (c *Client) Unsubscribe(topicURL string) {
func (c *Client) Unsubscribe(topic string) {
c.mu.Lock()
defer c.mu.Unlock()
topicURL := c.expandTopicURL(topic)
sub, ok := c.subscriptions[topicURL]
if !ok {
return
@ -111,6 +109,15 @@ func (c *Client) Unsubscribe(topicURL string) {
return
}
func (c *Client) expandTopicURL(topic string) string {
if strings.HasPrefix(topic, "http://") || strings.HasPrefix(topic, "https://") {
return topic
} else if strings.Contains(topic, "/") {
return fmt.Sprintf("https://%s", topic)
}
return fmt.Sprintf("%s/%s", c.config.DefaultHost, topic)
}
func handleSubscribeConnLoop(ctx context.Context, msgChan chan *Message, topicURL string, options ...SubscribeOption) {
for {
if err := performSubscribeRequest(ctx, msgChan, topicURL, options...); err != nil {
@ -147,7 +154,6 @@ func performSubscribeRequest(ctx context.Context, msgChan chan *Message, topicUR
if err := json.NewDecoder(strings.NewReader(line)).Decode(&m); err != nil {
return err
}
m.BaseURL = strings.TrimSuffix(topicURL, "/"+m.Topic) // FIXME hack!
m.TopicURL = topicURL
m.Raw = line
msgChan <- m

18
client/client.yml Normal file
View file

@ -0,0 +1,18 @@
# ntfy client config file
# Base URL used to expand short topic names in the "ntfy publish" and "ntfy subscribe" commands.
# If you self-host a ntfy server, you'll likely want to change this.
#
# default-host: https://ntfy.sh
# Subscriptions to topics and their actions. This option is only used by the "ntfy subscribe --from-config"
# command.
#
# Here's a (hopefully self-explanatory) example:
# subscribe:
# - topic: mytopic
# exec: /usr/local/bin/mytopic-triggered.sh
# - topic: myserver.com/anothertopic
# exec: 'echo "$message"'
#
# subscribe:

20
client/config.go Normal file
View file

@ -0,0 +1,20 @@
package client
const (
DefaultBaseURL = "https://ntfy.sh"
)
type Config struct {
DefaultHost string
Subscribe []struct {
Topic string
Exec string
}
}
func NewConfig() *Config {
return &Config{
DefaultHost: DefaultBaseURL,
Subscribe: nil,
}
}