Documentation ¶
Index ¶
- func AllowCookie(yesno bool)
- func ApiPanicHandler(w http.ResponseWriter, err any)
- func GetGlobalCORS() (enable bool, origin []string)
- func IsCookieAllowed() bool
- func Key(key string) string
- func LoadLanguage(tag language.Tag, trans map[string]string)
- func LogPanic(mesg string, trace []string)
- func MatchPreferredLanguage(r *http.Request) *message.Printer
- func RawPanicHandler(w http.ResponseWriter, err any)
- func Segment(text string) []string
- func SetGlobalCORS(enable bool, origin ...string)
- func WithLangSpecifier(lang string)
- func WithPanicLogger(h func(string, []string))
- type Numbers
- type PanicHandler
- type ParamTypes
- type ParametricMessage
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AllowCookie ¶
func AllowCookie(yesno bool)
For security reason, cookie is not allowed by default. Use this function to enable cookies. See "Arguments" section in the HAP specification for details.
func ApiPanicHandler ¶
func ApiPanicHandler(w http.ResponseWriter, err any)
A PanicHandler function that handles panics by sending a JSON-formatted error response conforming to the HAP specification.
func GetGlobalCORS ¶
Returns global CORS status, where `enable` indicates whether CORS is allowed or not, and `origin` is a slice of allowed domain names which may appear in the Access-Control-Allow-Origin header.
Returns the global CORS (Cross-Origin Resource Sharing) settings.
- `enable` indicates whether CORS is globally enabled.
- `origin` is a slice of allowed domain names that may appear in the Access-Control-Allow-Origin header (i.e. permitted).
HAP may control CORS either globally or per API, see [api.AllowCORS] for details.
func LoadLanguage ¶
Sets up the language settings for localization.
- `tag` specifies the language tag for which translations are being loaded.
- `trans` is a map of translation key-value pairs where the key is the translation identifier and the value is the translated string.
If this function is called multiple times with the same `tag`, the translations for that tag are merged. This means that existing translations are preserved, and any new translations will overwrite the older ones for the same keys.
func LogPanic ¶
In case of panics, this function is called by [handler.HandlePanic] to log the error message and call stack trace.
func MatchPreferredLanguage ¶
Internally called by the framework to obtain an appropriate message.Printer based on the request's language settings.
- If a language specifier is set using WithLangSpecifier, it takes precedence.
- If no language specifier is set, the function checks the Accept-Language header of the request.
- If no matching language is found via the specifier or Accept-Language header, the default language (English) is used.
The function returns a message.Printer configured for the chosen language.
func RawPanicHandler ¶
func RawPanicHandler(w http.ResponseWriter, err any)
A PanicHandler function that handles panics by sending a plain text error response.
func Segment ¶
Segments a string into paragraphs based on consecutive newline characters (\n). The resulting paragraphs are returned as a slice of strings.
The function handles cases where lines have varying amounts of leading whitespace by removing it consistently from all lines in a paragraph.
func SetGlobalCORS ¶
Set global CORS switch. If `enable` is set to true and `origin` is nil, all origins are allowed, i.e. the Access-Control-Allow-Origin header is set to "*".
func WithLangSpecifier ¶
func WithLangSpecifier(lang string)
Sets the language specifier, which is a URL query parameter used to specify the requested language for responses.
For example, if `lang` is set to 'language', a request like:
GET http://my.hap.site/api/endpoint?language=fr
will request a response in French. See MatchPreferredLanguage for more information on how language preferences are matched.
func WithPanicLogger ¶
Sets a custom panic logger function. Parameters of the provided function `h` should be:
- The first parameter is the error message.
- The second parameter is a slice of strings representing the stack trace.
By default, the panic logger outputs to stderr. This function allows you to specify a custom logger to handle and log panic-related information.
Types ¶
type Numbers ¶
type Numbers interface { ints | uints | floats }
type PanicHandler ¶
type PanicHandler func(http.ResponseWriter, any)
Type alias for a function that handles panics in HTTP requests.
- `http.ResponseWriter` is used to send the HTTP response.
- `any` represents the panic value or error that occurred.
A PanicHandler function can be used to manage and respond to panics, allowing for custom error handling and response generation in case of runtime errors during HTTP request processing.