raizu

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2022 License: Apache-2.0 Imports: 13 Imported by: 0

README

raizu

Nano HTTP web application framework that draws inspiration from Python Flask and Django.

Raizu works just like net/http with a few extra functions. It is designed to offer suggestions and default implementations, but doesn't enforce any specific dependencies or project layout.

Raizu provides configuration and conventions to get started and build reusable web applications.

Documentation

Index

Constants

View Source
const (
	DefaultAddress = ":http"
)

Variables

View Source
var (
	ErrMalformedContent = errors.New("content has syntax error")
	ErrTooLarge         = errors.New("request is too large")
)
View Source
var (
	ErrBadRequest                   = NewErrorOpts(http.StatusBadRequest)
	ErrUnauthorized                 = NewErrorOpts(http.StatusUnauthorized)
	ErrPaymentRequired              = NewErrorOpts(http.StatusPaymentRequired)
	ErrForbidden                    = NewErrorOpts(http.StatusForbidden)
	ErrNotFound                     = NewErrorOpts(http.StatusNotFound)
	ErrMethodNotAllowed             = NewErrorOpts(http.StatusMethodNotAllowed)
	ErrNotAcceptable                = NewErrorOpts(http.StatusNotAcceptable)
	ErrProxyAuthRequired            = NewErrorOpts(http.StatusProxyAuthRequired)
	ErrRequestTimeout               = NewErrorOpts(http.StatusRequestTimeout)
	ErrConflict                     = NewErrorOpts(http.StatusConflict)
	ErrGone                         = NewErrorOpts(http.StatusGone)
	ErrLengthRequired               = NewErrorOpts(http.StatusLengthRequired)
	ErrPreconditionFailed           = NewErrorOpts(http.StatusPreconditionFailed)
	ErrRequestEntityTooLarge        = NewErrorOpts(http.StatusRequestEntityTooLarge)
	ErrRequestURITooLong            = NewErrorOpts(http.StatusRequestURITooLong)
	ErrUnsupportedMediaType         = NewErrorOpts(http.StatusUnsupportedMediaType)
	ErrRequestedRangeNotSatisfiable = NewErrorOpts(http.StatusRequestedRangeNotSatisfiable)
	ErrExpectationFailed            = NewErrorOpts(http.StatusExpectationFailed)
	ErrTeapot                       = NewErrorOpts(http.StatusTeapot)
	ErrMisdirectedRequest           = NewErrorOpts(http.StatusMisdirectedRequest)
	ErrUnprocessableEntity          = NewErrorOpts(http.StatusUnprocessableEntity)
	ErrLocked                       = NewErrorOpts(http.StatusLocked)
	ErrFailedDependency             = NewErrorOpts(http.StatusFailedDependency)
	ErrTooEarly                     = NewErrorOpts(http.StatusTooEarly)
	ErrUpgradeRequired              = NewErrorOpts(http.StatusUpgradeRequired)
	ErrPreconditionRequired         = NewErrorOpts(http.StatusPreconditionRequired)
	ErrTooManyRequests              = NewErrorOpts(http.StatusTooManyRequests)
	ErrRequestHeaderFieldsTooLarge  = NewErrorOpts(http.StatusRequestHeaderFieldsTooLarge)
	ErrUnavailableForLegalReasons   = NewErrorOpts(http.StatusUnavailableForLegalReasons)

	ErrInternalServerError = NewErrorOpts(http.StatusInternalServerError)
)
View Source
var (
	ErrUnsupported = errors.New("unsupported")
)

Functions

func DecodeBody

func DecodeBody(res http.ResponseWriter, req *http.Request, out any) error

func HasFlush

func HasFlush(w http.ResponseWriter) bool

func HasHijack

func HasHijack(w http.ResponseWriter) bool

func HasReadFrom

func HasReadFrom(w http.ResponseWriter) bool

func ListenAndServe

func ListenAndServe(s HTTPServer, address string) error

func StartJSONResponse

func StartJSONResponse(w http.ResponseWriter, statusCode int)

func Wrap

func Wrap(f HandlerFn) http.Handler

Wrap wraps a function as an http.Handler.

func WrapFn

func WrapFn(f HandlerFn) http.HandlerFunc

WrapFn wraps a function as an http.HandlerFunc.

func WrapHandler

func WrapHandler(h Handler) http.Handler

WrapHandler wraps a raizu.Handler as http.Handler.

func WriteError

func WriteError(w http.ResponseWriter, err *Error)

func WriteJSON

func WriteJSON(w http.ResponseWriter, statusCode int, body any) error

func WriteOK

func WriteOK(w http.ResponseWriter) error

func WriteOKMessage

func WriteOKMessage(w http.ResponseWriter, msg string) error

Types

type AppMux

type AppMux[MT MounterHandler] struct {
	// contains filtered or unexported fields
}

func NewAppMux

func NewAppMux[MT MounterHandler](cfg AppMuxConfig[MT]) (*AppMux[MT], error)

func (*AppMux[MT]) Handler

func (ag *AppMux[MT]) Handler() http.Handler

func (*AppMux[MT]) Prefix

func (ag *AppMux[MT]) Prefix() string

type AppMuxConfig

type AppMuxConfig[MT MounterHandler] struct {
	Prefix     string
	NewMuxFn   func() MT
	Blueprints []Blueprint
}

type Blueprint

type Blueprint interface {
	NewHandlerProvider() (HandlerProvider, error)
}

func AppMuxBlueprint

func AppMuxBlueprint[MT MounterHandler](cfg AppMuxConfig[MT]) Blueprint

func HandlerAppBlueprint added in v0.2.0

func HandlerAppBlueprint(cfg HandlerAppConfig) Blueprint

func NewBlueprint

func NewBlueprint[HPT HandlerProvider, CT any](makeFn func(cf CT) (HPT, error), cf CT) Blueprint

type BlueprintImpl

type BlueprintImpl[HPT HandlerProvider, CT any] struct {
	// contains filtered or unexported fields
}

func (BlueprintImpl[HPT, CT]) NewHandlerProvider

func (bp BlueprintImpl[HPT, CT]) NewHandlerProvider() (HandlerProvider, error)

type Error

type Error struct {
	// contains filtered or unexported fields
}

func NewError

func NewError(status int, msg string, inner error) *Error

func NewErrorOpts

func NewErrorOpts(status int, opts ...ErrorOption) (e *Error)

func (*Error) Error

func (err *Error) Error() (s string)

func (*Error) Unwrap

func (err *Error) Unwrap() error

type ErrorOption

type ErrorOption func(e *Error)

func WithInner

func WithInner(err error) ErrorOption

func WithMessage

func WithMessage(msg string) ErrorOption

type HTTPHandler

type HTTPHandler struct{ Handler }

HTTPHandler wraps a raizu.Handler as http.Handler.

Use the WrapHandler constructor function as a shorthand for wrapping a raizu.Handler as an http.Handler.

func (HTTPHandler) ServeHTTP

func (h HTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type HTTPServer

type HTTPServer interface {
	// Close immediately closes all active [net.Listener] instances and
	// any connections in state [http.StateNew], [http.StateActive], or [http.StateIdle]. For a
	// graceful shutdown, use Shutdown.
	//
	// See [http.Server.Close] for more details.
	Close() error

	// Shutdown gracefully shuts down the server without interrupting any
	// active connections.
	//
	// See [http.Server.Shutdown] for more details.
	Shutdown(ctx context.Context) error

	// Serve starts accepting incoming connections on the [net.Listener] in l, creating a
	// new service goroutine for each. The service goroutines read requests and
	// then is expected to call srv.Handler of the corresponding NewBlueprint to reply to them.
	//
	// See [http.Server.Serve] for more details.
	Serve(l net.Listener) error
}

A HTTPServer represents an HTTP server.

func NewServer

func NewServer(config ServerConfig) (HTTPServer, error)

NewServer creates a new raizu.HTTPServer based on the raizu.ServerConfig provided.

All Blueprints provided in raizu.ServerConfig.Blueprints will be instantiated and mounted on the server. If any raizu.Blueprint instance fails, the whole function fails and returns an error instead.

type Handler

type Handler interface {
	ServeHTTP(http.ResponseWriter, *http.Request) error
}

A Handler responds to an HTTP request.

ServeHTTP should write reply headers and data to the ResponseWriter and then return nil to indicate that the request was successfully processed or return an error if handling the request failed for whatever reason.

All other notes from http.Handler apply here as well.

type HandlerApp added in v0.2.0

type HandlerApp struct {
	// contains filtered or unexported fields
}

func NewHandlerApp added in v0.2.0

func NewHandlerApp(cfg HandlerAppConfig) (*HandlerApp, error)

func (*HandlerApp) Handler added in v0.2.0

func (a *HandlerApp) Handler() http.Handler

func (*HandlerApp) Prefix added in v0.2.0

func (a *HandlerApp) Prefix() string

type HandlerAppConfig added in v0.2.0

type HandlerAppConfig struct {
	Prefix  string
	Handler http.Handler
}

type HandlerFn

type HandlerFn func(http.ResponseWriter, *http.Request) error

HandlerFn is an adapter to allow the use of ordinary functions as raizu.Handler handlers. If f is a function with the appropriate signature, HandlerFn(f) is a raizu.Handler that calls f.

func (HandlerFn) ServeHTTP

func (f HandlerFn) ServeHTTP(w http.ResponseWriter, r *http.Request) error

ServeHTTP calls f(w, r).

type HandlerProvider

type HandlerProvider interface {
	Prefix() string
	Handler() http.Handler
}

type Mounter

type Mounter interface {
	Mount(prefix string, h http.Handler)
}

Mounter is anything a http.Handler can be mounted on.

type MounterHandler

type MounterHandler interface {
	http.Handler
	Mounter
}

MounterHandler represents a http.Handler that can mount other http.Handler instances.

type ServerConfig

type ServerConfig struct {
	// AccessLog will receive http access requests logs as Info
	// level logging and any error encountered during the handling
	// of http access requests.
	Logger logr.Logger

	// TLSConfig
	TLSConfig *tls.Config

	// ReadTimeout see [http.Server.ReadTimeout]
	ReadTimeout time.Duration

	// ReadTimeout see [http.Server.ReadHeaderTimeout]
	ReadHeaderTimeout time.Duration

	// ReadTimeout see [http.Server.WriteTimeout]
	WriteTimeout time.Duration

	// IdleTimeout see [http.Server.IdleTimeout]
	IdleTimeout time.Duration

	// Blueprint is a factory that produces an app which will
	// be served via the http server.
	Blueprint Blueprint
}

ServerConfig describes the Server runtime configuration.

func NewServerConfigDefault

func NewServerConfigDefault() ServerConfig

func (ServerConfig) WithBlueprint

func (c ServerConfig) WithBlueprint(af Blueprint) ServerConfig

func (ServerConfig) WithIdleTimeout

func (c ServerConfig) WithIdleTimeout(v time.Duration) ServerConfig

func (ServerConfig) WithLogger

func (c ServerConfig) WithLogger(logger logr.Logger) ServerConfig

func (ServerConfig) WithReadHeaderTimeout

func (c ServerConfig) WithReadHeaderTimeout(v time.Duration) ServerConfig

func (ServerConfig) WithReadTimeout

func (c ServerConfig) WithReadTimeout(v time.Duration) ServerConfig

func (ServerConfig) WithTLSConfig

func (c ServerConfig) WithTLSConfig(cfg *tls.Config) ServerConfig

func (ServerConfig) WithWriteTimeout

func (c ServerConfig) WithWriteTimeout(v time.Duration) ServerConfig

type UnsupportedError

type UnsupportedError struct {
	// contains filtered or unexported fields
}

func (UnsupportedError) Error

func (e UnsupportedError) Error() string

func (UnsupportedError) Unwrap

func (e UnsupportedError) Unwrap() error

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

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