Documentation ¶
Overview ¶
Package webutils provides utility functions.
Package webutils provides utility functions.
Package webutils provides utility functions.
Index ¶
- Constants
- 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(elems []string, sep string) template.HTML
- func RenderTemplateForTest(t *testing.T, tmpl *template.Template, name string, data any) string
- func RenderTemplateOrError(tmpl *template.Template, w http.ResponseWriter, name string, data interface{}) error
- func RespondWithError(w http.ResponseWriter, code int)
- func SetContentType(w http.ResponseWriter, contentType string)
- func SetContentTypeHTML(w http.ResponseWriter)
- func SetContentTypeText(w http.ResponseWriter)
- func SetHeaders(w http.ResponseWriter, headers map[string]string)
- func SetNoCacheHeaders(w http.ResponseWriter)
- func TemplateNames(tmpl *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, tzName string) (time.Time, error)
Constants ¶
const MsgTemplateError = "The server is unable to display this page."
Variables ¶
This section is empty.
Functions ¶
func CheckAllowedMethods ¶
CheckAllowedMethods validates the request's method against a list of allowed methods, automatically including the OPTIONS method. It sets the 'Allow' header and sends appropriate responses. The caller should stop further processing if false is returned.
func IsMethodOrError ¶
IsMethodOrError checks if the HTTP request method matches the specified method and 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 Join ¶
Join concatenates the elements of a []string into a single string, separated by sep. The result is returned as template.HTML, which should not include user-controlled input without escaping.
func RenderTemplateForTest ¶
RenderTemplateForTest executes a template with data for testing purposes, returning the output as a string.
func RenderTemplateOrError ¶
func RenderTemplateOrError(tmpl *template.Template, w http.ResponseWriter, name string, data interface{}) error
RenderTemplateOrError attempts to render a named template with data, handling errors by responding with HTTP 500. 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 SetContentType ¶
func SetContentType(w http.ResponseWriter, contentType string)
SetContentType sets the 'Content-Type' and associated security headers.
func SetContentTypeHTML ¶
func SetContentTypeHTML(w http.ResponseWriter)
SetContentTypeHTML sets HTTP headers for HTML responses.
func SetContentTypeText ¶
func SetContentTypeText(w http.ResponseWriter)
SetContentTypeText sets HTTP headers for plain text responses.
func SetHeaders ¶
func SetHeaders(w http.ResponseWriter, headers map[string]string)
SetHeaders applies headers to the HTTP response.
func SetNoCacheHeaders ¶
func SetNoCacheHeaders(w http.ResponseWriter)
SetNoCacheHeaders instructs the client not to cache the response.
func TemplateNames ¶
TemplateNames returns a list of all template names within the template tree.
func TemplatesWithFuncs ¶
TemplatesWithFuncs parses templates from files matching the given pattern and applies a FuncMap.
Types ¶
This section is empty.