mirror of
https://github.com/binwiederhier/ntfy.git
synced 2025-06-02 19:35:53 +02:00
WIP CLI
This commit is contained in:
parent
5639cf7a0f
commit
f266afa1de
12 changed files with 209 additions and 74 deletions
client
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue