server

package
v0.0.0-...-7092ca2 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2014 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EmptyBody

func EmptyBody() []byte

returns an empty byte array. intended for use as a parameter to NewFailHttpImmediately or HttpImmediately.succMsg

Types

type FailHttpImmediately

type FailHttpImmediately struct {
	// contains filtered or unexported fields
}

a server that executes all of its steps and writes the output, failure or success to the provided http.ResponseWriter. if all steps succeeded, writes SuccCode and SuccMsg to the ResponseWriter. otherwise, stops execution immediately after the failed HttpStep and writes details on the failed HttpStep to the same ResponseWriter. example usage:

steps := []*server.HttpStep {
  &server.HttpStep {
    Runner: func() error {
      //do nothing
      return nil
    },
    FailCode: http.StatusInternalServerError,
    FailMsg: server.JsonErr()
  },
}
NewFailImmediately(resp, steps, http.StatusOK, []byte("OK!")).Execute()

func NewFailHttpImmediately

func NewFailHttpImmediately(resp http.ResponseWriter, steps []*HttpStep, succCode int, succMsg []byte) *FailHttpImmediately

create a new FailHttpImmediately server

func (*FailHttpImmediately) Execute

func (fh *FailHttpImmediately) Execute() []error

execute the series of steps in the FailHttpImmediately

type FailMsg

type FailMsg func(error) []byte

the function that converts errors into HTTP response bodies

func JsonErr

func JsonErr() FailMsg

a FailMsg to convert a standard error into a JSON format.

type HttpStep

type HttpStep struct {
	Runner   Runner
	FailCode int
	FailMsg  func(error) []byte
}

a single step to run in a series to implement a server endpoint

type Runner

type Runner func() error

the function to actually run each HttpStep

func EncodeJson

func EncodeJson(val interface{}, target *[]byte) Runner

a Runner that: - encodes val to json - writes the resulting bytes to *target - returns nil if there were errors at any step, does not write to *target and returns the error

func QueryString

func QueryString(req *http.Request, key string, idx int, target *string) Runner

a Runner to extract a value from a query string key. intended for use as a HttpStep.runner. the returned Runner will: - gets the query string from the given http.Request - gets the value(s) for the given key from that query string - of the values for that key, attempts to get the value at index idx - writes that value to *target - returns nil if any of the above steps fails, the function returns an appropriate error and writes nothing to *target. idx is 0 based

func QueryStringParseBool

func QueryStringParseBool(req *http.Request, key string, idx int, target *bool) Runner

a Runner that: - executes QueryString - attempts to convert the output of QueryString to a bool - writes that value to *target - returns nil if anything went wrong along the way, return an appropriate error and write nothing to *target

func ReadBody

func ReadBody(req *http.Request, target *[]byte) Runner

a Runner that: - reads the entire body from req - writes the body to *target if there were errors at any step, returns the error and does not write to *target

func ReadJson

func ReadJson(req *http.Request, target interface{}) Runner

a Runner that: - reads the entire body from req - attempts to decode json from that body to target if there were errors at any step, returns the error. make sure to pass an address to the data structure to read, similar to what you'd do with the encoding/json package for example:

var targetMap map[string]string
server.ReadJson(req, &targetMap)

when the ReadJson runner is executed, the json will be written into targetMap if there were no errors

Jump to

Keyboard shortcuts

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