Documentation
¶
Index ¶
- Constants
- Variables
- func CorsMiddleware(options *Cors, ctx *HttpContext, next func(error))
- func MiddlewareWrapper(options *Cors) func(*HttpContext, func(error))
- type Buffer
- type BufferInterface
- func NewBytesBuffer(buf []byte) BufferInterface
- func NewBytesBufferReader(r io.Reader) (BufferInterface, error)
- func NewBytesBufferString(s string) BufferInterface
- func NewStringBuffer(buf []byte) BufferInterface
- func NewStringBufferReader(r io.Reader) (BufferInterface, error)
- func NewStringBufferString(s string) BufferInterface
- type BytesBuffer
- type Callable
- type CodeMessage
- type Cors
- type ErrorMessage
- type EventEmitter
- type EventName
- type Events
- type HttpCompression
- type HttpContext
- func (c *HttpContext) Context() context.Context
- func (c *HttpContext) Done() <-chan Void
- func (c *HttpContext) Flush()
- func (c *HttpContext) Get(key string, _default ...string) string
- func (c *HttpContext) GetHost() (string, error)
- func (c *HttpContext) GetMethod() string
- func (c *HttpContext) GetPathInfo() string
- func (c *HttpContext) GetStatusCode() int
- func (c *HttpContext) Gets(key string, _default ...[]string) []string
- func (c *HttpContext) Headers() *utils.ParameterBag
- func (c *HttpContext) IsDone() bool
- func (c *HttpContext) Method() string
- func (c *HttpContext) Path() string
- func (c *HttpContext) Query() *utils.ParameterBag
- func (c *HttpContext) Request() *http.Request
- func (c *HttpContext) Response() http.ResponseWriter
- func (c *HttpContext) Secure() bool
- func (c *HttpContext) SetStatusCode(statusCode int)
- func (c *HttpContext) UserAgent() string
- func (c *HttpContext) Write(wb []byte) (int, error)
- type HttpServer
- func (s *HttpServer) Close(fn func(error)) (err error)
- func (s *HttpServer) Listen(addr string, fn Callable) *http.Server
- func (s *HttpServer) ListenHTTP3TLS(addr string, certFile string, keyFile string, quicConfig *quic.Config, ...) *http3.Server
- func (s *HttpServer) ListenTLS(addr string, certFile string, keyFile string, fn Callable) *http.Server
- func (s *HttpServer) ListenWebTransportTLS(addr string, certFile string, keyFile string, quicConfig *quic.Config, ...) *webtransport.Server
- type Kv
- type Listener
- type Map
- func (m *Map[TKey, TValue]) Clear()
- func (m *Map[TKey, TValue]) CompareAndDelete(key TKey, old TValue) (deleted bool)
- func (m *Map[TKey, TValue]) CompareAndSwap(key TKey, old TValue, new TValue) (swapped bool)
- func (m *Map[TKey, TValue]) Delete(key TKey)
- func (m *Map[TKey, TValue]) Keys() (keys []TKey)
- func (m *Map[TKey, TValue]) Len() (n int)
- func (m *Map[TKey, TValue]) Load(key TKey) (value TValue, ok bool)
- func (m *Map[TKey, TValue]) LoadAndDelete(key TKey) (value TValue, loaded bool)
- func (m *Map[TKey, TValue]) LoadOrStore(key TKey, value TValue) (actual TValue, loaded bool)
- func (m *Map[TKey, TValue]) Range(f func(key TKey, value TValue) bool)
- func (m *Map[TKey, TValue]) Store(key TKey, value TValue)
- func (m *Map[TKey, TValue]) Swap(key TKey, value TValue) (previous TValue, loaded bool)
- func (m *Map[TKey, TValue]) Values() (values []TValue)
- type PerMessageDeflate
- type ServeMux
- func (mux *ServeMux) Handle(pattern string, handler http.Handler)
- func (mux *ServeMux) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
- func (mux *ServeMux) Handler(r *http.Request) (h http.Handler, pattern string)
- func (mux *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request)
- type Set
- func (s *Set[KType]) Add(keys ...KType) bool
- func (s *Set[KType]) All() map[KType]Void
- func (s *Set[KType]) Clear() bool
- func (s *Set[KType]) Delete(keys ...KType) bool
- func (s *Set[KType]) Has(key KType) bool
- func (s *Set[KType]) Keys() []KType
- func (s *Set[KType]) Len() int
- func (s *Set[KType]) MarshalJSON() ([]byte, error)
- func (s *Set[KType]) MarshalMsgpack() ([]byte, error)
- func (s *Set[KType]) UnmarshalJSON(data []byte) error
- func (s *Set[KType]) UnmarshalMsgpack(data []byte) error
- type Slice
- func (s *Slice[T]) All() []T
- func (s *Slice[T]) AllAndClear() []T
- func (s *Slice[T]) Clear()
- func (s *Slice[T]) DoRead(op func([]T))
- func (s *Slice[T]) DoWrite(op func([]T) []T)
- func (s *Slice[T]) Filter(condition func(T) bool) (filtered []T)
- func (s *Slice[T]) FindIndex(condition func(T) bool) int
- func (s *Slice[T]) Get(index int) (element T, err error)
- func (s *Slice[T]) Len() int
- func (s *Slice[T]) Pop() (element T, err error)
- func (s *Slice[T]) Push(elements ...T) int
- func (s *Slice[T]) Range(f func(T, int) bool, reverse ...bool)
- func (s *Slice[T]) RangeAndSplice(f func(T, int) (bool, int, int, []T), reverse ...bool) ([]T, error)
- func (s *Slice[T]) Remove(condition func(T) bool)
- func (s *Slice[T]) RemoveAll(condition func(T) bool)
- func (s *Slice[T]) Replace(elements []T)
- func (s *Slice[T]) Set(index int, element T) error
- func (s *Slice[T]) Shift() (element T, err error)
- func (s *Slice[T]) Slice(start, end int) ([]T, error)
- func (s *Slice[T]) Splice(start, deleteCount int, insert ...T) ([]T, error)
- func (s *Slice[T]) Unshift(elements ...T) int
- type StringBuffer
- type Void
- type WebSocketConn
- type WebTransportConn
Examples ¶
Constants ¶
const ( // Version current version number EventVersion = "0.0.3" // DefaultMaxListeners is the number of max listeners per event // default EventEmitters will print a warning if more than x listeners are // added to it. This is a useful default which helps finding memory leaks. // Defaults to 0, which means unlimited EventDefaultMaxListeners = 0 )
Variables ¶
var ( ErrSliceEmpty = errors.New("slice is empty") ErrIndexOutOfBounds = errors.New("index out of bounds") ErrInvalidSliceRange = errors.New("invalid slice range") )
Define custom error types
Functions ¶
func CorsMiddleware ¶ added in v2.0.1
func CorsMiddleware(options *Cors, ctx *HttpContext, next func(error))
func MiddlewareWrapper ¶
func MiddlewareWrapper(options *Cors) func(*HttpContext, func(error))
Types ¶
type Buffer ¶ added in v2.3.2
Buffer types alias for simplicity
func NewBufferString ¶ added in v2.3.2
NewBufferString creates a new Buffer from a string.
type BufferInterface ¶ added in v2.3.2
type BufferInterface = types.BufferInterface
Buffer types alias for simplicity
func NewBytesBuffer ¶ added in v2.3.2
func NewBytesBuffer(buf []byte) BufferInterface
NewBytesBuffer creates a new BytesBuffer from a byte slice.
func NewBytesBufferReader ¶ added in v2.3.2
func NewBytesBufferReader(r io.Reader) (BufferInterface, error)
NewBytesBufferReader creates a new BytesBuffer from an io.Reader.
func NewBytesBufferString ¶ added in v2.3.2
func NewBytesBufferString(s string) BufferInterface
NewBytesBufferString creates a new BytesBuffer from a string.
func NewStringBuffer ¶ added in v2.3.2
func NewStringBuffer(buf []byte) BufferInterface
NewStringBuffer creates a new StringBuffer from a byte slice.
func NewStringBufferReader ¶ added in v2.3.2
func NewStringBufferReader(r io.Reader) (BufferInterface, error)
NewStringBufferReader creates a new StringBuffer from an io.Reader.
func NewStringBufferString ¶ added in v2.3.2
func NewStringBufferString(s string) BufferInterface
NewStringBufferString creates a new StringBuffer from a string.
type BytesBuffer ¶ added in v2.3.2
type BytesBuffer = types.BytesBuffer
Buffer types alias for simplicity
type CodeMessage ¶
type Cors ¶
type Cors struct { // Supported types: string, []any, *regexp.Regexp, bool Origin any // Supported types: string, []string Methods any // Supported types: nil, string, []string AllowedHeaders any // Supported types: nil, string, []string Headers any // Supported types: string, []string ExposedHeaders any MaxAge string Credentials bool PreflightContinue bool OptionsSuccessStatus int }
type ErrorMessage ¶
type ErrorMessage struct { *CodeMessage Req *HttpContext `json:"req,omitempty" msgpack:"req,omitempty"` Context map[string]any `json:"context,omitempty" msgpack:"context,omitempty"` }
type EventEmitter ¶ added in v2.3.0
type EventEmitter interface { // AddListener is an alias for .On(eventName, listener). AddListener(EventName, ...Listener) error // Emit fires a particular event, // Synchronously calls each of the listeners registered for the event named // eventName, in the order they were registered, // passing the supplied arguments to each. Emit(EventName, ...any) // EventNames returns an array listing the events for which the emitter has registered listeners. // The values in the array will be strings. EventNames() []EventName // GetMaxListeners returns the max listeners for this emmiter // see SetMaxListeners GetMaxListeners() uint // ListenerCount returns the length of all registered listeners to a particular event ListenerCount(EventName) int // Listeners returns a copy of the array of listeners for the event named eventName. Listeners(EventName) []Listener // On registers a particular listener for an event, func receiver parameter(s) is/are optional On(EventName, ...Listener) error // Once adds a one time listener function for the event named eventName. // The next time eventName is triggered, this listener is removed and then invoked. Once(EventName, ...Listener) error // RemoveAllListeners removes all listeners, or those of the specified eventName. // Note that it will remove the event itself. // Returns an indicator if event and listeners were found before the remove. RemoveAllListeners(EventName) bool // RemoveListener removes given listener from the event named eventName. // Returns an indicator whether listener was removed RemoveListener(EventName, Listener) bool // Clear removes all events and all listeners, restores Events to an empty value Clear() // SetMaxListeners obviously this function allows the MaxListeners // to be decrease or increase. Set to zero for unlimited SetMaxListeners(uint) // Len returns the length of all registered events Len() int }
EventEmitter is the message/or/event manager
func NewEventEmitter ¶ added in v2.3.0
func NewEventEmitter() EventEmitter
New returns a new, empty, EventEmitter
type EventName ¶ added in v2.3.0
type EventName string
EventName is just a type of string, it's the event name
type Events ¶ added in v2.3.0
Events the type for registered listeners, it's just a map[string][]func(...any)
Example ¶
// regiter our events to the default event emmiter for evt, listeners := range testEvents { _event.On(evt, listeners...) } user := "user1" room := "room1" createUser(user) joinUserTo(user, room) leaveFromRoom(user, room)
Output: A new User just created! A new User just created, *from second event listener user1 joined to room: room1 user1 left from the room: room1
func (Events) CopyTo ¶ added in v2.3.0
func (e Events) CopyTo(emitter EventEmitter)
CopyTo copies the event listeners to an EventEmitter
type HttpCompression ¶
type HttpCompression struct {
Threshold int `json:"threshold,omitempty" msgpack:"threshold,omitempty"`
}
type HttpContext ¶
type HttpContext struct { EventEmitter Websocket *WebSocketConn WebTransport *WebTransportConn Cleanup Callable ResponseHeaders *utils.ParameterBag // contains filtered or unexported fields }
func NewHttpContext ¶
func NewHttpContext(w http.ResponseWriter, r *http.Request) *HttpContext
func (*HttpContext) Context ¶
func (c *HttpContext) Context() context.Context
func (*HttpContext) Done ¶
func (c *HttpContext) Done() <-chan Void
func (*HttpContext) Flush ¶
func (c *HttpContext) Flush()
func (*HttpContext) GetHost ¶
func (c *HttpContext) GetHost() (string, error)
func (*HttpContext) GetMethod ¶
func (c *HttpContext) GetMethod() string
func (*HttpContext) GetPathInfo ¶
func (c *HttpContext) GetPathInfo() string
func (*HttpContext) GetStatusCode ¶
func (c *HttpContext) GetStatusCode() int
func (*HttpContext) Headers ¶
func (c *HttpContext) Headers() *utils.ParameterBag
func (*HttpContext) IsDone ¶
func (c *HttpContext) IsDone() bool
func (*HttpContext) Method ¶
func (c *HttpContext) Method() string
func (*HttpContext) Path ¶
func (c *HttpContext) Path() string
func (*HttpContext) Query ¶
func (c *HttpContext) Query() *utils.ParameterBag
func (*HttpContext) Request ¶
func (c *HttpContext) Request() *http.Request
func (*HttpContext) Response ¶
func (c *HttpContext) Response() http.ResponseWriter
func (*HttpContext) Secure ¶
func (c *HttpContext) Secure() bool
func (*HttpContext) SetStatusCode ¶
func (c *HttpContext) SetStatusCode(statusCode int)
func (*HttpContext) UserAgent ¶
func (c *HttpContext) UserAgent() string
type HttpServer ¶
type HttpServer struct { EventEmitter *ServeMux // contains filtered or unexported fields }
func CreateServer
deprecated
func CreateServer(defaultHandler http.Handler) *HttpServer
Deprecated: this method will be removed in the next major release, please use NewWebServer instead.
func NewWebServer ¶
func NewWebServer(defaultHandler http.Handler) *HttpServer
func (*HttpServer) Close ¶
func (s *HttpServer) Close(fn func(error)) (err error)
func (*HttpServer) ListenHTTP3TLS ¶
func (*HttpServer) ListenWebTransportTLS ¶
func (s *HttpServer) ListenWebTransportTLS(addr string, certFile string, keyFile string, quicConfig *quic.Config, fn Callable) *webtransport.Server
type Listener ¶ added in v2.3.0
type Listener func(...any)
Listener is the type of a Listener, it's a func which receives any,optional, arguments from the caller/emmiter
type Map ¶
type Map[TKey comparable, TValue any] struct { // contains filtered or unexported fields }
Map is like a Go map[any]any but is safe for concurrent use by multiple goroutines without additional locking or coordination. Loads, stores, and deletes run in amortized constant time.
The Map type is specialized. Most code should use a plain Go map instead, with separate locking or coordination, for better type safety and to make it easier to maintain other invariants along with the map content.
The Map type is optimized for two common use cases: (1) when the entry for a given key is only ever written once but read many times, as in caches that only grow, or (2) when multiple goroutines read, write, and overwrite entries for disjoint sets of keys. In these two cases, use of a Map may significantly reduce lock contention compared to a Go map paired with a separate [Mutex] or [RWMutex].
The zero Map is empty and ready for use. A Map must not be copied after first use.
In the terminology of the Go memory model, Map arranges that a write operation “synchronizes before” any read operation that observes the effect of the write, where read and write operations are defined as follows. Map.Load, Map.LoadAndDelete, Map.LoadOrStore, Map.Swap, Map.CompareAndSwap, and Map.CompareAndDelete are read operations; Map.Delete, Map.LoadAndDelete, Map.Store, and Map.Swap are write operations; Map.LoadOrStore is a write operation when it returns loaded set to false; Map.CompareAndSwap is a write operation when it returns swapped set to true; and Map.CompareAndDelete is a write operation when it returns deleted set to true.
func (*Map[TKey, TValue]) Clear ¶ added in v2.0.3
func (m *Map[TKey, TValue]) Clear()
Clear deletes all the entries, resulting in an empty Map.
func (*Map[TKey, TValue]) CompareAndDelete ¶
CompareAndDelete deletes the entry for key if its value is equal to old. The old value must be of a comparable type.
If there is no current value for key in the map, CompareAndDelete returns false (even if the old value is the nil interface value).
func (*Map[TKey, TValue]) CompareAndSwap ¶
CompareAndSwap swaps the old and new values for key if the value stored in the map is equal to old. The old value must be of a comparable type.
func (*Map[TKey, TValue]) Delete ¶
func (m *Map[TKey, TValue]) Delete(key TKey)
Delete deletes the value for a key.
func (*Map[TKey, TValue]) Load ¶
Load returns the value stored in the map for a key, or nil if no value is present. The ok result indicates whether value was found in the map.
func (*Map[TKey, TValue]) LoadAndDelete ¶
LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present.
func (*Map[TKey, TValue]) LoadOrStore ¶
LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.
func (*Map[TKey, TValue]) Range ¶
Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.
Range does not necessarily correspond to any consistent snapshot of the Map's contents: no key will be visited more than once, but if the value for any key is stored or deleted concurrently (including by f), Range may reflect any mapping for that key from any point during the Range call. Range does not block other methods on the receiver; even f itself may call any method on m.
Range may be O(N) with the number of elements in the map even if f returns false after a constant number of calls.
func (*Map[TKey, TValue]) Store ¶
func (m *Map[TKey, TValue]) Store(key TKey, value TValue)
Store sets the value for a key.
type PerMessageDeflate ¶
type PerMessageDeflate struct {
Threshold int `json:"threshold,omitempty" msgpack:"threshold,omitempty"`
}
type ServeMux ¶
type ServeMux struct { DefaultHandler http.Handler // Default Handler // contains filtered or unexported fields }
func NewServeMux ¶
NewServeMux allocates and returns a new ServeMux.
func (*ServeMux) Handle ¶
Handle registers the handler for the given pattern. If a handler already exists for pattern, Handle panics.
func (*ServeMux) HandleFunc ¶
HandleFunc registers the handler function for the given pattern.
func (*ServeMux) Handler ¶
Handler returns the handler to use for the given request, consulting r.Method, r.Host, and r.URL.Path. It always returns a non-nil handler. If the path is not in its canonical form, the handler will be an internally-generated handler that redirects to the canonical path. If the host contains a port, it is ignored when matching handlers.
The path and host are used unchanged for CONNECT requests.
Handler also returns the registered pattern that matches the request or, in the case of internally-generated redirects, the pattern that will match after following the redirect.
If there is no registered handler that applies to the request, Handler returns a “page not found” handler and an empty pattern.
type Set ¶
type Set[KType comparable] struct { // contains filtered or unexported fields }
func NewSet ¶
func NewSet[KType comparable](keys ...KType) *Set[KType]
NewSet creates a new Set and initializes it with the provided keys.
func (*Set[KType]) Keys ¶
func (s *Set[KType]) Keys() []KType
Keys returns a slice containing all keys in the set.
func (*Set[KType]) MarshalJSON ¶ added in v2.2.3
MarshalJSON implements the json.Marshaler interface.
func (*Set[KType]) MarshalMsgpack ¶ added in v2.2.4
MarshalMsgpack implements the msgpack.Marshaler interface.
func (*Set[KType]) UnmarshalJSON ¶ added in v2.2.3
UnmarshalJSON implements the json.Unmarshaler interface.
func (*Set[KType]) UnmarshalMsgpack ¶ added in v2.2.4
UnmarshalMsgpack implements the msgpack.Unmarshaler interface.
type Slice ¶ added in v2.1.0
type Slice[T any] struct { // contains filtered or unexported fields }
Slice is a generic type that holds elements of any type.
func (*Slice[T]) All ¶ added in v2.1.0
func (s *Slice[T]) All() []T
All returns a copy of all the elements in the slice.
func (*Slice[T]) AllAndClear ¶ added in v2.1.0
func (s *Slice[T]) AllAndClear() []T
AllAndClear returns all the elements in the slice and clears the slice.
func (*Slice[T]) Clear ¶ added in v2.1.0
func (s *Slice[T]) Clear()
Clear removes all the elements in the slice.
func (*Slice[T]) DoRead ¶ added in v2.1.0
func (s *Slice[T]) DoRead(op func([]T))
DoRead allows a custom read-only operation on the slice with a read lock.
func (*Slice[T]) DoWrite ¶ added in v2.1.0
func (s *Slice[T]) DoWrite(op func([]T) []T)
DoWrite allows a custom write operation on the slice with a write lock.
func (*Slice[T]) Filter ¶ added in v2.1.0
Filter returns a new slice containing all elements that satisfy the provided function.
func (*Slice[T]) FindIndex ¶ added in v2.1.0
FindIndex returns the index of the first element that satisfies the provided function. If no element satisfies the function, it returns -1.
func (*Slice[T]) Get ¶ added in v2.1.0
Get returns the element at the specified index, or an error if the index is out of bounds.
func (*Slice[T]) Pop ¶ added in v2.1.0
Pop removes the last element from the slice and returns it, or an error if the slice is empty.
func (*Slice[T]) Push ¶ added in v2.1.0
Push adds one or more elements to the end of the slice and returns the new length.
func (*Slice[T]) Range ¶ added in v2.1.0
Range executes the provided function once for each slice element.
func (*Slice[T]) RangeAndSplice ¶ added in v2.1.0
func (s *Slice[T]) RangeAndSplice(f func(T, int) (bool, int, int, []T), reverse ...bool) ([]T, error)
RangeAndSplice executes a function on each slice element and performs splice operations based on the function's return values. reverse is an optional parameter; if provided and true, the iteration will be in reverse order.
func (*Slice[T]) Remove ¶ added in v2.1.0
Remove removes the first element in the slice that satisfies the conditional function.
func (*Slice[T]) RemoveAll ¶ added in v2.1.0
RemoveAll removes elements from the slice that satisfy the provided condition function.
func (*Slice[T]) Replace ¶ added in v2.1.0
func (s *Slice[T]) Replace(elements []T)
Replace replaces the slice elements with the given elements.
func (*Slice[T]) Set ¶ added in v2.1.0
Set sets the element at the specified index, and returns an error if the index is out of bounds.
func (*Slice[T]) Shift ¶ added in v2.1.0
Shift removes the first element from the slice and returns it, or an error if the slice is empty.
func (*Slice[T]) Slice ¶ added in v2.1.0
Slice returns a new slice containing the elements between start and end.
type StringBuffer ¶ added in v2.3.2
type StringBuffer = types.StringBuffer
Buffer types alias for simplicity
type WebSocketConn ¶
type WebSocketConn struct { EventEmitter *websocket.Conn }
func (*WebSocketConn) Close ¶ added in v2.1.0
func (t *WebSocketConn) Close() error
type WebTransportConn ¶ added in v2.2.1
type WebTransportConn struct { EventEmitter *webtransport.Conn }
func (*WebTransportConn) CloseWithError ¶ added in v2.2.1
func (t *WebTransportConn) CloseWithError(code wt.SessionErrorCode, msg string) error