| 1 |
package gotify |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"fmt" |
| 6 |
"log" |
| 7 |
"net/url" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 11 |
"github.com/nicholas-fedor/shoutrrr/pkg/services/standard" |
| 12 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 13 |
) |
| 14 |
|
| 15 |
// Config holds settings for the Gotify notification service. |
| 16 |
// This struct contains all configuration parameters needed to connect to and authenticate |
| 17 |
// with a Gotify server, including connection details, authentication credentials, |
| 18 |
// notification defaults, and additional metadata. |
| 19 |
type Config struct { |
| 20 |
standard.EnumlessConfig // Embeds standard configuration functionality without enum handling |
| 21 |
|
| 22 |
Token string `desc:"Application token" required:"" url:"path2"` // Gotify application token for authentication (must be 15 chars starting with 'A') |
| 23 |
Host string `desc:"Server hostname (and optionally port)" required:"" url:"host,port"` // Gotify server hostname and optional port number |
| 24 |
Path string `desc:"Server subpath" url:"path1" optional:""` // Optional subpath for Gotify installation (e.g., "/gotify") |
| 25 |
Priority int ` default:"0" key:"priority"` // Notification priority level (-2 to 10, where higher numbers are more important; negative values have special meanings in some clients) |
| 26 |
Title string ` default:"Shoutrrr notification" key:"title"` // Default notification title when none provided |
| 27 |
DisableTLS bool ` default:"No" key:"disabletls"` // Disable TLS in URL scheme only (use HTTP instead of HTTPS) |
| 28 |
InsecureSkipVerify bool ` default:"No" key:"insecureskipverify"` // Skip TLS certificate verification (insecure, use with caution) |
| 29 |
UseHeader bool `desc:"Enable header-based authentication" default:"No" key:"useheader"` // Send token in X-Gotify-Key header instead of URL query parameter |
| 30 |
Date string ` default:"" key:"date"` // Optional custom timestamp in ISO 8601 format for the notification |
| 31 |
Extras map[string]any // Additional extras parsed from JSON - custom key-value pairs sent with notifications |
| 32 |
} |
| 33 |
|
| 34 |
// Scheme identifies this service in configuration URLs. |
| 35 |
const ( |
| 36 |
Scheme = "gotify" |
| 37 |
) |
| 38 |
|
| 39 |
// GetURL returns a URL representation of the current configuration. |
| 40 |
func (c *Config) GetURL() *url.URL { |
| 41 |
resolver := format.NewPropKeyResolver(c) |
| 42 |
|
| 43 |
return c.getURL(&resolver) |
| 44 |
} |
| 45 |
|
| 46 |
// SetURL updates the configuration from a URL representation. |
| 47 |
func (c *Config) SetURL(serviceURL *url.URL) error { |
| 48 |
resolver := format.NewPropKeyResolver(c) |
| 49 |
|
| 50 |
return c.setURL(&resolver, serviceURL) |
| 51 |
} |
| 52 |
|
| 53 |
// getURL generates a URL from the current configuration values. |
| 54 |
// This internal method constructs a URL representation of the configuration, |
| 55 |
// including all settings as query parameters and extras as JSON in the query string. |
| 56 |
// Used for serialization and URL reconstruction. |
| 57 |
// Parameters: |
| 58 |
// - resolver: Configuration resolver for building query parameters from config fields |
| 59 |
// |
| 60 |
// Returns: *url.URL containing the complete configuration as a URL. |
| 61 |
func (c *Config) getURL(resolver types.ConfigQueryResolver) *url.URL { |
| 62 |
// Build base query string from configuration fields using the resolver |
| 63 |
query := format.BuildQuery(resolver) |
| 64 |
|
| 65 |
// Handle extras serialization if present |
| 66 |
if c.Extras != nil { |
| 67 |
// Marshal extras map to JSON string |
| 68 |
extrasJSON, err := json.Marshal(c.Extras) |
| 69 |
if err != nil { |
| 70 |
// Skip adding extras when Extras cannot be serialized |
| 71 |
log.Printf("Failed to marshal Extras %v: %v, skipping extras", c.Extras, err) |
| 72 |
} else { |
| 73 |
// Append extras to query string with proper URL encoding |
| 74 |
if query != "" { |
| 75 |
query += "&" |
| 76 |
} |
| 77 |
|
| 78 |
query += "extras=" + url.QueryEscape(string(extrasJSON)) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
// Construct and return the complete URL |
| 83 |
return &url.URL{ |
| 84 |
Host: c.Host, // Server hostname and port |
| 85 |
Scheme: Scheme, // URL scheme (gotify) |
| 86 |
ForceQuery: false, // Don't force query string presence |
| 87 |
Path: c.Path + c.Token, // Path with token appended |
| 88 |
RawQuery: query, // Query parameters including extras |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
// processQueryParameters processes query parameters from URL. |
| 93 |
// This function handles both standard configuration parameters and the special 'extras' |
| 94 |
// JSON parameter, setting appropriate config fields through the resolver or direct assignment. |
| 95 |
// Parameters: |
| 96 |
// - resolver: Configuration resolver for setting standard config properties |
| 97 |
// - query: URL query parameters to process |
| 98 |
// |
| 99 |
// Returns: error if parameter parsing or setting fails. |
| 100 |
func (c *Config) processQueryParameters( |
| 101 |
resolver types.ConfigQueryResolver, |
| 102 |
query url.Values, |
| 103 |
) error { |
| 104 |
// Iterate through all query parameters |
| 105 |
for key := range query { |
| 106 |
if key == "extras" { |
| 107 |
// Special handling for extras JSON parameter |
| 108 |
if query.Get(key) != "" { |
| 109 |
// Initialize extras map |
| 110 |
c.Extras = make(map[string]any) |
| 111 |
// Parse JSON string into map |
| 112 |
if err := json.Unmarshal([]byte(query.Get(key)), &c.Extras); err != nil { |
| 113 |
return fmt.Errorf("%w", ErrExtrasParseFailed) |
| 114 |
} |
| 115 |
} |
| 116 |
} else { |
| 117 |
// Standard parameter handling through resolver |
| 118 |
if err := resolver.Set(key, query.Get(key)); err != nil { |
| 119 |
return fmt.Errorf("%w", ErrConfigPropertyFailed) |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
return nil |
| 125 |
} |
| 126 |
|
| 127 |
// setURL updates the configuration from a URL representation. |
| 128 |
// This internal method parses a URL and extracts configuration values including |
| 129 |
// host, path, token, and query parameters, populating the config struct fields. |
| 130 |
// Used for deserialization from URL format. |
| 131 |
// Parameters: |
| 132 |
// - resolver: Configuration resolver for setting config fields from query parameters |
| 133 |
// - url: The URL to parse configuration values from |
| 134 |
// |
| 135 |
// Returns: error if URL parsing or parameter processing fails. |
| 136 |
func (c *Config) setURL(resolver types.ConfigQueryResolver, serviceURL *url.URL) error { |
| 137 |
// Extract and clean the path from the URL |
| 138 |
path := serviceURL.Path |
| 139 |
if path != "" && path[len(path)-1] == '/' { |
| 140 |
path = path[:len(path)-1] // Remove trailing slash if present |
| 141 |
} |
| 142 |
|
| 143 |
// Find the last slash to separate path from token |
| 144 |
tokenIndex := strings.LastIndex(path, "/") + 1 |
| 145 |
|
| 146 |
// Extract path component (everything before the token) |
| 147 |
c.Path = path[:tokenIndex] |
| 148 |
if c.Path == "/" { |
| 149 |
c.Path = c.Path[1:] // Remove leading slash to normalize empty path |
| 150 |
} |
| 151 |
|
| 152 |
// Set host and token from URL components |
| 153 |
c.Host = serviceURL.Host |
| 154 |
c.Token = path[tokenIndex:] |
| 155 |
|
| 156 |
// Process query parameters to set remaining configuration fields |
| 157 |
if err := c.processQueryParameters(resolver, serviceURL.Query()); err != nil { |
| 158 |
return fmt.Errorf("failed to process query parameters: %w", err) |
| 159 |
} |
| 160 |
|
| 161 |
return nil |
| 162 |
} |