https://github.com/farnasirim/hkit
An http toolkit containing useful utilities like seamless debug logging, painless caching, etc.
https://github.com/farnasirim/hkit
cache go golang http http-cache logrus toolkit toolkits utilities utility utility-library
Last synced: 18 days ago
JSON representation
An http toolkit containing useful utilities like seamless debug logging, painless caching, etc.
- Host: GitHub
- URL: https://github.com/farnasirim/hkit
- Owner: farnasirim
- License: mit
- Created: 2017-06-26T14:50:57.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-11-09T04:25:43.000Z (about 7 years ago)
- Last Synced: 2024-06-20T16:43:34.482Z (over 1 year ago)
- Topics: cache, go, golang, http, http-cache, logrus, toolkit, toolkits, utilities, utility, utility-library
- Language: Go
- Homepage:
- Size: 20.5 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# HKIT
`hkit` is an http toolkit which implements handy, easy to configure facades.
To Install, do as you would with go modules:
```bash
vgo get github.com/farnasirim/hkit
```
You can use old fashioned `go get` too if you want.
## Docs
hkit currently implements utilities for the following use cases:
- [Logging](#logging)
You can use hkit.Logger with any `http.Handler` or `http.HandlerFunc` as easy as the following:
#### net/http
```go
package main
import (
"net/http"
"github.com/farnasirim/hkit"
)
func main() {
handlerFunc := func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"x": "2", "y": {"a": "b"}}`))
}
wrappedHandler := hkit.NewLogger(handlerFunc)
http.ListenAndServe(":8000", wrappedHandler)
}
```
And then
```bash
curl localhost:8000 -d '{"some": "json"}'
```
will result in
```
Method: POST
remote address: [::1]:39252
Content-Type: application/x-www-form-urlencoded
User-Agent: curl/7.61.1
Accept: */*
Content-Length: 16
{"some": "json"}
{"x": "2", "y": {"a": "b"}}
```
#### logrus
This is the easiest way you can use hkit with logrus:
```go
package main
import (
"net/http"
log "github.com/Sirupsen/logrus"
"github.com/colonelmo/hkit"
)
func main() {
handlerFunc := func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"x": "2", "y": {"a": "b"}}`))
}
prettyLogWriter := log.StandardLogger().Writer()
wrappedHandler := hkit.NewLogger(handlerFunc).SetWriter(prettyLogWriter)
http.ListenAndServe(":8000", wrappedHandler)
}
```
And the previous curl will result in:
INFO[0021] Method: POST
INFO[0021] remote address: [::1]:39358
INFO[0021]
INFO[0021] User-Agent: curl/7.61.1
INFO[0021] Accept: */*
INFO[0021] Content-Length: 16
INFO[0021] Content-Type: application/x-www-form-urlencoded
INFO[0021]
INFO[0021] {"some": "json"}
INFO[0021]
being written to the stdout.
# License
MIT