Debian Codegraph @

pkg/services/standard/doc.go

raw
1 // Package standard provides standard implementations for core Shoutrrr service functionality.
2 //
3 // The standard package offers reusable components that notification services can embed
4 // to implement common functionality required by the Shoutrrr framework. Rather than
5 // implementing logging, templating, and error handling from scratch, services can embed
6 // the Standard type to get these capabilities out of the box.
7 //
8 // # Overview
9 //
10 // The package provides three main components:
11 //
12 // - Standard: A composable type that embeds Logger and Templater
13 // - Logger: Standard logging implementation for services
14 // - Templater: Template management using Go's text/template package
15 //
16 // In addition, the package provides utility types and functions for error handling
17 // and configuration management that are commonly needed across different services.
18 //
19 // # Usage
20 //
21 // Services typically embed standard.Standard in their Service struct:
22 //
23 // type Service struct {
24 // standard.Standard
25 // // service-specific fields
26 // }
27 //
28 // This gives the Service access to all logging and templating methods through
29 // composition. The Standard type implements the types.StdLogger and types.Templater
30 // interfaces required by the shoutrrr.Service interface. Its ServiceTimeout
31 // reports types.DefaultSendTimeout; override that method when a service's own
32 // limit is longer.
33 //
34 // # Key Types
35 //
36 // ## Standard
37 //
38 // The Standard type embeds both Logger and Templater, providing a convenient way
39 // to include all standard functionality in a single embed:
40 //
41 // var service Service
42 // service.Standard = standard.Standard{}
43 // service.SetLogger(logger)
44 // service.Log("Service initialized")
45 //
46 // ## Logger
47 //
48 // Logger provides methods for outputting non-fatal log information. It wraps the
49 // types.StdLogger interface and provides Log and Logf methods:
50 //
51 // logger := &standard.Logger{}
52 // logger.SetLogger(myLogger)
53 // logger.Log("Message without formatting")
54 // logger.Logf("Formatted message: %s", argument)
55 //
56 // When SetLogger is called with nil, a discard logger is used to prevent nil
57 // pointer dereferences.
58 //
59 // ## Templater
60 //
61 // Templater provides template management using Go's text/template package. Services
62 // can use it to store and retrieve templates by ID:
63 //
64 // templater := &standard.Templater{}
65 // templater.SetTemplateString("welcome", "Hello, {{.Name}}!")
66 //
67 // tpl, found := templater.GetTemplate("welcome")
68 // if found {
69 // // Use tpl.Execute() to render the template
70 // }
71 //
72 // Templates can be loaded from strings or files:
73 //
74 // err := templater.SetTemplateString("my-template", "Template content")
75 // err := templater.SetTemplateFile("my-template", "/path/to/template.txt")
76 //
77 // # Error Handling
78 //
79 // The package provides failure types and helper functions for common error scenarios:
80 //
81 // const (
82 // FailTestSetup failures.FailureID = -1 // Test setup errors
83 // FailParseURL failures.FailureID = -2 // URL parsing errors
84 // FailServiceInit failures.FailureID = -3 // Service initialization errors
85 // FailUnknown failures.FailureID = 0 // Default/unknown errors
86 // )
87 //
88 // The Failure function creates a Failure instance with a specific failure ID:
89 //
90 // fail := standard.Failure(standard.FailParseURL, err, "invalid URL format")
91 //
92 // The IsTestSetupFailure helper checks if a failure is due to test setup issues:
93 //
94 // if msg, isSetupFailure := standard.IsTestSetupFailure(fail); isSetupFailure {
95 // // Handle test setup failure
96 // }
97 //
98 // # Configuration
99 //
100 // EnumlessConfig provides a minimal implementation of types.ServiceConfig for
101 // services that don't use enumerated configuration fields:
102 //
103 // type Config struct {
104 // standard.EnumlessConfig
105 // // service-specific config fields
106 // }
107 //
108 // func (c *Config) Enums() map[string]types.EnumFormatter {
109 // return map[string]types.EnumFormatter{} // Empty - no enum fields
110 // }
111 //
112 // # Examples
113 //
114 // ## Basic Service Setup
115 //
116 // func NewService() *Service {
117 // return &Service{
118 // Standard: standard.Standard{},
119 // }
120 // }
121 //
122 // func (s *Service) Initialize(serviceURL *url.URL, logger types.StdLogger) error {
123 // s.SetLogger(logger)
124 // s.Log("Initializing service")
125 // // service-specific initialization
126 // return nil
127 // }
128 //
129 // ## Using Templates
130 //
131 // func (s *Service) Send(ctx context.Context, params *types.Params) error {
132 // // Load template if not already present
133 // if _, found := s.GetTemplate("message"); !found {
134 // s.SetTemplateString("message", "Title: {{.Title}}\n{{.Body}}")
135 // }
136 //
137 // // Render template
138 // tpl, _ := s.GetTemplate("message")
139 // var buf bytes.Buffer
140 // if err := tpl.Execute(&buf, params); err != nil {
141 // return standard.Failure(standard.FailUnknown, err)
142 // }
143 //
144 // return s.sendToProvider(buf.String())
145 // }
146 //
147 // ## Error Handling
148 //
149 // func (s *Service) parseURL(rawURL string) (*url.URL, error) {
150 // parsed, err := url.Parse(rawURL)
151 // if err != nil {
152 // return nil, standard.Failure(standard.FailParseURL, err)
153 // }
154 //
155 // if parsed.Host == "" {
156 // return nil, standard.Failure(standard.FailParseURL, errors.New("empty host"))
157 // }
158 //
159 // return parsed, nil
160 // }
161 //
162 // # Integration with Services
163 //
164 // The standard package is designed to be embedded by notification service implementations.
165 // All built-in Shoutrrr services use this pattern:
166 //
167 // - Chat services: Discord, Slack, Mattermost, Telegram, Teams, etc.
168 // - Push services: Gotify, ntfy, Pushover, Pushbullet, etc.
169 // - Email services: SMTP
170 // - Incident services: PagerDuty, OpsGenie
171 // - Specialized services: Generic, Logger, Notifiarr
172 //
173 // By embedding standard.Standard, services automatically get:
174 //
175 // - Logging via Log() and Logf() methods
176 // - Template management via GetTemplate(), SetTemplateString(), SetTemplateFile()
177 // - Integration with shoutrrr's service discovery and URL parsing
178 //
179 // This approach promotes code reuse and consistency across different notification
180 // service implementations while allowing each service to focus on its specific
181 // provider integration.
182 package standard