Skip to content
This repository was archived by the owner on Jul 31, 2024. It is now read-only.

Commit 6af93eb

Browse files
feat: implement example and README (#3)
* fix: go module name * feat: implement example usage * feat: write README
1 parent d9774df commit 6af93eb

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
1-
# golang-template
1+
# middlechain
2+
3+
The simple HTTP middleware chainer for Golang.
4+
5+
## How to use
6+
7+
```go
8+
mux := http.NewServeMux()
9+
10+
handler := middlechain.Chain(mux,
11+
someMiddleware,
12+
)
13+
14+
// handle http request...
15+
16+
http.ListenAndServe(":8080", handler)
17+
```

_example/example.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
"strings"
6+
7+
"github.com/Pranc1ngPegasus/middlechain"
8+
)
9+
10+
func main() {
11+
mux := http.NewServeMux()
12+
13+
handler := middlechain.Chain(mux,
14+
heaatbeat("/ping"),
15+
)
16+
17+
if err := http.ListenAndServe(":8080", handler); err != nil {
18+
panic(err)
19+
}
20+
}
21+
22+
func heaatbeat(endpoint string) func(h http.Handler) http.Handler {
23+
return func(h http.Handler) http.Handler {
24+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
25+
if (r.Method == http.MethodGet || r.Method == http.MethodHead) && strings.EqualFold(r.URL.Path, endpoint) {
26+
w.Header().Set("Content-Type", "text/plain")
27+
w.WriteHeader(http.StatusOK)
28+
w.Write([]byte("."))
29+
30+
return
31+
}
32+
})
33+
}
34+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/Pranc1ngPegasus/golang-template
1+
module github.com/Pranc1ngPegasus/middlechain
22

33
go 1.19
44

0 commit comments

Comments
 (0)