internal

package
v2.0.0-alpha.113 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2025 License: MIT Imports: 15 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 FixedReplyFields

func FixedReplyFields(args map[string]any)

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 MapReply

func MapReply(code int, data any, mesg string) map[string]any

func MatchPreferredLanguage

func MatchPreferredLanguage(r *http.Request) language.Tag

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 language.Tag for creating the message.Printer.

func Num

func Num[T Numbers](v any) (T, error)

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 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 JSONMarshallerOption

type JSONMarshallerOption struct {
	// contains filtered or unexported fields
}

func JSONMarshaller

func JSONMarshaller() *JSONMarshallerOption

func (*JSONMarshallerOption) CodeAs

Sets the JSON key for Code property of the reply, which by default is "code".

func (*JSONMarshallerOption) DataAs

Sets the JSON key for Data property of the reply, which by default is "data". If this value is set to blank string, reply will not contain the Data property.

func (*JSONMarshallerOption) MesgAs

Sets the JSON key for Mesg property of the reply, which by default is "mesg". If this value is set to blank string, reply will not contain the Mesg property.

type Numbers

type Numbers interface {
	ints | uints | floats
}

type PanicHandler

type PanicHandler func(http.ResponseWriter, any) *Reply

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
}

type Reply

type Reply struct {
	Data any

	Code int
	Raw  bool
	// contains filtered or unexported fields
}

func ApiPanicHandler

func ApiPanicHandler(w http.ResponseWriter, err any) *Reply

A PanicHandler function that handles panics by sending a JSON-formatted error response conforming to the HAP specification.

func RawPanicHandler

func RawPanicHandler(w http.ResponseWriter, err any) *Reply

A PanicHandler function that handles panics by sending a plain text error response.

func (*Reply) Emit

func (r *Reply) Emit(w http.ResponseWriter)

func (*Reply) Error

func (r *Reply) Error() string

func (*Reply) IsRaw

func (r *Reply) IsRaw() bool

func (*Reply) MarshalJSON

func (r *Reply) MarshalJSON() ([]byte, error)

func (Reply) Mesg

func (r Reply) Mesg() string

func (*Reply) MimeType

func (r *Reply) MimeType() string

func (*Reply) String

func (r *Reply) String() string

func (*Reply) UnmarshalJSON

func (r *Reply) UnmarshalJSON(data []byte) (err error)

func (*Reply) WithData

func (r *Reply) WithData(data any) *Reply

func (*Reply) WithHelp

func (r *Reply) WithHelp(mesg string, args ...any) *Reply

func (*Reply) WithLocalizer

func (r *Reply) WithLocalizer(mp *message.Printer) *Reply

func (*Reply) WithMimeType

func (r *Reply) WithMimeType(mime string) *Reply

only applicable to raw Reply

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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