httpe

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2020 License: Apache-2.0 Imports: 1 Imported by: 2

Documentation

Overview

Package httpe holds http server utilities

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHandler

func NewHandler(h HandlerE, ew ErrWriter) http.Handler

NewHandler creates a new http.HandlerFunc which calls HandlerE.ServeHTTP and if it returns an error calls ErrWriter.Write to create the appropriate response.

func NewHandlerFunc

func NewHandlerFunc(h HandlerFuncE, ew ErrWriterFunc) http.HandlerFunc

NewHandlerFunc creates a new http.HandlerFunc which calls HandlerFuncE and if it returns an error calls ErrWriterFunc to create the appropriate response.

Types

type ErrWriter

type ErrWriter interface {
	WriteErr(http.ResponseWriter, error)
}

ErrWriter translates an error into the appropriate http response StatusCode and Body and writes it.

type ErrWriterFunc

type ErrWriterFunc func(http.ResponseWriter, error)

The ErrWriterFunc type is an adapter to allow the use of ordinary functions as ErrWriter. If f is a function with the appropriate signature, ErrWriterFunc(f) is a ErrWriter that calls f.

func (ErrWriterFunc) WriteErr

func (ew ErrWriterFunc) WriteErr(w http.ResponseWriter, err error)

WriteErr translates an error into the appropriate http response StatusCode and Body and writes it.

type HandlerE

type HandlerE interface {
	ServeHTTPe(http.ResponseWriter, *http.Request) error
}

HandlerE works like an HTTP.Handler with the addition of an error return value. It is intended to be used with ErrWriter which handles the error and turns it into an appropriate http response StatusCode and Body.

func Chain

func Chain(he ...HandlerE) HandlerE

Chain returns a HandlerE which executes each of the HandlerFuncE parameters sequentially stopping at the first one that returns an error, and returning that error, or nil if none return an error.

type HandlerFuncE

type HandlerFuncE func(http.ResponseWriter, *http.Request) error

The HandlerFuncE type is an adapter to allow the use of ordinary functions as HandlerE. If f is a function with the appropriate signature, HandlerFuncE(f) is a HandlerE that calls f.

Example
package main

import (
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"strings"

	"foxygo.at/s/httpe"
)

type User struct {
	Name string
	Age  int
}

var (
	errInput     = errors.New("input error")
	errDuplicate = errors.New("duplicate")
	storage      = map[string]User{}
)

func handle(w http.ResponseWriter, r *http.Request) error {
	user := User{}
	body, _ := ioutil.ReadAll(r.Body)
	if err := json.Unmarshal(body, &user); err != nil {
		return errInput
	}
	if user.Name == "" || user.Age < 0 {
		return errInput
	}
	if _, ok := storage[user.Name]; ok {
		return errDuplicate
	}
	storage[user.Name] = user
	return nil
}

func writeErr(w http.ResponseWriter, err error) {
	switch {
	case errors.Is(err, errInput):
		http.Error(w, err.Error(), http.StatusBadRequest)
	case errors.Is(err, errDuplicate):
		http.Error(w, "duplicate user", http.StatusForbidden)
	default:
		http.Error(w, "something went wrong", http.StatusInternalServerError)
	}
}

func main() {
	handler := httpe.NewHandlerFunc(handle, writeErr)
	// http.ListenAndServe(":9090", handler)

	w := httptest.NewRecorder()
	r := httptest.NewRequest("POST", "/user", strings.NewReader(`{"Name": "truncated...`))
	handler(w, r)
	fmt.Printf("%d %s", w.Code, w.Body.String())
}
Output:

400 input error

func (HandlerFuncE) ServeHTTPe

func (f HandlerFuncE) ServeHTTPe(w http.ResponseWriter, r *http.Request) error

ServeHTTPe calls f(w, r) and returns its error.

Jump to

Keyboard shortcuts

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