impart

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2019 License: MIT Imports: 4 Imported by: 21

README

impart

MIT license Discuss on our forum

impart is a library for the final layer between the API and the consumer. It's used in the latest Write.as and HTMLhouse APIs.

We're still in the early stages of development, so there may be breaking changes.

Example use

package main

import (
	"fmt"
	"github.com/writeas/impart"
	"net/http"
)

type handlerFunc func(w http.ResponseWriter, r *http.Request) error

func main() {
	http.HandleFunc("/", handle(index))
	http.ListenAndServe("127.0.0.1:8080", nil)
}

func index(w http.ResponseWriter, r *http.Request) error {
	fmt.Fprintf(w, "Hello world!")

	return nil
}

func handle(f handlerFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		handleError(w, r, func() error {
			// Do authentication...

			// Handle the request
			err := f(w, r)

			// Log the request and result...

			return err
		}())
	}
}

func handleError(w http.ResponseWriter, r *http.Request, err error) {
	if err == nil {
		return
	}

	if err, ok := err.(impart.HTTPError); ok {
		impart.WriteError(w, err)
		return
	}

	impart.WriteError(w, impart.HTTPError{http.StatusInternalServerError, "Internal server error :("})
}

Documentation

Overview

Package impart provides a simple interface for a JSON-based API. It is designed for passing errors around a web application, sending back a status code and error message if needed, or a status code and some data on success.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RenderActivityJSON

func RenderActivityJSON(w http.ResponseWriter, value interface{}, status int) error

func ReqJSON

func ReqJSON(r *http.Request) bool

ReqJSON returns whether or not the given Request is sending JSON, based on the Content-Type header being application/json.

func WriteError

func WriteError(w http.ResponseWriter, e HTTPError) error

WriteError writes the error to the ResponseWriter as JSON.

func WriteOAuthError added in v1.1.1

func WriteOAuthError(w http.ResponseWriter, e HTTPError) error

WriteOAuthError writes the error to the ResponseWriter as JSON.

func WriteRedirect

func WriteRedirect(w http.ResponseWriter, e HTTPError) int

WriteRedirect sends a redirect

func WriteSuccess

func WriteSuccess(w http.ResponseWriter, data interface{}, status int) error

WriteSuccess writes the successful data and metadata to the ResponseWriter as JSON.

Types

type Envelope

type Envelope struct {
	Code         int         `json:"code"`
	ErrorType    string      `json:"error_type,omitempty"`
	ErrorMessage string      `json:"error_msg,omitempty"`
	Data         interface{} `json:"data,omitempty"`
}

Envelope contains metadata and optional data for a response object. Responses will always contain a status code and either: - response Data on a 2xx response, or - an ErrorMessage on non-2xx responses

ErrorType is not currently used.

type HTTPError

type HTTPError struct {
	Status  int
	Message string
}

HTTPError holds an HTTP status code and an error message.

func (HTTPError) Error

func (h HTTPError) Error() string

Error displays the HTTPError's error message and satisfies the error interface.

type PlainErrEnvelope added in v1.1.1

type PlainErrEnvelope struct {
	Error string `json:"error"`
}

Jump to

Keyboard shortcuts

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