Documentation
¶
Index ¶
- func AllowCookie(yesno bool)
- func FixedReplyFields(args map[string]any)
- func IsCookieAllowed() bool
- func Key(key string) string
- func LoadLanguage(tag language.Tag, trans map[string]string)
- func LogPanic(mesg string, trace []string)
- func MapReply(code int, data any, mesg string) map[string]any
- func MatchPreferredLanguage(r *http.Request) language.Tag
- func Num[T Numbers](v any) (T, error)
- func Segment(text string) []string
- func WithLangSpecifier(lang string)
- func WithPanicLogger(h func(string, []string))
- type JSONMarshallerOption
- type Numbers
- type PanicHandler
- type ParamTypes
- type ParametricMessage
- type Reply
- func (r *Reply) Emit(w http.ResponseWriter)
- func (r *Reply) Error() string
- func (r *Reply) IsRaw() bool
- func (r *Reply) MarshalJSON() ([]byte, error)
- func (r Reply) Mesg() string
- func (r *Reply) MimeType() string
- func (r *Reply) String() string
- func (r *Reply) UnmarshalJSON(data []byte) (err error)
- func (r *Reply) WithData(data any) *Reply
- func (r *Reply) WithHelp(mesg string, args ...any) *Reply
- func (r *Reply) WithLocalizer(mp *message.Printer) *Reply
- func (r *Reply) WithMimeType(mime string) *Reply
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 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 language.Tag for creating the message.Printer.
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 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 JSONMarshallerOption ¶
type JSONMarshallerOption struct {
// contains filtered or unexported fields
}
func JSONMarshaller ¶
func JSONMarshaller() *JSONMarshallerOption
func (*JSONMarshallerOption) CodeAs ¶
func (jo *JSONMarshallerOption) CodeAs(code string) *JSONMarshallerOption
Sets the JSON key for Code property of the reply, which by default is "code".
func (*JSONMarshallerOption) DataAs ¶
func (jo *JSONMarshallerOption) DataAs(data string) *JSONMarshallerOption
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 ¶
func (jo *JSONMarshallerOption) MesgAs(mesg string) *JSONMarshallerOption
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 Reply ¶
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)