ejhttp

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2024 License: BSD-2-Clause Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// HeaderNameAuthorization is the name of the HTTP Authorization header
	HeaderNameAuthorization = "Authorization"

	// VarRemoteAddr is the name of the variable used to store the remote address of the HTTP client
	VarRemoteAddr = "remote_addr"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Address      string `toml:"address"`        // Address is the address this server will listen on. Default: All available interfaces
	OptionsAge   int    `toml:"options_age"`    // OptionsAge is the number of seconds a client should cache OPTIONS replies for. Default: 300
	Origin       string `toml:"allowed_origin"` // Origin is the CORS origin answer for this server. Default: *
	Port         int    `toml:"port"`           // Port is the TCP port this server should listen on. Default: 8080
	TimeoutRead  int    `toml:"read_timeout"`   // TimeoutRead is the number of seconds that a read can take before it times out. Default: 10
	TimeoutWrite int    `toml:"write_timeout"`  // TimeoutWrite is the number of seconds that a write can take before it times out. Default: 10
}

Config holds the configuration data for the HTTP Server

type Context

type Context interface {
	AddCookie(*http.Cookie)          // AddCookie adds a cookie to be added to the response
	GetCookies() []*http.Cookie      // GetCookies returns any cookies that are to be added to the response
	GetData() interface{}            // GetData returns data stored by a previous middleware or handler
	GetHeaders() map[string][]string // GetHeaders returns any headers that have been set by any middleware or endpoint handlers
	GetResponse() (int, interface{}) // GetResponse returns the response code and data to be sent to the client
	GetRequest() *http.Request       // GetRequest returns the original HTTP request
	GetVar(string) string            // GetVar returns a stored variable
	Logger() log.Logger              // Logger returns the logging instance
	SetData(interface{})             // SetData sets data in the context to be used by the next middleware or handler
	SetHeader(string, []string)      // SetHeader sets a header to be returned to the caller
	SetResponse(int, interface{})    // SetResponse sets the response code and data to be sent to the client
	SetVar(string, string)           // SetVar stores a variable and its data
}

Context is the context interface passed through middlewares and handlers

Context holds the original ejhttp.Request and a Logging interface that holds an ID specific to it.

func NewContext

func NewContext(req *http.Request, logger log.Logger) Context

type Distributor

type Distributor struct {
	AllowedOrigin string       // AllowedOrigin is an allowed origin for CORS policy
	Handler       Handler      // Handler is the final handler for this path and method
	Headers       []string     // Headers are the headers that are accepted for this endpoint.
	Method        string       // Method is the HTTP method that this distributor is operating for
	Middlewares   []Middleware // Middlewares are any middleware handlers that need to be run for this path and method
	Logger        log.Logger   // Logger is the parent logger interface that this distributor should be using
	Path          string       // Path is the URI path that this distributor is operating on
	// contains filtered or unexported fields
}

Distributor handles a HTTP request for a route

It is the callers responsibility to provide all headers that the distributor expect or could expect, including the Authorization header if it is required. These headers will be used to automatically build an OPTIONS handler.

The trailing slash for a path is automatically handled. The provided path will have it's trailing slash removed and a new path with the trailing slash will also be pointed at the Distributor.

The distributor will set a variable named `remote_addr`, accessible via ctx.GetVar(`remote_addr`) or ctx.GetVar(ejhttp.VarRemoteAddr). This variable holds the address of the client making this request. This address is searched for, in order of header names X-Real-IP and X-Forwarded-For, and then finally falling back to the ejhttp servers storage of the remote address.

func (*Distributor) ServeHTTP

func (d *Distributor) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP begins handling a HTTP request for this route and method

type Handler

type Handler func(Context)

Handler executes final operations for a route

type Middleware

type Middleware func(Context) bool

Middleware executes a middleware handler

type Server

type Server interface {
	AddDistributor(dist *Distributor) error // AddDistributor adds a Distributor to the server
	ClearDistributors() error               // ClearDistributors clears all stored Distributors
	ClearMux() error                        // ClearMux clears the configured mux for this server
	PopulateMux() error                     // PopulateMux configures and populates the mux (router) for this server
	Start() error                           // Start starts this HTTP server
	Stop() error                            // Stops this HTTP server
}

Server is the HTTP server interface

func New

func New(config Config, logger log.Logger) (Server, error)

New returns a new Server instance

Jump to

Keyboard shortcuts

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