Skip to content

Commit ffba94e

Browse files
Document the package
1 parent 3f8d9cd commit ffba94e

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,18 @@ Methodmux has been written with readability in mind and is just as fast and effi
2222
## Usage
2323

2424
```Go
25+
package main
26+
27+
import (
28+
"net/http"
29+
30+
methodmux "github.com/pierreprinetti/go-methodmux"
31+
)
32+
2533
func main() {
2634
mux := methodmux.New()
2735

28-
getHandler := myNewGetHandler()
36+
getHandler := myNewGetHandler()
2937

3038
mux.Handle(http.MethodGet, "/some-endpoint/", getHandler)
3139
mux.HandleFunc(http.MethodPost, "/some-endpoint/", func(rw http.ResponseWriter, req *http.Request) {

mux.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,43 @@
1-
// Package methodmux provides a method-aware HTTP router based on net/http.
1+
/*
2+
Package methodmux provides a method-aware HTTP router based on net/http.
3+
4+
Example usage:
5+
6+
package main
7+
8+
import (
9+
"net/http"
10+
11+
methodmux "github.com/pierreprinetti/go-methodmux"
12+
)
13+
14+
func main() {
15+
mux := methodmux.New()
16+
17+
getHandler := myNewGetHandler()
18+
19+
mux.Handle(http.MethodGet, "/some-endpoint/", getHandler)
20+
mux.HandleFunc(http.MethodPost, "/some-endpoint/", func(rw http.ResponseWriter, req *http.Request) {
21+
fmt.Fprintf(rw, "Hi! This the response to a POST call.")
22+
})
23+
24+
srv := &http.Server{
25+
Handler: mux,
26+
ReadTimeout: 10 * time.Second,
27+
WriteTimeout: 10 * time.Second,
28+
MaxHeaderBytes: 1 << 20,
29+
}
30+
31+
log.Fatal(srv.ListenAndServe())
32+
}
33+
34+
Methodmux exposes a single type: `ServeMux`. `ServeMux` holds a separate `http.ServeMux` for every HTTP verb an http.Handler has been registered to.
35+
36+
Every new request will be matched against the underlying `http.ServeMux` that corresponds to the HTTP method of the request.
37+
If no match is found, `ServeMux` will look for a match in the other HTTP verbs. If a match is found, an HTTP code 405 "Method Not Allowed" is returned. If not, an HTTP code 404 "Not Found" is returned.
38+
39+
Methodmux has been written with readability in mind and is just as fast and efficient as `net/http` is.
40+
*/
241
package methodmux // import "github.com/pierreprinetti/go-methodmux"
342

443
import (

0 commit comments

Comments
 (0)