{"id":30793997,"url":"https://github.com/sendgrid/martini","last_synced_at":"2025-09-05T16:26:32.270Z","repository":{"id":15609936,"uuid":"18346277","full_name":"sendgrid/martini","owner":"sendgrid","description":null,"archived":false,"fork":false,"pushed_at":"2017-10-28T20:41:12.000Z","size":152,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":189,"default_branch":"master","last_synced_at":"2025-08-13T23:56:42.928Z","etag":null,"topics":["mailpipeline"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sendgrid.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-04-01T21:55:42.000Z","updated_at":"2023-06-13T07:01:25.000Z","dependencies_parsed_at":"2022-09-08T04:51:41.699Z","dependency_job_id":null,"html_url":"https://github.com/sendgrid/martini","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/sendgrid/martini","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sendgrid%2Fmartini","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sendgrid%2Fmartini/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sendgrid%2Fmartini/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sendgrid%2Fmartini/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sendgrid","download_url":"https://codeload.github.com/sendgrid/martini/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sendgrid%2Fmartini/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273782730,"owners_count":25167204,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-05T02:00:09.113Z","response_time":402,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mailpipeline"],"created_at":"2025-09-05T16:26:26.707Z","updated_at":"2025-09-05T16:26:32.233Z","avatar_url":"https://github.com/sendgrid.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Martini  [![wercker status](https://app.wercker.com/status/174bef7e3c999e103cacfe2770102266 \"wercker status\")](https://app.wercker.com/project/bykey/174bef7e3c999e103cacfe2770102266) [![Codebot](https://codebot.io/badge/github.com/codegangsta/martini.png)](http://codebot.io/doc/pkg/github.com/codegangsta/martini \"Codebot\") [![GoDoc](https://godoc.org/github.com/codegangsta/martini?status.png)](http://godoc.org/github.com/codegangsta/martini)\n\nMartini is a powerful package for quickly writing modular web applications/services in Golang.\n\n## Getting Started\n\nAfter installing Go and setting up your [GOPATH](http://golang.org/doc/code.html#GOPATH), create your first `.go` file. We'll call it `server.go`.\n\n~~~ go\npackage main\n\nimport \"github.com/codegangsta/martini\"\n\nfunc main() {\n  m := martini.Classic()\n  m.Get(\"/\", func() string {\n    return \"Hello world!\"\n  })\n  m.Run()\n}\n~~~\n\nThen install the Martini package (**go 1.1** and greater is required):\n~~~\ngo get github.com/codegangsta/martini\n~~~\n\nThen run your server:\n~~~\ngo run server.go\n~~~\n\nYou will now have a Martini webserver running on `localhost:3000`.\n\n## Getting Help\n\nJoin the [Mailing list](https://groups.google.com/forum/#!forum/martini-go)\n\nWatch the [Demo Video](http://martini.codegangsta.io/#demo)\n\n## Features\n* Extremely simple to use.\n* Non-intrusive design.\n* Play nice with other Golang packages.\n* Awesome path matching and routing.\n* Modular design - Easy to add functionality, easy to rip stuff out.\n* Lots of good handlers/middlewares to use.\n* Great 'out of the box' feature set.\n* **Fully compatible with the [http.HandlerFunc](http://godoc.org/net/http#HandlerFunc) interface.**\n\n## More Middleware\nFor more middleware and functionality, check out the [martini-contrib](http://github.com/codegangsta/martini-contrib) repository.\n\n## Table of Contents\n* [Classic Martini](#classic-martini)\n  * [Handlers](#handlers)\n  * [Routing](#routing)\n  * [Services](#services)\n  * [Serving Static Files](#serving-static-files)\n* [Middleware Handlers](#middleware-handlers)\n  * [Next()](#next)\n* [FAQ](#faq)\n\n## Classic Martini\nTo get up and running quickly, [martini.Classic()](http://godoc.org/github.com/codegangsta/martini#Classic) provides some reasonable defaults that work well for most web applications:\n~~~ go\n  m := martini.Classic()\n  // ... middleware and routing goes here\n  m.Run()\n~~~\n\nBelow is some of the functionality [martini.Classic()](http://godoc.org/github.com/codegangsta/martini#Classic) pulls in automatically:\n  * Request/Response Logging - [martini.Logger](http://godoc.org/github.com/codegangsta/martini#Logger)\n  * Panic Recovery - [martini.Recovery](http://godoc.org/github.com/codegangsta/martini#Recovery)\n  * Static File serving - [martini.Static](http://godoc.org/github.com/codegangsta/martini#Static)\n  * Routing - [martini.Router](http://godoc.org/github.com/codegangsta/martini#Router)\n\n### Handlers\nHandlers are the heart and soul of Martini. A handler is basically any kind of callable function:\n~~~ go\nm.Get(\"/\", func() {\n  println(\"hello world\")\n})\n~~~\n\n#### Return Values\nIf a handler returns something, Martini will write the result to the current [http.ResponseWriter](http://godoc.org/net/http#ResponseWriter) as a string:\n~~~ go\nm.Get(\"/\", func() string {\n  return \"hello world\" // HTTP 200 : \"hello world\"\n})\n~~~\n\nYou can also optionally return a status code:\n~~~ go\nm.Get(\"/\", func() (int, string) {\n  return 418, \"i'm a teapot\" // HTTP 418 : \"i'm a teapot\"\n})\n~~~\n\n#### Service Injection\nHandlers are invoked via reflection. Martini makes use of *Dependency Injection* to resolve dependencies in a Handlers argument list. **This makes Martini completely  compatible with golang's `http.HandlerFunc` interface.** \n\nIf you add an argument to your Handler, Martini will search its list of services and attempt to resolve the dependency via type assertion:\n~~~ go\nm.Get(\"/\", func(res http.ResponseWriter, req *http.Request) { // res and req are injected by Martini\n  res.WriteHeader(200) // HTTP 200\n})\n~~~\n\nThe following services are included with [martini.Classic()](http://godoc.org/github.com/codegangsta/martini#Classic):\n  * [*log.Logger](http://godoc.org/log#Logger) - Global logger for Martini.\n  * [martini.Context](http://godoc.org/github.com/codegangsta/martini#Context) - http request context.\n  * [martini.Params](http://godoc.org/github.com/codegangsta/martini#Params) - `map[string]string` of named params found by route matching.\n  * [martini.Routes](http://godoc.org/github.com/codegangsta/martini#Routes) - Route helper service.\n  * [http.ResponseWriter](http://godoc.org/net/http/#ResponseWriter) - http Response writer interface.\n  * [*http.Request](http://godoc.org/net/http/#Request) - http Request.\n\n### Routing\nIn Martini, a route is an HTTP method paired with a URL-matching pattern.\nEach route can take one or more handler methods:\n~~~ go\nm.Get(\"/\", func() {\n  // show something\n})\n\nm.Patch(\"/\", func() {\n  // update something\n})\n\nm.Post(\"/\", func() {\n  // create something\n})\n\nm.Put(\"/\", func() {\n  // replace something\n})\n\nm.Delete(\"/\", func() {\n  // destroy something\n})\n\nm.Options(\"/\", func() {\n  // http options\n})\n\nm.NotFound(func() {\n  // handle 404\n})\n~~~\n\nRoutes are matched in the order they are defined. The first route that\nmatches the request is invoked.\n\nRoute patterns may include named parameters, accessible via the [martini.Params](http://godoc.org/github.com/codegangsta/martini#Params) service:\n~~~ go\nm.Get(\"/hello/:name\", func(params martini.Params) string {\n  return \"Hello \" + params[\"name\"]\n})\n~~~\n\nRoutes can be matched with regular expressions and globs as well:\n~~~ go\nm.Get(\"/hello/**\", func(params martini.Params) string {\n  return \"Hello \" + params[\"_1\"]\n})\n~~~\n\nRoute handlers can be stacked on top of each other, which is useful for things like authentication and authorization:\n~~~ go\nm.Get(\"/secret\", authorize, func() {\n  // this will execute as long as authorize doesn't write a response\n})\n~~~\n\n### Services\nServices are objects that are available to be injected into a Handler's argument list. You can map a service on a *Global* or *Request* level.\n\n#### Global Mapping\nA Martini instance implements the inject.Injector interface, so mapping a service is easy:\n~~~ go\ndb := \u0026MyDatabase{}\nm := martini.Classic()\nm.Map(db) // the service will be available to all handlers as *MyDatabase\n// ...\nm.Run()\n~~~\n\n#### Request-Level Mapping\nMapping on the request level can be done in a handler via [martini.Context](http://godoc.org/github.com/codegangsta/martini#Context):\n~~~ go\nfunc MyCustomLoggerHandler(c martini.Context, req *http.Request) {\n  logger := \u0026MyCustomLogger{req}\n  c.Map(logger) // mapped as *MyCustomLogger\n}\n~~~\n\n#### Mapping values to Interfaces\nOne of the most powerful parts about services is the ability to map a service to an interface. For instance, if you wanted to override the [http.ResponseWriter](http://godoc.org/net/http#ResponseWriter) with an object that wrapped it and performed extra operations, you can write the following handler:\n~~~ go\nfunc WrapResponseWriter(res http.ResponseWriter, c martini.Context) {\n  rw := NewSpecialResponseWriter(res)\n  c.MapTo(rw, (*http.ResponseWriter)(nil)) // override ResponseWriter with our wrapper ResponseWriter\n}\n~~~\n\n### Serving Static Files\nA [martini.Classic()](http://godoc.org/github.com/codegangsta/martini#Classic) instance automatically serves static files from the \"public\" directory in the root of your server.\nYou can serve from more directories by adding more [martini.Static](http://godoc.org/github.com/codegangsta/martini#Static) handlers.\n~~~ go\nm.Use(martini.Static(\"assets\")) // serve from the \"assets\" directory as well\n~~~\n\n## Middleware Handlers\nMiddleware Handlers sit between the incoming http request and the router. In essence they are no different than any other Handler in Martini. You can add a middleware handler to the stack like so:\n~~~ go\nm.Use(func() {\n  // do some middleware stuff\n})\n~~~\n\nYou can have full control over the middleware stack with the `Handlers` function. This will replace any handlers that have been previously set:\n~~~ go\nm.Handlers(\n  Middleware1,\n  Middleware2,\n  Middleware3,\n)\n~~~\n\nMiddleware Handlers work really well for things like logging, authorization, authentication, sessions, gzipping, error pages and any other operations that must happen before or after an http request:\n~~~ go\n// validate an api key\nm.Use(func(res http.ResponseWriter, req *http.Request) {\n  if req.Header.Get(\"X-API-KEY\") != \"secret123\" {\n    res.WriteHeader(http.StatusUnauthorized)\n  }\n})\n~~~\n\n### Next()\n[Context.Next()](http://godoc.org/github.com/codegangsta/martini#Context) 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:\n~~~ go\n// log before and after a request\nm.Use(func(c martini.Context, log *log.Logger){\n  log.Println(\"before a request\")\n\n  c.Next()\n  \n  log.Println(\"after a request\")\n})\n~~~\n\n## FAQ\n\n### Where do I find middleware X?\n\nStart by looking in the [martini-contrib](http://github.com/codegangsta/martini-contrib) package. If it is not there feel free to put up a Pull Request for one.\n\n* [auth](https://github.com/codegangsta/martini-contrib/tree/master/auth) - Handlers for authentication.\n* [binding](https://github.com/codegangsta/martini-contrib/tree/master/binding) - Handler for mapping/validating a raw request into a structure.\n* [gzip](https://github.com/codegangsta/martini-contrib/tree/master/gzip) - Handler for adding gzip compress to requests\n* [render](https://github.com/codegangsta/martini-contrib/tree/master/render) - Handler that provides a service for easily rendering JSON and HTML templates.\n* [acceptlang](https://github.com/codegangsta/martini-contrib/tree/master/acceptlang) - Handler for parsing the `Accept-Language` HTTP header.\n* [sessions](https://github.com/codegangsta/martini-contrib/tree/master/sessions) - Handler that provides a Session service.\n* [web](https://github.com/codegangsta/martini-contrib/tree/master/web) - web.go Context compatibility.\n* [strip](https://github.com/codegangsta/martini-contrib/tree/master/strip) - URL Prefix stripping.\n* [method](https://github.com/codegangsta/martini-contrib/tree/master/method) - HTTP method overriding via Header or form fields.\n* [secure](https://github.com/codegangsta/martini-contrib/tree/master/secure) - Implements a few quick security wins.\n* [encoder](https://github.com/codegangsta/martini-contrib/tree/master/encoder) - Encoder service for rendering data in several formats and content negotiation.\n\n### How do I integrate with existing servers?\n\nA Martini instance implements `http.Handler`, so it can easily be used to serve subtrees \non existing Go servers. For example this is a working Martini app for Google App Engine:\n\n~~~ go\npackage hello\n\nimport (\n  \"net/http\"\n  \"github.com/codegangsta/martini\"\n)\n\nfunc init() {\n  m := martini.Classic()\n  m.Get(\"/\", func() string {\n    return \"Hello world!\"\n  })\n  http.Handle(\"/\", m)\n}\n~~~\n\n### How do I change the port/host?\n\nMartini's `Run` function looks for the PORT and HOST environment variables and uses those. Otherwise Martini will default to localhost:3000.\nTo have more flexibility over port and host, use the `http.ListenAndServe` function instead.\n\n~~~ go\n  m := martini.Classic()\n  // ...\n  http.ListenAndServe(\":8080\", m)\n~~~\n\n## Contributing\nMartini is meant to be kept tiny and clean. Most contributions should end up in the [martini-contrib](http://github.com/codegangsta/martini-contrib) repository. If you do have a contribution for the core of Martini feel free to put up a Pull Request.\n\n## About\n\nInspired by [express](https://github.com/visionmedia/express) and [sinatra](https://github.com/sinatra/sinatra)\n\nMartini is obsessively designed by none other than the [Code Gangsta](http://codegangsta.io/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsendgrid%2Fmartini","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsendgrid%2Fmartini","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsendgrid%2Fmartini/lists"}