Git Product home page Git Product logo

httpkit's Introduction

HttpKit

A tiny HTTP server toolkit for Go. This module is a collection of packages that provide an opinionated approach to building HTTP servers in Go while still allowing you to use your favorite router mux, and middleware.

Packages

server

The server package provides an encapsulated HTTP server with support for Graceful Shutdown, and Background Tasks. This is a bring your own mux approach, so you can use any router mux you want.

It also includes some useful helpers when working with HTTP requests and responses.

  • Middleware
    • StripTrailingSlash
  • JSON Response helper
  • Decode JSON (Strict and Non-Strict)
  • Signal Shutdown error

errchain

The errchain package provides a simple way to handle errors in a chain of handler functions using a custom http.Handler interface that returns an error.

Errchain implements a ToHandler method that transforms the custom handler into a standard http.Handler allowing you to mix and match custom handlers with standard http.Handlers and ensures that the custom handler is always compatible with the standard http.Handler interface.

errtrace

The errtrace packages is intended to work in conjunction with the errchain package. One of the problems with the errchain package is that it can be difficult to trace the error back to the original handler function or core service level function. The errtrace package provides a way to define Traceable errors that provide contextual information like:

  • File path (relative or custom)
  • Caller function name
  • Caller function line number
  • Additional message context
  • Full wrapped error chain
  • Human readable error trace (think stacktrace)

Error Trace Examples

error: defines a generic error type and not a traceable error.

trace error: defines a traceable error type which provides additional context.

Note: real terminal output is colorized for readability

error:
  failed to do something: error creating user in database
trace error:
  error creating user in database
  cmd/cli/main.go:22  main.ServiceNewUser() -> user repo: error writing to database
trace error:
  error writing to database
  cmd/cli/main.go:12  main.CreateUser() -> wrap: cmd/cli/main.go:10 main.CreateUser: user with id 1 already exists
error:
  cmd/cli/main.go:10 main.CreateUser: user with id 1 already exists

httpkit's People

Contributors

hay-kot avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

httpkit's Issues

Support for listening on Unix domain sockets

Hi, this library currently only allows listening on TCP sockets via host and port options.

I am interested in adding support to Homebox to listen on a local Unix domain socket. For example, listening on /var/run/homebox/homebox.sock which can be accessed as curl --unix-socket /var/run/homebox/homebox.sock http://localhost.

Please let me know if this is something that httpkit (and Homebox) is willing to support and maintain; if so, I will create and file a PR for it.

Approximately, I want to add the capability to start the HTTP server as:

// if a "socket" option is supplied instead of "host" and "port"

ln, err := net.Listen("unix", ...)
if err != nil { ... }

srv.Serve(ln)

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.