Skip to content

Commit

Permalink
feat(gotify): add support for gotify URL path (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
piksel committed Jan 31, 2021
1 parent 9ef6db2 commit c889ea9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pkg/services/gotify/gotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func buildURL(config *Config) (string, error) {
if config.DisableTLS {
scheme = scheme[:4]
}
return fmt.Sprintf("%s://%s/message?token=%s", scheme, config.Host, token), nil
return fmt.Sprintf("%s://%s%s/message?token=%s", scheme, config.Host, config.Path, token), nil
}

// Send a notification message to Gotify
Expand Down
10 changes: 8 additions & 2 deletions pkg/services/gotify/gotify_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/containrrr/shoutrrr/pkg/format"
"github.com/containrrr/shoutrrr/pkg/types"
"net/url"
"strings"

"github.com/containrrr/shoutrrr/pkg/services/standard"
)
Expand All @@ -13,6 +14,7 @@ type Config struct {
standard.EnumlessConfig
Token string
Host string
Path string `optional:""`
Priority int `key:"priority" default:"0"`
Title string `key:"title" default:"Shoutrrr notification"`
DisableTLS bool `key:"disabletls" default:"No"`
Expand All @@ -35,14 +37,18 @@ func (config *Config) getURL(resolver types.ConfigQueryResolver) *url.URL {
Host: config.Host,
Scheme: Scheme,
ForceQuery: false,
Path: config.Token,
Path: config.Path + config.Token,
RawQuery: format.BuildQuery(resolver),
}
}

func (config *Config) setURL(resolver types.ConfigQueryResolver, url *url.URL) error {

tokenIndex := strings.LastIndex(url.Path, "/")
config.Path = url.Path[:tokenIndex]

config.Host = url.Host
config.Token = url.Path
config.Token = url.Path[tokenIndex:]

for key, vals := range url.Query() {
if err := resolver.Set(key, vals[0]); err != nil {
Expand Down
30 changes: 29 additions & 1 deletion pkg/services/gotify/gotify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ var _ = Describe("the Gotify plugin URL building and token validation functions"
})
})

When("a custom path is provided", func() {
It("should add it to the URL", func() {
config := Config{
Token: "Aaa.bbb.ccc.ddd",
Host: "my.gotify.tld",
Path: "/gotify",
}
url, err := buildURL(&config)
Expect(err).To(BeNil())
expectedURL := "https://my.gotify.tld/gotify/message?token=Aaa.bbb.ccc.ddd"
Expect(url).To(Equal(expectedURL))
})
})

When("provided a valid token", func() {
It("should return true", func() {
token := "Ahwbsdyhwwgarxd"
Expand All @@ -64,7 +78,21 @@ var _ = Describe("the Gotify plugin URL building and token validation functions"
})
Describe("creating a config", func() {
When("parsing the configuration URL", func() {
It("should be identical after de-/serialization", func() {
It("should be identical after de-/serialization (with path)", func() {
testURL := "gotify://my.gotify.tld/gotify/Aaa.bbb.ccc.ddd?disabletls=No&priority=1&title=Test title"

url, err := url.Parse(testURL)
Expect(err).NotTo(HaveOccurred(), "parsing")

config := &Config{}
err = config.SetURL(url)
Expect(err).NotTo(HaveOccurred(), "verifying")

outputURL := config.GetURL()
Expect(outputURL.String()).To(Equal(testURL))

})
It("should be identical after de-/serialization (without path)", func() {
testURL := "gotify://my.gotify.tld/Aaa.bbb.ccc.ddd?disabletls=No&priority=1&title=Test title"

url, err := url.Parse(testURL)
Expand Down

0 comments on commit c889ea9

Please sign in to comment.