martini: github.com/go-martini/martini Index | Files

package martini

import "github.com/go-martini/martini"

Package martini is a powerful package for quickly writing modular web applications/services in Golang.

For a full guide visit http://github.com/go-martini/martini

package main

import "github.com/go-martini/martini"

func main() {
  m := martini.Classic()

  m.Get("/", func() string {
    return "Hello world!"
  })

  m.Run()
}

Index

Package Files

env.go logger.go martini.go recovery.go response_writer.go return_handler.go router.go static.go

Constants

const (
    Dev  string = "development"
    Prod string = "production"
    Test string = "test"
)

Envs

Variables

var Env = Dev

Env is the environment that Martini is executing in. The MARTINI_ENV is read on initialization to set this variable.

var Root string

type BeforeFunc Uses

type BeforeFunc func(ResponseWriter)

BeforeFunc is a function that is called before the ResponseWriter has been written to.

type ClassicMartini Uses

type ClassicMartini struct {
    *Martini
    Router
}

ClassicMartini represents a Martini with some reasonable defaults. Embeds the router functions for convenience.

func Classic Uses

func Classic() *ClassicMartini

Classic creates a classic Martini with some basic default middleware - martini.Logger, martini.Recovery and martini.Static. Classic also maps martini.Routes as a service.

type Context Uses

type Context interface {
    inject.Injector
    // Next is an optional function that Middleware Handlers can call to yield the until after
    // the other Handlers have been executed. This works really well for any operations that must
    // happen after an http request
    Next()
    // Written returns whether or not the response for this context has been written.
    Written() bool
}

Context represents a request context. Services can be mapped on the request level from this interface.

type Handler Uses

type Handler interface{}

Handler can be any callable function. Martini attempts to inject services into the handler's argument list. Martini will panic if an argument could not be fullfilled via dependency injection.

func Logger Uses

func Logger() Handler

Logger returns a middleware handler that logs the request as it goes in and the response as it goes out.

func Recovery Uses

func Recovery() Handler

Recovery returns a middleware that recovers from any panics and writes a 500 if there was one. While Martini is in development mode, Recovery will also output the panic as HTML.

func Static Uses

func Static(directory string, staticOpt ...StaticOptions) Handler

Static returns a middleware handler that serves static files in the given directory.

type Martini Uses

type Martini struct {
    inject.Injector
    // contains filtered or unexported fields
}

Martini represents the top level web application. inject.Injector methods can be invoked to map services on a global level.

func New Uses

func New() *Martini

New creates a bare bones Martini instance. Use this method if you want to have full control over the middleware that is used.

func (*Martini) Action Uses

func (m *Martini) Action(handler Handler)

Action sets the handler that will be called after all the middleware has been invoked. This is set to martini.Router in a martini.Classic().

func (*Martini) Handlers Uses

func (m *Martini) Handlers(handlers ...Handler)

Handlers sets the entire middleware stack with the given Handlers. This will clear any current middleware handlers. Will panic if any of the handlers is not a callable function

func (*Martini) Logger Uses

func (m *Martini) Logger(logger *log.Logger)

Logger sets the logger

func (*Martini) Run Uses

func (m *Martini) Run()

Run the http server. Listening on os.GetEnv("PORT") or 3000 by default.

func (*Martini) RunOnAddr Uses

func (m *Martini) RunOnAddr(addr string)

Run the http server on a given host and port.

func (*Martini) ServeHTTP Uses

func (m *Martini) ServeHTTP(res http.ResponseWriter, req *http.Request)

ServeHTTP is the HTTP Entry point for a Martini instance. Useful if you want to control your own HTTP server.

func (*Martini) Use Uses

func (m *Martini) Use(handler Handler)

Use adds a middleware Handler to the stack. Will panic if the handler is not a callable func. Middleware Handlers are invoked in the order that they are added.

type Params Uses

type Params map[string]string

Params is a map of name/value pairs for named routes. An instance of martini.Params is available to be injected into any route handler.

type ResponseWriter Uses

type ResponseWriter interface {
    http.ResponseWriter
    http.Flusher
    http.Hijacker
    // Status returns the status code of the response or 0 if the response has not been written.
    Status() int
    // Written returns whether or not the ResponseWriter has been written.
    Written() bool
    // Size returns the size of the response body.
    Size() int
    // Before allows for a function to be called before the ResponseWriter has been written to. This is
    // useful for setting headers or any other operations that must happen before a response has been written.
    Before(BeforeFunc)
}

ResponseWriter is a wrapper around http.ResponseWriter that provides extra information about the response. It is recommended that middleware handlers use this construct to wrap a responsewriter if the functionality calls for it.

func NewResponseWriter Uses

func NewResponseWriter(rw http.ResponseWriter) ResponseWriter

NewResponseWriter creates a ResponseWriter that wraps an http.ResponseWriter

type ReturnHandler Uses

type ReturnHandler func(Context, []reflect.Value)

ReturnHandler is a service that Martini provides that is called when a route handler returns something. The ReturnHandler is responsible for writing to the ResponseWriter based on the values that are passed into this function.

type Route Uses

type Route interface {
    // URLWith returns a rendering of the Route's url with the given string params.
    URLWith([]string) string
    // Name sets a name for the route.
    Name(string)
    // GetName returns the name of the route.
    GetName() string
    // Pattern returns the pattern of the route.
    Pattern() string
    // Method returns the method of the route.
    Method() string
}

Route is an interface representing a Route in Martini's routing layer.

type RouteMatch Uses

type RouteMatch int
const (
    NoMatch RouteMatch = iota
    StarMatch
    OverloadMatch
    ExactMatch
)

func (RouteMatch) BetterThan Uses

func (r RouteMatch) BetterThan(o RouteMatch) bool

Higher number = better match

type Router Uses

type Router interface {
    Routes

    // Group adds a group where related routes can be added.
    Group(string, func(Router), ...Handler)
    // Get adds a route for a HTTP GET request to the specified matching pattern.
    Get(string, ...Handler) Route
    // Patch adds a route for a HTTP PATCH request to the specified matching pattern.
    Patch(string, ...Handler) Route
    // Post adds a route for a HTTP POST request to the specified matching pattern.
    Post(string, ...Handler) Route
    // Put adds a route for a HTTP PUT request to the specified matching pattern.
    Put(string, ...Handler) Route
    // Delete adds a route for a HTTP DELETE request to the specified matching pattern.
    Delete(string, ...Handler) Route
    // Options adds a route for a HTTP OPTIONS request to the specified matching pattern.
    Options(string, ...Handler) Route
    // Head adds a route for a HTTP HEAD request to the specified matching pattern.
    Head(string, ...Handler) Route
    // Any adds a route for any HTTP method request to the specified matching pattern.
    Any(string, ...Handler) Route
    // AddRoute adds a route for a given HTTP method request to the specified matching pattern.
    AddRoute(string, string, ...Handler) Route

    // NotFound sets the handlers that are called when a no route matches a request. Throws a basic 404 by default.
    NotFound(...Handler)

    // Handle is the entry point for routing. This is used as a martini.Handler
    Handle(http.ResponseWriter, *http.Request, Context)
}

Router is Martini's de-facto routing interface. Supports HTTP verbs, stacked handlers, and dependency injection.

func NewRouter Uses

func NewRouter() Router

NewRouter creates a new Router instance. If you aren't using ClassicMartini, then you can add Routes as a service with:

m := martini.New()
r := martini.NewRouter()
m.MapTo(r, (*martini.Routes)(nil))

If you are using ClassicMartini, then this is done for you.

type Routes Uses

type Routes interface {
    // URLFor returns a rendered URL for the given route. Optional params can be passed to fulfill named parameters in the route.
    URLFor(name string, params ...interface{}) string
    // MethodsFor returns an array of methods available for the path
    MethodsFor(path string) []string
    // All returns an array with all the routes in the router.
    All() []Route
}

Routes is a helper service for Martini's routing layer.

type StaticOptions Uses

type StaticOptions struct {
    // Prefix is the optional prefix used to serve the static directory content
    Prefix string
    // SkipLogging will disable [Static] log messages when a static file is served.
    SkipLogging bool
    // IndexFile defines which file to serve as index if it exists.
    IndexFile string
    // Expires defines which user-defined function to use for producing a HTTP Expires Header
    // https://developers.google.com/speed/docs/insights/LeverageBrowserCaching
    Expires func() string
    // Fallback defines a default URL to serve when the requested resource was
    // not found.
    Fallback string
    // Exclude defines a pattern for URLs this handler should never process.
    Exclude string
}

StaticOptions is a struct for specifying configuration options for the martini.Static middleware.

Package martini imports 19 packages (graph) and is imported by 1855 packages. Updated 2017-01-21. Refresh now. Tools for package owners.