Skip to content

ernestoalejo/context-handler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

context-handler

GoDoc

Go HTTP handlers using context.

Installation

go get github.com/ernestoalejo/context-handler

Features

  • Pass context to all handlers.
  • Buffer responses to return errors to users correctly.
  • Read and write JSON in POST requests easily.
  • Return errors easily from the handler.
  • Show stack traces of errors in stderr (compatible with App Engine).
  • Configurable middlewares.
  • Auto load App Engine context from the request.
  • Graceful HTTP listener.
  • Use gin in development to autoreload the server.

Usage

package main

import (
  "fmt"
  "net/http"
  "log"
  "time"

  "github.com/ernestoalejo/context-handler"
  "github.com/juju/errors"
  "golang.org/x/net/context"
)

func main() {
  http.HandleFunc("/", handler.Ctx(homeHandler))
  http.HandleFunc("/health", handler.Ctx(healthHandler))

  handler.ServeGracefully(9000, 30*time.Second)
}

func homeHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
  fmt.Fprintln(w, "Home handler")

  return errors.New("cannot handle request correctly")
}

func healthHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
  fmt.Fprintln(w, "ok")
  
  return nil
}

Releases

No releases published

Packages

No packages published

Languages