httprouter

package
v1.10.1 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2023 License: AGPL-3.0 Imports: 22 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthAccessType

type AuthAccessType int
const (
	AccessTypePublic AuthAccessType = iota
	AccessTypeQuota
	AccessTypePrivate
	AccessTypeAdmin
)

type HTTPContext

type HTTPContext struct {
	Writer  http.ResponseWriter
	Request *http.Request
	// contains filtered or unexported fields
}

HTTPContext is the Context for an HTTP request.

func (*HTTPContext) Send

func (h *HTTPContext) Send(msg []byte, httpStatusCode int) error

Send replies the request with the provided message.

func (*HTTPContext) URLParam

func (h *HTTPContext) URLParam(key string) string

URLParam is a wrapper around go-chi to get a URL parameter (specified in the path pattern as {key})

type HTTProuter

type HTTProuter struct {
	Mux        *chi.Mux
	TLSconfig  *tls.Config
	TLSdomain  string
	TLSdirCert string
	// contains filtered or unexported fields
}

HTTProuter is a thread-safe multiplexer http(s) router using go-chi and autocert with a set of preconfigured options. The router abstracts the HTTP layer and uses a custom Message type that allows create handlers in a comfortable manner. Allows multiple different kind of data processors by using the RouterNamespace interface. Each processor is identified by a unique namespace string which must be specified when adding handlers. Handlers can be Public, Private and Administrator. The proper checks must be implemented by the RouterNamespace implementation.

func (*HTTProuter) AddAdminHandler

func (r *HTTProuter) AddAdminHandler(namespaceID,
	pattern, HTTPmethod string, handler RouterHandlerFn)

AddAdminHandler adds a handler function for the namespace, pattern and HTTPmethod. The Admin requests are usually protected by some authorization mechanism.

func (*HTTProuter) AddNamespace

func (r *HTTProuter) AddNamespace(id string, rns RouterNamespace)

AddNamespace creates a new namespace handled by the RouterNamespace implementation.

func (*HTTProuter) AddPrivateHandler

func (r *HTTProuter) AddPrivateHandler(namespaceID,
	pattern, HTTPmethod string, handler RouterHandlerFn)

AddPrivateHandler adds a handler function for the namespace, pattern and HTTPmethod. The Private requests are usually protected by some authorization mechanism, such as signature verification, which must be handled by the namespace implementation.

func (*HTTProuter) AddPublicHandler

func (r *HTTProuter) AddPublicHandler(namespaceID,
	pattern, HTTPmethod string, handler RouterHandlerFn)

AddPublicHandler adds a handled function for the namespace, patter and HTTPmethod. The public requests are not protected so all requests are allowed.

func (*HTTProuter) AddQuotaHandler

func (r *HTTProuter) AddQuotaHandler(namespaceID,
	pattern, HTTPmethod string, handler RouterHandlerFn)

AddQuotaHandler adds a handler function for the namespace, pattern and HTTPmethod. The Quota requests are rate-limited per bearer token

func (*HTTProuter) AddRawHTTPHandler

func (r *HTTProuter) AddRawHTTPHandler(pattern, HTTPmethod string, handler http.HandlerFunc)

AddRawHTTPHandler adds a standard net/http handled function to the router. The public requests are not protected so all requests are allowed.

func (*HTTProuter) Address

func (r *HTTProuter) Address() net.Addr

Address return the current network address used by the HTTP router

func (*HTTProuter) EnablePrometheusMetrics added in v1.4.0

func (r *HTTProuter) EnablePrometheusMetrics(prometheusID string)

EnablePrometheusMetrics enables go-chi prometheus metrics under specified ID. If ID empty, the default "gochi_http" is used.

func (*HTTProuter) ExposePrometheusEndpoint added in v1.10.0

func (r *HTTProuter) ExposePrometheusEndpoint(path string)

ExposePrometheusEndpoint registers a HTTPHandler at the passed path that will expose the metrics collected by VictoriaMetrics and Prometheus

func (*HTTProuter) Init

func (r *HTTProuter) Init(host string, port int) error

Init initializes the router

type Message

type Message struct {
	Data      any
	TimeStamp time.Time
	Path      []string
	Context   *HTTPContext
}

Message is a wrapper for messages for a RouterNamespace implementation. Data is set by the namespace and can be of any type (implementation details should be checked). In order to send a reply, Context.Send() should be called.

type RouterHandlerFn

type RouterHandlerFn = func(msg Message)

RouterHandlerFn is the function signature for adding handlers to the HTTProuter.

type RouterNamespace

type RouterNamespace interface {
	AuthorizeRequest(data any, accessType AuthAccessType) (valid bool, err error)
	ProcessData(req *http.Request) (data any, err error)
}

RouterNamespace is the interface that a HTTProuter handler should follow in order to become a valid namespace.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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