nicehttp

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 11, 2020 License: Apache-2.0 Imports: 14 Imported by: 2

README

Convenience library for HTTP handlers in Go. These help @samthor write servers. YMMV.

See the godoc for documentation.

Convenience handler type

The core Handler type looks like this and implements http.Handler:

type Handler func(ctx context.Context, r *http.Request) interface{}

As you write HTTP handlers, you can just return a variety of types to have your HTTP server do something sensible. If you return an error, the server will serve a 500 and log the error.

See the godoc for more supported types.

Yes, this prevents static type checking for your HTTP handlers. Yes, the convenience is worth it.

App Engine

The dev_appserver script seems to be going away for runtimes of go112 and beyond, which means your program will no longer read its "app.yaml" in development. Static handlers will still work in production ¯‍\‍_‍(‍ツ‍)‍_‍/‍¯

Run nicehttp.Serve("app.yaml", nil) to host your application with:

  • static handler support in development, and
  • automatic serving on the environment variable PORT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidStatusCode is generated when a Handler returns an out-of-range status code.
	ErrInvalidStatusCode = errors.New("status code <100 or >=600 specified")
)

Functions

func CORS

func CORS(handler http.Handler) http.Handler

CORS adds CORS headers to the given http.Handler.

func Handle

func Handle(pattern string, handler func(w http.ResponseWriter, r *http.Request) interface{})

Handle is a convenience method around the Handler type.

func IsProdAppEngine

func IsProdAppEngine() bool

IsProdAppEngine returns whether environment variables match the production App Engine environment.

func Serve

func Serve(yamlPath string, handler http.Handler)

Serve hosts this App Engine application. In dev, hosts on port 8080 and reads the passed YAML file for static handlers. This is needed for runtimes like go112, which don't serve handlers locally. Don't use this for go111 or earlier, which still use dev_appserver.

func Static

func Static(yamlPath string, fallback http.Handler) (http.Handler, error)

Static builds a *ServeMux that enacts the given handler rules, or falls back to the passed http.Handler.

Types

type ContentType

type ContentType struct {
	ContentType string
	Response    interface{}
}

ContentType wraps a Response a with specific Content-Type.

type Handler

type Handler func(w http.ResponseWriter, r *http.Request) interface{}

Handler is the nicehttp helper type, allowing arbitrary return types from a HTTP handler. This supports:

  • error to generate a StatusInternalServerError
  • int for status code
  • string to render directly
  • the Template type to execute a template
  • the Redirect type to cause a StatusSeeOther
  • io.Reader to be piped to the output
  • http.Handler to serve another real handler
  • ... all other responses to be written as JSON, with ETag/If-Not-Modified support for GET

Additionally, wrapper types are supported:

  • the ContentType type to wrap another repsonse with a given Content-Type header
  • the Status type to wrap another response with a HTTP status code

func (Handler) ServeHTTP

func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler.

type Redirect

type Redirect string

Redirect is a http.StatusSeeOther.

type Status

type Status struct {
	Status   int
	Response interface{}
}

Status wraps a Response with a specific status code.

type Template

type Template struct {
	Template *template.Template
	Data     interface{}
}

Template runs a *template.Template.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL