diff --git a/README.md b/README.md index 055f85c..2200fbd 100644 --- a/README.md +++ b/README.md @@ -1 +1,17 @@ -# golang-template \ No newline at end of file +# 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) +``` diff --git a/_example/example.go b/_example/example.go new file mode 100644 index 0000000..5a7d30d --- /dev/null +++ b/_example/example.go @@ -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 + } + }) + } +} diff --git a/go.mod b/go.mod index d8702f0..161cd38 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/Pranc1ngPegasus/golang-template +module github.com/Pranc1ngPegasus/middlechain go 1.19