Initial commit

This commit is contained in:
Anton Zadvorny 2020-09-30 19:40:06 +03:00
commit 7c06c3c6d8
3 changed files with 45 additions and 0 deletions

19
app_info.go Normal file
View File

@ -0,0 +1,19 @@
package middleware
import (
"net/http"
)
// AppInfo adds application name and version headers to the response
func AppInfo(name string, version string) func(http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("App-Name", name)
w.Header().Set("App-Version", version)
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
}

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module go.pkg.cx/middleware
go 1.15

23
ping.go Normal file
View File

@ -0,0 +1,23 @@
package middleware
import (
"net/http"
"strings"
)
// Ping responses with pong to /ping request and stops chain
func Ping(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" && strings.HasSuffix(strings.ToLower(r.URL.Path), "/ping") {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
w.Write([]byte("pong")) // nolint:errcheck
return
}
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}