https://github.com/nanoninja/httptool
Go HTTP Tool Package
https://github.com/nanoninja/httptool
go golang handler helper http middleware module
Last synced: 6 months ago
JSON representation
Go HTTP Tool Package
- Host: GitHub
- URL: https://github.com/nanoninja/httptool
- Owner: nanoninja
- License: bsd-3-clause
- Created: 2020-08-03T14:38:25.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-08-04T08:18:41.000Z (about 5 years ago)
- Last Synced: 2025-02-14T15:36:03.506Z (8 months ago)
- Topics: go, golang, handler, helper, http, middleware, module
- Language: Go
- Homepage:
- Size: 9.77 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# HTTPTool
The HTTP tool is a simple extension of the net/http package written in Go.
It is not a Framework and offers additional. This package provides a slightly different handling of the Handler type by
exploiting the return to handle potential errors.The ResponseWriter type has been increased to retrieve the state of the HTTP response such as
code, length and whether the header has been written.[](https://golang.org/)
[](https://pkg.go.dev/github.com/nanoninja/httptool?tab=doc)
[](https://travis-ci.org/nanoninja/httptool)
[](https://coveralls.io/github/nanoninja/httptool?branch=master)
[](https://goreportcard.com/report/github.com/nanoninja/httptool)
[](https://codebeat.co/projects/github-com-nanoninja-httptool-master)## Installation
```shell script
go get github.com/nanoninja/httptool
```## Getting Started
```go
package mainimport (
"log"
"net/http"
"os""github.com/nanoninja/httptool"
)func logAccess(next http.Handler, logger *log.Logger) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)ip := httptool.ClientIP(r)
rw := w.(httptool.ResponseWriter)logger.Printf("[nanoninja] %s %s %s %d\n", ip, r.Method, r.RequestURI, rw.Status())
})
}func logError(next httptool.HandlerFunc, logger *log.Logger) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if err := next.ServeHTTP(w, r); err != nil {
logger.Println(err)
}
}
}func greeting(w http.ResponseWriter, _ *http.Request) error {
w.WriteHeader(http.StatusOK)_, err := w.Write([]byte("Hello, Gophers"))
return err
}func main() {
errLogger := log.New(os.Stderr, "", log.Lshortfile)
accessLogger := log.New(os.Stdout, "", log.Lshortfile)mux := http.NewServeMux()
mux.Handle("/", logError(greeting, errLogger))handler := logAccess(mux, accessLogger)
handler = httptool.ResponseHandler(handler)
handler = httptool.RecoveryHandler(handler, errLogger)log.Fatalln(http.ListenAndServe(":3000", handler))
}
```## License
HTTPTool is licensed under the Creative Commons Attribution 3.0 License, and code is licensed under a [BSD license](https://github.com/nanoninja/httptool/blob/master/LICENSE).