Documentation ¶
Overview ¶
Package webutils provides utility functions.
Index ¶
- Constants
- Variables
- func CheckAllowedMethods(w http.ResponseWriter, r *http.Request, allowed ...string) bool
- func ClientIP(r *http.Request) string
- func IsMethodOrError(w http.ResponseWriter, r *http.Request, method string) bool
- func Join(values []string, sep string) template.HTML
- func MockSMTPServerConnection(conn net.Conn)
- func MockSMTPServerStart(ready chan<- bool, addr string)
- func RenderTemplateForTest(t *testing.T, tmpl *template.Template, name string, data any) string
- func RenderTemplateOrError(t *template.Template, w http.ResponseWriter, name string, data interface{}) error
- func RespondWithError(w http.ResponseWriter, code int)
- func ServeFileHandler(filePath string) http.HandlerFunc
- func SetContentType(w http.ResponseWriter, contentType string)
- func SetContentTypeHTML(w http.ResponseWriter)
- func SetContentTypeText(w http.ResponseWriter)
- func SetNoCacheHeaders(w http.ResponseWriter)
- func SliceOfStructsToCSV(w io.Writer, data interface{}) error
- func TemplateNames(t *template.Template) []string
- func Templates(pattern string) (*template.Template, error)
- func TemplatesWithFuncs(pattern string, funcMap template.FuncMap) (*template.Template, error)
- func ToTimeZone(t time.Time, name string) time.Time
- type RedactedSMTPConfig
- type SMTPConfig
Constants ¶
const MsgTemplateError = "The server is unable to display this page."
Variables ¶
Functions ¶
func CheckAllowedMethods ¶
CheckAllowedMethods validates the request's method against a list of allowed methods. It automatically supports the OPTIONS method. If the method is allowed, it returns true. If the method is not allowed, it sets the 'Allow' header, responds appropriately, and returns false. The caller is advised to halt further processing if false is returned.
func ClientIP ¶
ClientIP retrieves the client's IP address from the request. It prioritizes the X-Real-IP header value if present; otherwise, it falls back to the request's RemoteAddr.
func IsMethodOrError ¶
IsMethodOrError verifies if the HTTP request method matches the specified method. It returns true if they match. Otherwise, it sends a 405 Method Not Allowed response and returns false. The caller should stop further processing if false is returned.
func MockSMTPServerConnection ¶
MockSMTPServerConnection handles a single mock SMTP server connection.
func MockSMTPServerStart ¶
MockSMTPServerStart starts a mock SMTP server using the provided addr. The server will signal on ready when setup is complete.
func RenderTemplateForTest ¶
RenderTemplateForTest renders a specific template with the provided data and returns the result as a string. This function is intended for use in tests to verify template output.
func RenderTemplateOrError ¶
func RenderTemplateOrError(t *template.Template, w http.ResponseWriter, name string, data interface{}) error
RenderTemplateOrError executes the named template with the given data and writes the result to the provided HTTP response writer.
If an error occurs, sets HTTP response status to 500 and returns the error.
The caller must ensure no further writes are done for a non-nil error.
func RespondWithError ¶
func RespondWithError(w http.ResponseWriter, code int)
RespondWithError sends an HTTP response with the specified error code and a corresponding error message.
func ServeFileHandler ¶
func ServeFileHandler(filePath string) http.HandlerFunc
ServeFileHandler creates an HTTP handler function that serves a specified file. It verifies the file's existence and accessibility before creating the handler. Returns nil if the file does not exist or is not accessible.
func SetContentType ¶
func SetContentType(w http.ResponseWriter, contentType string)
SetContentType sets headers for client to interpret response as contentType.
func SetContentTypeHTML ¶
func SetContentTypeHTML(w http.ResponseWriter)
SetContentTypeHTML sets headers for client to interpret response as HTML.
func SetContentTypeText ¶
func SetContentTypeText(w http.ResponseWriter)
SetContentTypeText sets headers for client to interpret response as plain text.
func SetNoCacheHeaders ¶
func SetNoCacheHeaders(w http.ResponseWriter)
SetNoCacheHeaders sets headers for client to not cache the response content.
func SliceOfStructsToCSV ¶
SliceOfStructsToCSV writes a slice of structs to a CSV writer. It uses the struct field names as headers and their values as rows.
func TemplateNames ¶
TemplateNames returns the names of all templates for t.
func TemplatesWithFuncs ¶
TemplatesWithFuncs parses the templates with FuncMap.
Types ¶
type RedactedSMTPConfig ¶
type RedactedSMTPConfig SMTPConfig
RedactedSMTPConfig is copy of SMTPConfig that doesn't expose sensitve fields.
type SMTPConfig ¶
type SMTPConfig struct { Host string `required:"true"` // Host address. Port string `required:"true"` // Port number. User string `required:"true"` // Server username. Password string `required:"true"` // Server password. }
SMTPConfig holds SMTP server settings for email functionality.
func (SMTPConfig) MarshalJSON ¶
func (s SMTPConfig) MarshalJSON() ([]byte, error)
MarshalJSON customizes JSON marshalling to redact sensitive data.
func (SMTPConfig) SendMessage ¶
func (s SMTPConfig) SendMessage(from string, to []string, subject, body string) error
SendMessage sends an email using the values provided.
func (SMTPConfig) String ¶
func (s SMTPConfig) String() string
String returns a string representation of s with sensitive data redacted.
func (SMTPConfig) Valid ¶
func (s SMTPConfig) Valid() bool
Valid checks s and returns true if required fields are non-empty.