stackr: github.com/ricallinson/stackr Index | Files

package stackr

import "github.com/ricallinson/stackr"

   Stackr is an extensible HTTP server framework for Go.

	   package main

	   import "github.com/ricallinson/stackr"

	   func main() {
		   app := stackr.CreateServer()
		   app.Use(stackr.Logger())
		   app.Use(stackr.Static())
		   app.Use("/", func(req *stackr.Request, res *stackr.Response, next func()) {
			   res.End("hello world\n")
		   })
		   app.Listen(3000)
	   }

Index

Package Files

error_handler.go favicon.go logger.go method_override.go request.go response.go response_time.go stackr.go static.go

func ErrorHandler Uses

func ErrorHandler(t ...string) func(*Request, *Response, func())

Error handler, providing res.Error message responses for requests accepting text, html, or json.

Example:

stackr.CreateServer().Use(stackr.ErrorHandler())
stackr.CreateServer().Use(stackr.ErrorHandler("Error Title"))

func Favicon Uses

func Favicon(o ...map[string]string) func(*Request, *Response, func())

By default serves the stackr favicon, or the favicon located by the given `path`.

Options:

* `MaxAge` cache-control max-age directive, defaulting to 1 day

Examples:

Serve default favicon:

stackr.CreateServer().Use(stackr.Favicon())
stackr.CreateServer().Use(stackr.Favicon(map[string]string{"maxage": "1000"}))

Serve favicon before logging for brevity:

app := stackr.CreateServer()
app.Use(stackr.Favicon())
app.Use(stackr.Logger())

Serve custom favicon:

stackr.CreateServer().Use(stackr.Favicon(map[string]string{"path": "./public/favicon.ico"}))

func Logger Uses

func Logger(o ...interface{}) func(*Request, *Response, func())

Log requests with the given `options` or a `format` string.

Options:

* `format` Format string, see below for tokens
* `buffer` (not implemented yet) Buffer duration, defaults to 1000ms when _true_
* `immediate` Write log line on request instead of response (for response times)

Optional Second Argument:

* `writer`  Output writer, defaults to _fmt.Println_

Tokens:

* (not implemented) `:req[header]` ex: `:req[Accept]`
* `:res[Content-Length]`
* `:http-version`
* `:response-time`
* `:remote-addr`
* `:date`
* `:method`
* `:url`
* `:referrer`
* `:user-agent`
* `:status`

Formats:

Pre-defined formats that ship with connect:

* `default` ':remote-addr - - [:date] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"'
* `short` ':remote-addr - :method :url HTTP/:http-version :status :res[content-length] - :response-time ms'
* `tiny` ':method :url :status :res[content-length] - :response-time ms'
* `dev` concise output colored by response status for development use

Examples:

app.Use(stackr.Logger()) // default
app.Use(stackr.Logger(map[string]string{"format": "short"}))
app.Use(stackr.Logger(map[string]string{"format": "tiny"}))
app.Use(stackr.Logger(map[string]string{"format": "dev", "immediate": "true"})
app.Use(stackr.Logger(map[string]string{"format": ":method :url - :referrer"})
app.Use(stackr.Logger(map[string]string{"format": ":req[content-type] -> :res[content-type]"})

func MethodOverride Uses

func MethodOverride() func(*Request, *Response, func())

Provides faux HTTP method support.

Pass an optional key to use when checking for a method override, otherwise defaults to _method. The original method is available via req.Map["OriginalMethod"].

Example:

stackr.CreateServer().Use(stackr.MethodOverride())

func ResponseTime Uses

func ResponseTime() func(*Request, *Response, func())

Adds the X-Response-Time header displaying the response duration in milliseconds.

Example:

stackr.CreateServer().Use(stackr.ResponseTime())

func Static Uses

func Static(o ...map[string]string) func(*Request, *Response, func())

Static file server with the given `root` path.

Options:

* `root` The root folder to serve static files from. Defaults to "./public".
* (not implemented yet) `maxage` Browser cache maxAge in milliseconds. Defaults to 0.
* (not implemented yet) `hidden` Allow transfer of hidden files. Defaults to false.
* (not implemented yet) `redirect` Redirect to trailing "/" when the pathname is a dir. Defaults to true.
* (not implemented yet) `index` Default file name. Defaults to 'index.html'.

Examples:

stackr.CreateServer().Use(stackr.Static())
stackr.CreateServer().Use(stackr.Static(map[string]string{"root": "./public"}))

type Request Uses

type Request struct {

    // The standard http.Request type
    *http.Request

    // The value of .URL.RequestURI() for easy access.
    // Note: this value may be changed by the Stackr.Handle() function.
    Url string

    // Set to the vlue of the matched portion of the .URL.RequestURI()
    MatchedUrl string

    // The value of .URL.RequestURI() for easy access.
    // Note: this value should NEVER be changed.
    OriginalUrl string

    // This property is a map containing the parsed request body.
    // This feature is provided by the bodyParser() middleware, though other body
    // parsing middleware may follow this convention as well.
    // This property defaults to {} when bodyParser() is used.
    Body map[string]string

    // This property is a map containing the parsed query-string, defaulting to {}.
    Query map[string]string

    // This property is a map of the files uploaded. This feature is provided
    // by the bodyParser() middleware, though other body parsing middleware may
    // follow this convention as well. This property defaults to {} when bodyParser() is used.
    Files map[string]interface{}

    // Holds custom values set by functions in the request flow.
    Map map[string]interface{}
}

A Request represents an HTTP request received by the server.

type Response Uses

type Response struct {

    // The standard http.ResponseWriter interface.
    Writer http.ResponseWriter

    // Ture if headers have been sent.
    HeaderSent bool

    // The HTTP status code to be return.
    StatusCode int

    // True if .End() has been called.
    Closed bool

    // Error. Populated by anything that wants to trigger an error.
    Error error
    // contains filtered or unexported fields
}

Response represents the response from an HTTP request.

func (*Response) Emit Uses

func (this *Response) Emit(event string)

Emit an event calling all registered listeners.

func (*Response) End Uses

func (this *Response) End(data string) bool

Close the connection to the client.

func (*Response) On Uses

func (this *Response) On(event string, fn func())

Register a listener function for an event.

func (*Response) RemoveHeader Uses

func (this *Response) RemoveHeader(key string)

Remove the named header.

func (*Response) SetHeader Uses

func (this *Response) SetHeader(key string, value string) bool

Set a single header.

Note: all keys are converted to lower case.

func (*Response) SetHeaders Uses

func (this *Response) SetHeaders(headers map[string]string) bool

Set a map of headers, calls SetHeader() for each item.

func (*Response) Write Uses

func (this *Response) Write(data string) bool

Write data to the client.

func (*Response) WriteBytes Uses

func (this *Response) WriteBytes(data []byte) bool

Write bytes to the client.

type Server Uses

type Server struct {
    Env string
    // contains filtered or unexported fields
}

A Stackr Server.

func CreateServer Uses

func CreateServer() *Server

Create a new stackr server.

func (*Server) Handle Uses

func (this *Server) Handle(req *Request, res *Response, index int)

Handle server requests, punting them down the middleware stack.

Note: this is a recursive function.

func (*Server) Listen Uses

func (this *Server) Listen(port int)

Listen for connections on HTTP.

func (*Server) ListenTLS Uses

func (this *Server) ListenTLS(port int, certFile string, keyFile string)

Listen for connections on HTTPS.

func (*Server) ServeHTTP Uses

func (this *Server) ServeHTTP(res http.ResponseWriter, req *http.Request)

ServeHTTP calls .Handle(req, res).

func (*Server) Use Uses

func (this *Server) Use(in ...interface{}) *Server
   Utilize the given middleware `Handle` to the given `route`,
   defaulting to _/_. This "route" is the mount-point for the
   middleware, when given a value other than _/_ the middleware
   is only effective when that segment is present in the request's
   pathname.

   For example if we were to mount a function at _/admin_, it would
   be invoked on _/admin_, and _/admin/settings_, however it would
   not be invoked for _/_, or _/posts_.

   Examples:

	   var app = stackr.CreateServer();
	   app.Use(stackr.Favicon())
	   app.Use(stackr.Logger())
	   app.Use("/public", stackr.Static())

   If we wanted to prefix static files with _/public_, we could
   "mount" the `Static()` middleware:

	   app.Use("/public", stackr.Static(stackr.OptStatic{Root: "./static_files"}))

   This api is chainable, so the following is valid:

	   stackr.CreateServer().Use(stackr.Favicon()).Listen(3000);

Package stackr imports 13 packages (graph) and is imported by 5 packages. Updated 2016-07-15. Refresh now. Tools for package owners.