Skip to content

Commit

Permalink
Updated for v0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
nikita-shtimenko committed Feb 23, 2024
1 parent 8b4941c commit 1cfbe63
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
<div align="center">
<img alt="httpmux" src="./logo.svg" width="256" />
<p>A tiny wrapper on top of standart http.ServeMux for Go web applications</p>
<p>A tiny wrapper on top of standart http.ServeMux designed for Go web applications (1.22+)</p>
</div>

---

httpmux packs small set of features that you'll probably need:

- Register handlers the **default** way with Handle and HandleFunc:

```go
mux := httpmux.New()
mux.Handle("GET /", http.HandlerFunc(exampleHandlerGet))
mux.HandleFunc("POST /", exampleHandlerPost)
// etc.
```

or **opt-in** to register handlers with provided methods:

```go
mux := httpmux.New()
mux.Get("/", exampleHandlerGet)
mux.Post("/", exampleHandlerPost)
mux.Put("/", exampleHandlerPut)
mux.Delete("/", exampleHandlerDelete)
mux.Head("/", exampleHandlerHead)
mux.Options("/", exampleHandlerOptions)
```

- Create route **groups which use different middleware**.
- **Customizable handler** for `404 Not Found` response.
- Works with `http.Handler`, `http.HandlerFunc`, and standard Go middleware.
- Zero dependencies.
- Tiny and readable codebase (~60 lines of code).
- Tiny and readable codebase (~90 lines of code).

---

Expand All @@ -26,7 +47,7 @@ go get github.com/nikita-shtimenko/httpmux@latest
```go
mux := httpmux.New()

// You can customize the deafult 'not found' handler to your handler.
// You can customize the deafult 'not found' handler.
mux.NotFound = http.HandlerFunc(handlerNotFound)

// The Use() method can be used to register middleware.
Expand All @@ -53,8 +74,8 @@ mux.Group(func(mux *httpmux.Mux) {
})

func handlerNotFound(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(http.StatusText(http.StatusNotFound)))
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(http.StatusText(http.StatusNotFound)))
}

func exampleHandlerFunc1(w http.ResponseWriter, r *http.Request) {
Expand All @@ -65,7 +86,6 @@ func exampleHandlerFunc1(w http.ResponseWriter, r *http.Request) {

### Notes

- httpmux is a thin wrapper over the standard http.ServeMux, so that the routing declaration and behavior remains standard.
- Middleware must be declared _before_ a route in order to be used by that route. Any middleware declared after a route won't act on that route. For example:

```go
Expand Down

0 comments on commit 1cfbe63

Please sign in to comment.