internal

package
v2.0.0-alpha.88 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

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

func GetGlobalCORS() (enable bool, origin []string)

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 IsCookieAllowed

func IsCookieAllowed() bool

Whether cookies are allowed or not.

func Key

func Key(key string) string

Standardize keys by trim spaces and convert to lowercase.

func LoadLanguage

func LoadLanguage(tag language.Tag, trans map[string]string)

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

func LogPanic(mesg string, trace []string)

In case of panics, this function is called by [handler.HandlePanic] to log the error message and call stack trace.

func MatchPreferredLanguage

func MatchPreferredLanguage(r *http.Request) *message.Printer

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

func Segment(text string) []string

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

func SetGlobalCORS(enable bool, origin ...string)

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

func WithPanicLogger(h func(string, []string))

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.

type ParamTypes

type ParamTypes interface {
	Numbers | string | bool
}

type ParametricMessage

type ParametricMessage struct {
	Text string
	Args []any
}

Jump to

Keyboard shortcuts

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