webutil

package
v0.0.0-...-0d80880 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 20, 2024 License: Apache-2.0 Imports: 10 Imported by: 1

Documentation

Overview

Package webutils provides utility functions.

Package webutils provides utility functions.

Package webutils provides utility functions.

Index

Constants

View Source
const MsgTemplateError = "The server is unable to display this page."

Variables

This section is empty.

Functions

func CheckAllowedMethods

func CheckAllowedMethods(w http.ResponseWriter, r *http.Request, allowed ...string) bool

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 ClientIP

func ClientIP(r *http.Request) string

ClientIP retrieves the client's IP address, preferring the X-Real-IP header.

func IsMethodOrError

func IsMethodOrError(w http.ResponseWriter, r *http.Request, method string) bool

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

func Join(elems []string, sep string) template.HTML

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

func RenderTemplateForTest(t *testing.T, tmpl *template.Template, name string, data any) string

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

func TemplateNames(tmpl *template.Template) []string

TemplateNames returns a list of all template names within the template tree.

func Templates

func Templates(pattern string) (*template.Template, error)

Templates parses templates from files matching the given pattern.

func TemplatesWithFuncs

func TemplatesWithFuncs(pattern string, funcMap template.FuncMap) (*template.Template, error)

TemplatesWithFuncs parses templates from files matching the given pattern and applies a FuncMap.

func ToTimeZone

func ToTimeZone(t time.Time, tzName string) (time.Time, error)

ToTimeZone returns time adjusted to the given timezone. If tzName is invalid, then the zero time value is returned.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL