farnasirim / hkit

An http toolkit containing useful utilities like seamless debug logging, painless caching, etc.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

HKIT

hkit is an http toolkit which implements handy, easy to configure facades.

To Install, do as you would with go modules:

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

You can use hkit.Logger with any http.Handler or http.HandlerFunc as easy as the following:

net/http

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

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:

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

About

An http toolkit containing useful utilities like seamless debug logging, painless caching, etc.

License:MIT License


Languages

Language:Go 100.0%