Skip to content
This repository was archived by the owner on Jul 31, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
# golang-template
# middlechain

The simple HTTP middleware chainer for Golang.

## How to use

```go
mux := http.NewServeMux()

handler := middlechain.Chain(mux,
someMiddleware,
)

// handle http request...

http.ListenAndServe(":8080", handler)
```
34 changes: 34 additions & 0 deletions _example/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"net/http"
"strings"

"github.com/Pranc1ngPegasus/middlechain"
)

func main() {
mux := http.NewServeMux()

handler := middlechain.Chain(mux,
heaatbeat("/ping"),
)

if err := http.ListenAndServe(":8080", handler); err != nil {
panic(err)
}
}

func heaatbeat(endpoint string) func(h http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if (r.Method == http.MethodGet || r.Method == http.MethodHead) && strings.EqualFold(r.URL.Path, endpoint) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
w.Write([]byte("."))

return
}
})
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/Pranc1ngPegasus/golang-template
module github.com/Pranc1ngPegasus/middlechain

go 1.19

Expand Down