diff options
author | Ben Morrison <ben@gbmor.dev> | 2019-05-20 04:22:17 -0400 |
---|---|---|
committer | Ben Morrison <ben@gbmor.dev> | 2019-05-20 04:22:17 -0400 |
commit | 1168570b071d8bb483e4028f3a8c45a222c93ecb (patch) | |
tree | 63d80521492c44a2af1b4918a67ff24e29006196 /httpmeta.go | |
parent | fc49c5701fde7d0c9dd2fa321ba0ebbff712b08a (diff) | |
download | getwtxt-1168570b071d8bb483e4028f3a8c45a222c93ecb.tar.gz |
renamed files for clarity
Diffstat (limited to 'httpmeta.go')
-rw-r--r-- | httpmeta.go | 60 |
1 files changed, 0 insertions, 60 deletions
diff --git a/httpmeta.go b/httpmeta.go deleted file mode 100644 index 2a733aa..0000000 --- a/httpmeta.go +++ /dev/null @@ -1,60 +0,0 @@ -package main - -import ( - "context" - "log" - "net/http" - "strings" -) - -// Attaches a request's IP address to the request's context -func newCtxUserIP(ctx context.Context, r *http.Request) context.Context { - - base := strings.Split(r.RemoteAddr, ":") - uip := base[0] - - return context.WithValue(ctx, ctxKey, uip) -} - -// Retrieves a request's IP address from the request's context -func getIPFromCtx(ctx context.Context) string { - - uip, ok := ctx.Value(ctxKey).(string) - if !ok { - log.Printf("Couldn't retrieve IP from request\n") - } - - return uip -} - -// Shim function to modify/pass context value to a handler -func ipMiddleware(hop http.Handler) http.Handler { - - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - ctx := newCtxUserIP(r.Context(), r) - hop.ServeHTTP(w, r.WithContext(ctx)) - }) -} - -// log output for 200s -func log200(r *http.Request) { - - uip := getIPFromCtx(r.Context()) - log.Printf("*** %v :: 200 :: %v %v\n", uip, r.Method, r.URL) -} - -// log output for 404s -func log404(w http.ResponseWriter, r *http.Request, err error) { - - uip := getIPFromCtx(r.Context()) - log.Printf("*** %v :: 404 :: %v %v :: %v\n", uip, r.Method, r.URL, err) - http.Error(w, err.Error(), http.StatusNotFound) -} - -// log output for 500s -func log500(w http.ResponseWriter, r *http.Request, err error) { - - uip := getIPFromCtx(r.Context()) - log.Printf("*** %v :: 500 :: %v %v :: %v\n", uip, r.Method, r.URL, err) - http.Error(w, err.Error(), http.StatusInternalServerError) -} |