engine

package module
v0.0.0-...-12e8980 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2014 License: MIT Imports: 19 Imported by: 0

README

Engine

GoDoc Build Status Coverage Status license

Engine is a core to drive a Go web framework with routing, context, http statuses and more to bridge the distance between the Go standard library and your own web framework.

https://thrisp.github.io/engine/

Documentation

Index

Constants

View Source
const (
	ErrorTypeInternal = 1 << iota
	ErrorTypeExternal = 1 << iota
	ErrorTypePanic    = 1 << iota
	ErrorTypeAll      = 0xffffffff
)
View Source
const (
	NotWritten = -1
)

Variables

View Source
var (
	CurrentContext context.Context
)
View Source
var (
	EnginePanic = []byte("engine-panic")
)

Functions

func CleanPath

func CleanPath(p string) string

CleanPath is the URL version of path.Clean, it returns a canonical URL path for p, eliminating . and .. elements.

The following rules are applied iteratively until no further processing can be done:

  1. Replace multiple slashes with a single slash.
  2. Eliminate each . path name element (the current directory).
  3. Eliminate each inner .. path name element (the parent directory) along with the non-.. element that precedes it.
  4. Eliminate .. elements that begin a rooted path: that is, replace "/.." by "/" at the beginning of a path.

If the result of this process is an empty string, "/" is returned

func MethodColor

func MethodColor(method string) (color string)

func PanicHandle

func PanicHandle(c context.Context)

PanicHandle is the default Manage for 500 & internal panics. Retrieves all ErrorTypePanic from context.Context.Errors, sends signal, logs to stdout or logger, and serves a basic html page if engine.ServePanic is true.

func SignalQueue

func SignalQueue(e *Engine)

A simple Signal queue outputting everything emitted to engine.Signals.

func StatusColor

func StatusColor(code int) (color string)

Types

type Conf

type Conf func(*Engine) error

A configuration function that takes an engine pointer, configures the engine within the function, and returns an error.

func HTMLStatus

func HTMLStatus(b bool) Conf

func Logger

func Logger(l *log.Logger) Conf

Logger specifies a log.Logger, and sets LoggingOn to true, and capturing signals with Head labeled "do-log"

func LoggingOn

func LoggingOn(b bool) Conf

LogginOn sets Logger to a default log.Logger and sets LoggingOn to true.

func MaxFormMemory

func MaxFormMemory(byts int64) Conf

func RedirectFixedPath

func RedirectFixedPath(b bool) Conf

func RedirectTrailingSlash

func RedirectTrailingSlash(b bool) Conf

func ServePanic

func ServePanic(b bool) Conf

type Ctx

type Ctx struct {
	RW ResponseWriter

	Params Params

	Errors errorMsgs
	// contains filtered or unexported fields
}

Ctx is the core request-response context passed between any Manage handlers, useful for storing & persisting data within a request & response.

func (*Ctx) Abort

func (c *Ctx) Abort(code int)

Immediately abort the context, writing out the code to the response

func (*Ctx) Data

func (c *Ctx) Data() map[string]interface{}

func (*Ctx) Error

func (c *Ctx) Error(err error, meta interface{})

Attaches an error to a list of errors. Call Error for each error that occurred during the resolution of a request.

func (*Ctx) Fail

func (c *Ctx) Fail(code int, err error)

Fail is the same as Abort plus an error message. Calling `c.Fail(500, err)` is equivalent to: ``` c.Error(err, "Failed.") c.Abort(500) ```

func (*Ctx) Files

func (c *Ctx) Files() map[string][]*multipart.FileHeader

func (Ctx) Fmt

func (r Ctx) Fmt() string

func (*Ctx) Form

func (c *Ctx) Form() url.Values

func (*Ctx) LastError

func (c *Ctx) LastError() error

Returns the last error for the Ctx.

func (Ctx) Latency

func (r Ctx) Latency() time.Duration

func (Ctx) LogFmt

func (r Ctx) LogFmt() string

func (Ctx) PostProcess

func (r Ctx) PostProcess(req *http.Request, rw ResponseWriter)

func (*Ctx) Request

func (c *Ctx) Request() *http.Request

func (Ctx) Requester

func (r Ctx) Requester(req *http.Request)

func (Ctx) Start

func (r Ctx) Start()

func (*Ctx) Status

func (c *Ctx) Status(code int)

Calls an HttpStatus in the current group by integer code from the Context, if the status exists.

func (*Ctx) StatusFunc

func (c *Ctx) StatusFunc() (func(int), bool)

func (Ctx) Stop

func (r Ctx) Stop()

func (*Ctx) Writer

func (c *Ctx) Writer() ResponseWriter

type Engine

type Engine struct {
	*Group

	Logger  *log.Logger
	Signals Signals
	Queues  queues
	// contains filtered or unexported fields
}

Engine is the the core struct with groups, routing, signaling and more.

func Basic

func Basic(opts ...Conf) (engine *Engine, err error)

Basic produces a new engine with LoggingOn set to true and a default logger.

func Empty

func Empty() *Engine

Empty returns an empty Engine with zero configuration.

func New

func New(opts ...Conf) (engine *Engine, err error)

New produces a new engine, with default configuration, a base group, method for retrieving a new Ctx, and signalling.

func (*Engine) Emit

func (e *Engine) Emit(message string)

Emit takes a string that goes as []byte directly to engine.Signals

func (*Engine) Handler

func (e *Engine) Handler(method, path string, handler http.Handler)

Handler allows the usage of a http.Handler as request manage.

func (*Engine) HandlerFunc

func (e *Engine) HandlerFunc(method, path string, handler http.HandlerFunc)

HandlerFunc allows the use of a http.HandlerFunc as request manage.

func (*Engine) Lookup

func (e *Engine) Lookup(method, path string) (Manage, Params, bool)

Lookup allows the manual lookup of a method + path combo.

func (*Engine) Manage

func (e *Engine) Manage(method string, path string, m Manage)

Registers a new request Manage function with the given path and method.

func (*Engine) Message

func (e *Engine) Message(message string)

Message goes directly to a logger, if enabled.

func (*Engine) PanicMessage

func (e *Engine) PanicMessage(message string)

PanicMessage goes to a standard and unavaoidable log, then emits a signal.

func (*Engine) Reconfigure

func (e *Engine) Reconfigure(reconfigure func() error) error

func (*Engine) Run

func (engine *Engine) Run(addr string)

func (*Engine) Send

func (e *Engine) Send(queue string, message string)

Send sends a message to the specified queue.

func (*Engine) ServeFiles

func (e *Engine) ServeFiles(path string, root http.FileSystem)

ServeFiles serves files from the given file system root. The path must end with "/*filepath", files are then served from the local path /defined/root/dir/*filepath.

e.g., if root is "/etc" and *filepath is "passwd", the local file "/etc/passwd" would be served.

Internally a http.FileServer is used, therefore http.NotFound is used instead of the Router's NotFound handler.

To use the operating system's file system implementation, use http.Dir:

router.ServeFiles("/src/*filepath", http.Dir("/var/www"))

func (*Engine) ServeHTTP

func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP makes the engine implement the http.Handler interface.

func (*Engine) SetConf

func (e *Engine) SetConf(opts ...Conf) error

func (*Engine) SetConfBool

func (e *Engine) SetConfBool(fieldname string, as bool) error

func (*Engine) SetConfInt64

func (e *Engine) SetConfInt64(fieldname string, as int64) error

type EngineError

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

func (*EngineError) Error

func (e *EngineError) Error() string

type Group

type Group struct {
	HttpStatuses
	// contains filtered or unexported fields
}

func NewGroup

func NewGroup(prefix string, engine *Engine) *Group

NewGroup creates a group with no parent and the provided prefix.

func (*Group) Middleware

func (group *Group) Middleware(h ...http.HandlerFunc)

func (*Group) New

func (group *Group) New(component string) *Group

New creates a group from an existing group using the the groups prefix and the provided component string as a prefix. The existing group will be the parent of the new group.

func (*Group) Take

func (group *Group) Take(route string, method string, handler func(context.Context))

Handle provides a route, method, and Manage to the router, and creates a function using the handler when the router matches the route and method.

func (*Group) TakeStatus

func (group *Group) TakeStatus(code int, statushandler func(context.Context))

type HttpStatus

type HttpStatus struct {
	Code     int
	Message  string
	Handlers []Manage
}

Status code, message, and Manage handlers for a http status.

func NewHttpStatus

func NewHttpStatus(code int, message string) *HttpStatus

Create new HttpStatus with the code, message, and default Manage handlers.

func (*HttpStatus) Update

func (h *HttpStatus) Update(handlers ...Manage)

Adds any number of custom Manage to the HttpStatus, between the default status before & after manage.

type HttpStatuses

type HttpStatuses map[int]*HttpStatus

A map of HttpStatus instances, keyed by status code

func (HttpStatuses) New

func (hs HttpStatuses) New(h *HttpStatus)

New adds a new HttpStatus to HttpStatuses keyed by status code.

type Manage

type Manage func(context.Context)

Manage is a function that can be registered to a route to handle HTTP requests. Like http.HandlerFunc, but takes a context.Context

type Param

type Param struct {
	Key   string
	Value string
}

Param is a single URL parameter, consisting of a key and a value.

type Params

type Params []Param

Params is a Param-slice, as returned by the router. The slice is ordered, the first URL parameter is also the first slice value. It is safe to read values by the index.

func (Params) ByName

func (ps Params) ByName(name string) string

ByName returns the value of the first Param which key matches the given name. If no matching Param is found, an empty string is returned.

type ResponseWriter

type ResponseWriter interface {
	http.ResponseWriter
	http.Hijacker
	http.Flusher
	http.CloseNotifier

	Status() int
	Size() int
	Written() bool
	WriteHeaderNow()
}

type Signal

type Signal []byte

Signal denotes a byte signal

type Signals

type Signals chan Signal

Signals is a channel for Signal

Jump to

Keyboard shortcuts

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