Skip to content

Commit 77d33d2

Browse files
author
Charlie Cooksey
committed
Revert "[docs] Document evaluation order for routes (gorilla#297)"
This reverts commit 3f19343.
1 parent 5bbbb5b commit 77d33d2

File tree

10 files changed

+419
-1080
lines changed

10 files changed

+419
-1080
lines changed

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ sudo: false
33

44
matrix:
55
include:
6+
- go: 1.2
7+
- go: 1.3
8+
- go: 1.4
69
- go: 1.5
710
- go: 1.6
811
- go: 1.7
912
- go: 1.8
10-
- go: 1.9
11-
- go: tip
12-
allow_failures:
1313
- go: tip
1414

1515
install:

ISSUE_TEMPLATE.md

Lines changed: 0 additions & 11 deletions
This file was deleted.

README.md

Lines changed: 34 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ The name mux stands for "HTTP request multiplexer". Like the standard `http.Serv
2727
* [Static Files](#static-files)
2828
* [Registered URLs](#registered-urls)
2929
* [Walking Routes](#walking-routes)
30-
* [Graceful Shutdown](#graceful-shutdown)
3130
* [Full Example](#full-example)
3231

3332
---
@@ -46,11 +45,11 @@ Let's start registering a couple of URL paths and handlers:
4645

4746
```go
4847
func main() {
49-
r := mux.NewRouter()
50-
r.HandleFunc("/", HomeHandler)
51-
r.HandleFunc("/products", ProductsHandler)
52-
r.HandleFunc("/articles", ArticlesHandler)
53-
http.Handle("/", r)
48+
r := mux.NewRouter()
49+
r.HandleFunc("/", HomeHandler)
50+
r.HandleFunc("/products", ProductsHandler)
51+
r.HandleFunc("/articles", ArticlesHandler)
52+
http.Handle("/", r)
5453
}
5554
```
5655

@@ -69,9 +68,9 @@ The names are used to create a map of route variables which can be retrieved cal
6968

7069
```go
7170
func ArticlesCategoryHandler(w http.ResponseWriter, r *http.Request) {
72-
vars := mux.Vars(r)
73-
w.WriteHeader(http.StatusOK)
74-
fmt.Fprintf(w, "Category: %v\n", vars["category"])
71+
vars := mux.Vars(r)
72+
w.WriteHeader(http.StatusOK)
73+
fmt.Fprintf(w, "Category: %v\n", vars["category"])
7574
}
7675
```
7776

@@ -123,7 +122,7 @@ r.Queries("key", "value")
123122

124123
```go
125124
r.MatcherFunc(func(r *http.Request, rm *RouteMatch) bool {
126-
return r.ProtoMajor == 0
125+
return r.ProtoMajor == 0
127126
})
128127
```
129128

@@ -136,14 +135,6 @@ r.HandleFunc("/products", ProductsHandler).
136135
Schemes("http")
137136
```
138137

139-
Routes are tested in the order they were added to the router. If two routes match, the first one wins:
140-
141-
```go
142-
r := mux.NewRouter()
143-
r.HandleFunc("/specific", specificHandler)
144-
r.PathPrefix("/").Handler(catchAllHandler)
145-
```
146-
147138
Setting the same matching conditions again and again can be boring, so we have a way to group several routes that share the same requirements. We call it "subrouting".
148139

149140
For example, let's say we have several URLs that should only match when the host is `www.example.com`. Create a route for that host and get a "subrouter" from it:
@@ -202,34 +193,22 @@ func main() {
202193
r.HandleFunc("/products", handler).Methods("POST")
203194
r.HandleFunc("/articles", handler).Methods("GET")
204195
r.HandleFunc("/articles/{id}", handler).Methods("GET", "PUT")
205-
r.HandleFunc("/authors", handler).Queries("surname", "{surname}")
206196
r.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
207197
t, err := route.GetPathTemplate()
208198
if err != nil {
209199
return err
210200
}
211-
qt, err := route.GetQueriesTemplates()
212-
if err != nil {
213-
return err
214-
}
215201
// p will contain regular expression is compatible with regular expression in Perl, Python, and other languages.
216202
// for instance the regular expression for path '/articles/{id}' will be '^/articles/(?P<v0>[^/]+)$'
217203
p, err := route.GetPathRegexp()
218204
if err != nil {
219205
return err
220206
}
221-
// qr will contain a list of regular expressions with the same semantics as GetPathRegexp,
222-
// just applied to the Queries pairs instead, e.g., 'Queries("surname", "{surname}") will return
223-
// {"^surname=(?P<v0>.*)$}. Where each combined query pair will have an entry in the list.
224-
qr, err := route.GetQueriesRegexp()
225-
if err != nil {
226-
return err
227-
}
228207
m, err := route.GetMethods()
229208
if err != nil {
230209
return err
231210
}
232-
fmt.Println(strings.Join(m, ","), strings.Join(qt, ","), strings.Join(qr, ","), t, p)
211+
fmt.Println(strings.Join(m, ","), t, p)
233212
return nil
234213
})
235214
http.Handle("/", r)
@@ -244,24 +223,24 @@ request that matches "/static/*". This makes it easy to serve static files with
244223

245224
```go
246225
func main() {
247-
var dir string
226+
var dir string
248227

249-
flag.StringVar(&dir, "dir", ".", "the directory to serve files from. Defaults to the current dir")
250-
flag.Parse()
251-
r := mux.NewRouter()
228+
flag.StringVar(&dir, "dir", ".", "the directory to serve files from. Defaults to the current dir")
229+
flag.Parse()
230+
r := mux.NewRouter()
252231

253-
// This will serve files under http://localhost:8000/static/<filename>
254-
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(dir))))
232+
// This will serve files under http://localhost:8000/static/<filename>
233+
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(dir))))
255234

256-
srv := &http.Server{
257-
Handler: r,
258-
Addr: "127.0.0.1:8000",
259-
// Good practice: enforce timeouts for servers you create!
260-
WriteTimeout: 15 * time.Second,
261-
ReadTimeout: 15 * time.Second,
262-
}
235+
srv := &http.Server{
236+
Handler: r,
237+
Addr: "127.0.0.1:8000",
238+
// Good practice: enforce timeouts for servers you create!
239+
WriteTimeout: 15 * time.Second,
240+
ReadTimeout: 15 * time.Second,
241+
}
263242

264-
log.Fatal(srv.ListenAndServe())
243+
log.Fatal(srv.ListenAndServe())
265244
}
266245
```
267246

@@ -352,101 +331,26 @@ r.HandleFunc("/", handler)
352331
r.HandleFunc("/products", handler).Methods("POST")
353332
r.HandleFunc("/articles", handler).Methods("GET")
354333
r.HandleFunc("/articles/{id}", handler).Methods("GET", "PUT")
355-
r.HandleFunc("/authors", handler).Queries("surname", "{surname}")
356334
r.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
357335
t, err := route.GetPathTemplate()
358336
if err != nil {
359337
return err
360338
}
361-
qt, err := route.GetQueriesTemplates()
362-
if err != nil {
363-
return err
364-
}
365339
// p will contain a regular expression that is compatible with regular expressions in Perl, Python, and other languages.
366340
// For example, the regular expression for path '/articles/{id}' will be '^/articles/(?P<v0>[^/]+)$'.
367341
p, err := route.GetPathRegexp()
368342
if err != nil {
369343
return err
370344
}
371-
// qr will contain a list of regular expressions with the same semantics as GetPathRegexp,
372-
// just applied to the Queries pairs instead, e.g., 'Queries("surname", "{surname}") will return
373-
// {"^surname=(?P<v0>.*)$}. Where each combined query pair will have an entry in the list.
374-
qr, err := route.GetQueriesRegexp()
375-
if err != nil {
376-
return err
377-
}
378345
m, err := route.GetMethods()
379346
if err != nil {
380347
return err
381348
}
382-
fmt.Println(strings.Join(m, ","), strings.Join(qt, ","), strings.Join(qr, ","), t, p)
349+
fmt.Println(strings.Join(m, ","), t, p)
383350
return nil
384351
})
385352
```
386353

387-
### Graceful Shutdown
388-
389-
Go 1.8 introduced the ability to [gracefully shutdown](https://golang.org/doc/go1.8#http_shutdown) a `*http.Server`. Here's how to do that alongside `mux`:
390-
391-
```go
392-
package main
393-
394-
import (
395-
"context"
396-
"flag"
397-
"log"
398-
"net/http"
399-
"os"
400-
"os/signal"
401-
402-
"github.com/gorilla/mux"
403-
)
404-
405-
func main() {
406-
var wait time.Duration
407-
flag.DurationVar(&wait, "graceful-timeout", time.Second * 15, "the duration for which the server gracefully wait for existing connections to finish - e.g. 15s or 1m")
408-
flag.Parse()
409-
410-
r := mux.NewRouter()
411-
// Add your routes as needed
412-
413-
srv := &http.Server{
414-
Addr: "0.0.0.0:8080",
415-
// Good practice to set timeouts to avoid Slowloris attacks.
416-
WriteTimeout: time.Second * 15,
417-
ReadTimeout: time.Second * 15,
418-
IdleTimeout: time.Second * 60,
419-
Handler: r, // Pass our instance of gorilla/mux in.
420-
}
421-
422-
// Run our server in a goroutine so that it doesn't block.
423-
go func() {
424-
if err := srv.ListenAndServe(); err != nil {
425-
log.Println(err)
426-
}
427-
}()
428-
429-
c := make(chan os.Signal, 1)
430-
// We'll accept graceful shutdowns when quit via SIGINT (Ctrl+C)
431-
// SIGKILL, SIGQUIT or SIGTERM (Ctrl+/) will not be caught.
432-
signal.Notify(c, os.Interrupt)
433-
434-
// Block until we receive our signal.
435-
<-c
436-
437-
// Create a deadline to wait for.
438-
ctx, cancel := context.WithTimeout(ctx, wait)
439-
// Doesn't block if no connections, but will otherwise wait
440-
// until the timeout deadline.
441-
srv.Shutdown(ctx)
442-
// Optionally, you could run srv.Shutdown in a goroutine and block on
443-
// <-ctx.Done() if your application should wait for other services
444-
// to finalize based on context cancellation.
445-
log.Println("shutting down")
446-
os.Exit(0)
447-
}
448-
```
449-
450354
## Full Example
451355

452356
Here's a complete, runnable example of a small `mux` based server:
@@ -455,22 +359,22 @@ Here's a complete, runnable example of a small `mux` based server:
455359
package main
456360

457361
import (
458-
"net/http"
459-
"log"
460-
"github.com/gorilla/mux"
362+
"net/http"
363+
"log"
364+
"github.com/gorilla/mux"
461365
)
462366

463367
func YourHandler(w http.ResponseWriter, r *http.Request) {
464-
w.Write([]byte("Gorilla!\n"))
368+
w.Write([]byte("Gorilla!\n"))
465369
}
466370

467371
func main() {
468-
r := mux.NewRouter()
469-
// Routes consist of a path and a handler function.
470-
r.HandleFunc("/", YourHandler)
372+
r := mux.NewRouter()
373+
// Routes consist of a path and a handler function.
374+
r.HandleFunc("/", YourHandler)
471375

472-
// Bind to a port and pass our router in
473-
log.Fatal(http.ListenAndServe(":8000", r))
376+
// Bind to a port and pass our router in
377+
log.Fatal(http.ListenAndServe(":8000", r))
474378
}
475379
```
476380

example_route_test.go

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)