Documentation ¶
Overview ¶
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 ¶
- Constants
- Variables
- type BeforeFunc
- type ClassicMartini
- type Context
- type Handler
- type Martini
- func (m *Martini) Action(handler Handler)
- func (m *Martini) Handlers(handlers ...Handler)
- func (m *Martini) Logger(logger *log.Logger)
- func (m *Martini) Run()
- func (m *Martini) RunOnAddr(addr string)
- func (m *Martini) ServeHTTP(res http.ResponseWriter, req *http.Request)
- func (m *Martini) Use(handler Handler)
- type Params
- type ResponseWriter
- type ReturnHandler
- type Route
- type RouteMatch
- type Router
- type Routes
- type StaticOptions
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
Functions ¶
This section is empty.
Types ¶
type BeforeFunc ¶
type BeforeFunc func(ResponseWriter)
BeforeFunc is a function that is called before the ResponseWriter has been written to.
type ClassicMartini ¶
ClassicMartini represents a Martini with some reasonable defaults. Embeds the router functions for convenience.
func Classic ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
func Static(directory string, staticOpt ...StaticOptions) Handler
Static returns a middleware handler that serves static files in the given directory.
type Martini ¶
Martini represents the top level web application. inject.Injector methods can be invoked to map services on a global level.
func New ¶
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 ¶
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 ¶
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) Run ¶
func (m *Martini) Run()
Run the http server. Listening on os.GetEnv("PORT") or 3000 by default.
type Params ¶
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 ¶
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 ¶
func NewResponseWriter(rw http.ResponseWriter) ResponseWriter
NewResponseWriter creates a ResponseWriter that wraps an http.ResponseWriter
type ReturnHandler ¶
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 ¶
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 ¶
type RouteMatch int
const ( NoMatch RouteMatch = iota StarMatch OverloadMatch ExactMatch )
func (RouteMatch) BetterThan ¶
func (r RouteMatch) BetterThan(o RouteMatch) bool
Higher number = better match
type Router ¶
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.
type Routes ¶
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 ¶
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.