Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
googollee committed Jan 30, 2014
2 parents a119163 + 9eab46a commit c48f35e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ func init() {

### How do I change the port/host?

Martini's `Run` function looks for the PORT environment variable and uses that. Otherwise Martini will default to port 3000.
Martini's `Run` function looks for the PORT and HOST environment variables and uses those. Otherwise Martini will default to localhost:3000.
To have more flexibility over port and host, use the `http.ListenAndServe` function instead.

~~~ go
Expand Down
9 changes: 7 additions & 2 deletions martini.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,13 @@ func (m *Martini) Run() {
port = "3000"
}

m.logger.Println("listening on port " + port)
m.logger.Fatalln(http.ListenAndServe(":"+port, m))
host := os.Getenv("HOST")
if len(host) == 0 {
host = ""
}

m.logger.Println("listening on host:port " + host + ":" + port)
m.logger.Fatalln(http.ListenAndServe(host+":"+port, m))
}

func (m *Martini) createContext(res http.ResponseWriter, req *http.Request) *context {
Expand Down
41 changes: 31 additions & 10 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,9 @@ func (r *router) Handle(res http.ResponseWriter, req *http.Request, context Cont
if ok {
params := Params(vals)
context.Map(params)
r := routes{}
r := routes{r}
context.MapTo(r, (*Routes)(nil))
_, err := context.Invoke(route.Handle)
if err != nil {
panic(err)
}
route.Handle(context, res)
return
}
}
Expand All @@ -112,21 +109,33 @@ func (r *router) addRoute(method string, pattern string, handlers []Handler) *ro
return route
}

func (r *router) findRoute(name string) *route {
for _, route := range r.routes {
if route.name == name {
return route
}
}

return nil
}

// Route is an interface representing a Route in Martini's routing layer.
type Route interface {
// URLWith returns a rendering of the Route's url with the given string params.
URLWith([]string) string
Name(string)
}

type route struct {
method string
regex *regexp.Regexp
handlers []Handler
pattern string
name string
}

func newRoute(method string, pattern string, handlers []Handler) *route {
route := route{method, nil, handlers, pattern}
route := route{method, nil, handlers, pattern, ""}
r := regexp.MustCompile(`:[^/#?()\.\\]+`)
pattern = r.ReplaceAllStringFunc(pattern, func(m string) string {
return fmt.Sprintf(`(?P<%s>[^/#?]+)`, m[1:])
Expand Down Expand Up @@ -199,16 +208,28 @@ func (r *route) URLWith(args []string) string {
return r.pattern
}

func (r *route) Name(name string) {
r.name = name
}

// Routes is a helper service for Martini's routing layer.
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(route Route, params ...interface{}) string
URLFor(name string, params ...interface{}) string
}

type routes struct{}
type routes struct {
router *router
}

// URLFor returns the url for the given route name.
func (r routes) URLFor(route Route, params ...interface{}) string {
func (r routes) URLFor(name string, params ...interface{}) string {
route := r.router.findRoute(name)

if route == nil {
panic("route not found")
}

var args []string
for _, param := range params {
switch v := param.(type) {
Expand All @@ -218,7 +239,7 @@ func (r routes) URLFor(route Route, params ...interface{}) string {
args = append(args, v)
default:
if v != nil {
panic("Arguments passed to UrlFor must be integers or strings")
panic("Arguments passed to URLFor must be integers or strings")
}
}
}
Expand Down
19 changes: 9 additions & 10 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,21 +333,20 @@ func Test_Any(t *testing.T) {

func Test_URLFor(t *testing.T) {
router := NewRouter()
var barIDNameRoute, fooRoute, barRoute Route

fooRoute = router.Get("/foo", func() {
router.Get("/foo", func() {
// Nothing
})
}).Name("foo")

barRoute = router.Post("/bar/:id", func(params Params) {
router.Post("/bar/:id", func(params Params) {
// Nothing
})
}).Name("bar")

barIDNameRoute = router.Get("/bar/:id/:name", func(params Params, routes Routes) {
expect(t, routes.URLFor(fooRoute, nil), "/foo")
expect(t, routes.URLFor(barRoute, 5), "/bar/5")
expect(t, routes.URLFor(barIDNameRoute, 5, "john"), "/bar/5/john")
})
router.Get("/bar/:id/:name", func(params Params, routes Routes) {
expect(t, routes.URLFor("foo", nil), "/foo")
expect(t, routes.URLFor("bar", 5), "/bar/5")
expect(t, routes.URLFor("bar_id", 5, "john"), "/bar/5/john")
}).Name("bar_id")

// code should be 200 if none is returned from the handler
recorder := httptest.NewRecorder()
Expand Down

0 comments on commit c48f35e

Please sign in to comment.