Code Monkey home page Code Monkey logo

hkit's Introduction

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

hkit's People

Contributors

farnasirim avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.