https://github.com/rikonor/go-status-code-tracker
Tiny wrapper for ResponseWriter to track response status code
https://github.com/rikonor/go-status-code-tracker
golang http status-code
Last synced: 6 months ago
JSON representation
Tiny wrapper for ResponseWriter to track response status code
- Host: GitHub
- URL: https://github.com/rikonor/go-status-code-tracker
- Owner: rikonor
- License: mit
- Created: 2017-01-28T04:49:04.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-01-28T05:33:34.000Z (almost 9 years ago)
- Last Synced: 2025-01-09T16:19:02.849Z (about 1 year ago)
- Topics: golang, http, status-code
- Language: Go
- Size: 2.93 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Status Code Tracker
---
Small utility for getting the status code of a finished http request.
### Usage
```go
package main
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
status "github.com/rikonor/go-status-code-tracker"
)
func main() {
r := mux.NewRouter()
r.HandleFunc("/", mainHandler)
http.Handle("/", middleware(r))
http.ListenAndServe(":8080", nil)
}
func middleware(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// wrap the ResponseWriter with a StatusCodeTracker
t := status.Track(w)
h.ServeHTTP(t, r)
// Can now get the response status code
fmt.Println(t.Status())
})
}
func mainHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "error", 400)
}
```