You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
-
147
138
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".
148
139
149
140
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:
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
-
funcmain() {
406
-
varwait 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
-
gofunc() {
424
-
iferr:= 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
-
450
354
## Full Example
451
355
452
356
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:
455
359
package main
456
360
457
361
import (
458
-
"net/http"
459
-
"log"
460
-
"github.com/gorilla/mux"
362
+
"net/http"
363
+
"log"
364
+
"github.com/gorilla/mux"
461
365
)
462
366
463
367
funcYourHandler(whttp.ResponseWriter, r *http.Request) {
464
-
w.Write([]byte("Gorilla!\n"))
368
+
w.Write([]byte("Gorilla!\n"))
465
369
}
466
370
467
371
funcmain() {
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.
0 commit comments