http

package
v0.0.0-...-0b10b3f Latest Latest
Warning

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

Go to latest
Published: May 1, 2024 License: MIT Imports: 32 Imported by: 0

Documentation

Overview

Source: https://github.com/benbjohnson/wtf/tree/main

Index

Constants

View Source
const (
	EventSendMessage = "send_message"
	EventNewMessage  = "new_message"
)

Variables

This section is empty.

Functions

func BadRequest

func BadRequest(w http.ResponseWriter, msg string)

func BearerAuth

func BearerAuth(r *http.Request) (string, error)

func ErrorStatusCode

func ErrorStatusCode(code string) int

ErrorStatusCode returns the associated HTTP status code

func Forbidden

func Forbidden(w http.ResponseWriter, msg string)

func FromErrorStatusCode

func FromErrorStatusCode(code int) string

FromErrorStatusCode returns the associated code for an HTTP status code.

func InternalServerError

func InternalServerError(w http.ResponseWriter, msg string)

func InvalidSession

func InvalidSession(w http.ResponseWriter)

func NotAuthenticaded

func NotAuthenticaded(w http.ResponseWriter)

func OK

func OK(w http.ResponseWriter, msg string)

func ReadBool

func ReadBool(qs url.Values, key string) bool

ReadBoolParam reads the key from the url query and returns true if the param is equal 'true' otherwise it returns false.

func ReadCSV

func ReadCSV(qs url.Values, key string, defaulValue []string) []string

func ReadInt

func ReadInt(qs url.Values, key string, defaultValue int, v *validator.Validator) int

func ReadString

func ReadString(qs url.Values, key string, defaulValue string) string

func ReadTime

func ReadTime(qs url.Values, key string, defaulValue time.Time) time.Time

func ReadTimeString

func ReadTimeString(qs url.Values, key string, defaulValue string) string

func ReadURLParam

func ReadURLParam(r *http.Request, key string) (string, error)

func ReadUUIDParam

func ReadUUIDParam(r *http.Request, key string) (uuid.UUID, error)

func TooManyRequestsError

func TooManyRequestsError(w http.ResponseWriter, msg string)

func Unauthorized

func Unauthorized(w http.ResponseWriter, msg string)

func ValidateRecipient

func ValidateRecipient(v *validator.Validator, recipient *echosight.Recipient)

Types

type Client

type Client struct {
	ID string
	// contains filtered or unexported fields
}

func NewWebSocketClient

func NewWebSocketClient(conn *websocket.Conn, manager *WebSocketManager, topicID string) *Client

type ClientList

type ClientList map[*Client]struct{}

ClientList. TODO: user map[string]*Client (clientID)

type Event

type Event struct {
	Type    string          `json:"type"`
	Payload json.RawMessage `json:"payload"`
}

type EventHandler

type EventHandler func(event Event, c *Client) error

type HandlerFunc

type HandlerFunc func(w http.ResponseWriter, r *http.Request) error

type NewMessageEvent

type NewMessageEvent struct {
	SendMessageEvent
	Sent time.Time `json:"sent"`
}

type RateLimiter

type RateLimiter struct {
	Enabled bool
	Burst   int
	Limit   int
}

type Response

type Response struct {
	Status  ResponseStatus `json:"status"`
	Code    string         `json:"code,omitempty"`
	Message string         `json:"message,omitempty"`
	Data    W              `json:"data,omitempty"`
	Errors  any            `json:"errors,omitempty"`
}

func (*Response) WithErrors

func (r *Response) WithErrors(errs any) *Response

func (*Response) WithMessage

func (r *Response) WithMessage(msg string) *Response

func (*Response) WithStatus

func (r *Response) WithStatus(status ResponseStatus) *Response

type ResponseStatus

type ResponseStatus string
const (
	StatusOK    ResponseStatus = "ok"
	StatusWarn  ResponseStatus = "warning"
	StatusErr   ResponseStatus = "error"
	StatusFatal ResponseStatus = "fatal"
)

type SendMessageEvent

type SendMessageEvent struct {
	Message string `json:"message"`
	From    string `json:"from"`
}

type Server

type Server struct {
	IsDev bool

	RateLimiter RateLimiter

	UserService       echosight.UserService
	HostService       echosight.HostService
	DetectorService   echosight.DetectorService
	RecipientService  echosight.RecipientService
	PreferenceService echosight.PreferenceService
	SessionService    echosight.SessionService

	MetricReader echosight.MetricReader
	Scheduler    *observer.Scheduler
	EventHandler *eventflow.Engine
	Crypter      echosight.Crypter
	// contains filtered or unexported fields
}

Server is a simple wrapper for e.g. chi mux to use custom hanlders wich returns an error

func NewServer

func NewServer(opts ServerOpts) (*Server, error)

NewServer creates a new EchoSight server with all routes mapped

func (*Server) Run

func (s *Server) Run() error

Run starts the server and blocks the code

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Server) ServerContext

func (s *Server) ServerContext(parent context.Context) (context.Context, context.CancelFunc)

type ServerOpts

type ServerOpts struct {
	TrustedOrigins []string
	Addr           string
}

type W

type W map[string]any

type WebSocketManager

type WebSocketManager struct {
	sync.RWMutex

	TrustedOrigins []string
	// contains filtered or unexported fields
}

func NewWebSocketManager

func NewWebSocketManager(s *Server) *WebSocketManager

func (*WebSocketManager) SendMessage

func (m *WebSocketManager) SendMessage(event Event, c *Client) error

SendMessage sends messages to the clients

Jump to

Keyboard shortcuts

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