httpService

package
v0.0.0-...-0c93acb Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2020 License: MIT Imports: 8 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AccessDenied

func AccessDenied(writer http.ResponseWriter)

AccessDenied is a convenience method. This will set the HTTP status code to 403 and send back a BasicJSONResponse with a message of Access Denied

func BadRequest

func BadRequest(writer http.ResponseWriter, response interface{})

BadRequest is a convenience method. This will set the HTTP status code to 400. This is useful for telling a caller that the request is malformed.

func Error

func Error(writer http.ResponseWriter, response interface{})

Error is a convenience method. This will set the HTTP status code to 500. This is useful for indicating some type of error ocurred during processing.

func Forbidden

func Forbidden(writer http.ResponseWriter, response interface{})

Forbidden is a convenience method. This will set the HTTP status code to 403. This is useful in telling a caller that some type of authentication failed.

func JSONBadRequest

func JSONBadRequest(writer http.ResponseWriter, message string)

JSONBadRequest is a shortcut to send a bad request with a basic JSON object

func JSONError

func JSONError(writer http.ResponseWriter, message string)

JSONError is a shortcut to send an error with a basic JSON object

func JSONForbidden

func JSONForbidden(writer http.ResponseWriter, message string)

JSONForbidden is a shortcut to send a forbidden response with a basic JSON object

func JSONNotFound

func JSONNotFound(writer http.ResponseWriter, message string)

JSONNotFound is a shortcut to send a not found message with a basic JSON object

func JSONSuccess

func JSONSuccess(writer http.ResponseWriter, message string)

JSONSuccess is a shortcut to send a 200 with a basic JSON object

func LoadHTML

func LoadHTML(pageName string) ([]byte, error)

LoadHTML loads a file with the specified name and returns a byte array of its contents. This will automatically append the HTML extension to the end of the page name

func NotFound

func NotFound(writer http.ResponseWriter, response interface{})

NotFound is a convenience method. This will set the HTTP status code to 404. This is useful in telling a caller that the resources they are trying to get is not found.

func ParseJSONBody

func ParseJSONBody(request *http.Request, receiver interface{}) error

ParseJSONBody is useful when a POST or PUT request has a Content-Type of application/json and the body has JSON data. Pass the address of a receiver variable and this function will fill in the values from the request JSON into any matching struct fields, or will hydrate them into a map if possible.

func RenderHTML

func RenderHTML(writer http.ResponseWriter, pageName string) error

RenderHTML takes the name of a page, loads it, and writes it to the provided ResponseWriter.

func RenderTemplate

func RenderTemplate(writer http.ResponseWriter, pageName string, data interface{}) error

RenderTemplate takes a page name, loads it, executes it as a template against the provided data, then writes it to the provided ResponseWriter

func RequestIsJSONContentType

func RequestIsJSONContentType(request *http.Request) bool

REquestIsJSONContentType returns true/false if the request contains a Content-Type header, and if that header is set to "application/json".

func SessionExpired

func SessionExpired(writer http.ResponseWriter)

SessionExpired is a convenience method for telling clients that a session has expired. This sends a SessionExpiredResponse structure and a status of 401 Unauthorized.

func SplitListOfIntsInURL

func SplitListOfIntsInURL(request *http.Request, key string, delimiter string) []int

SplitListOfIntsInURL takes a delimited list value in a URL key and splits it into a slice of integers

func Success

func Success(writer http.ResponseWriter, response interface{})

Success is a convenience method. This will set the HTTP status code to 200.

func WriteHTML

func WriteHTML(writer http.ResponseWriter, html string, code int)

WriteHTML writes HTML out to a ResponseWriter

func WriteJSON

func WriteJSON(writer http.ResponseWriter, object interface{}, code int)

WriteJSON serializes an object to JSON and writes it to the specified writer stream with an HTTP code. The object is of type interface{} so can technically be anything. A struct or map would be the usual type of item to serialize.

func WriteText

func WriteText(writer http.ResponseWriter, text string, code int)

WriteText writes text to a ResponseWriter with a content type of text/plain

Types

type BasicJSONResponse

type BasicJSONResponse struct {
	IsSuccess bool   `json:"success"`
	Message   string `json:"message"`
}

A BasicJSONResponse allows you to return a message and a success indicator

func ErrorJSONResponse

func ErrorJSONResponse(message string) BasicJSONResponse

func NewBasicJSONResponse

func NewBasicJSONResponse(success bool, message string) BasicJSONResponse

NewBasicJSONResponse creates a new structure.

func (BasicJSONResponse) BadRequest

func (response BasicJSONResponse) BadRequest(writer http.ResponseWriter)

BadRequest sends this basic JSON response as a 400

func (BasicJSONResponse) Error

func (response BasicJSONResponse) Error(writer http.ResponseWriter)

Error sends this basic JSON response as a 500

func (BasicJSONResponse) Forbidden

func (response BasicJSONResponse) Forbidden(writer http.ResponseWriter)

Forbidden sends this basic JSON response as a 403

func (BasicJSONResponse) NotFound

func (response BasicJSONResponse) NotFound(writer http.ResponseWriter)

NotFound sends this basic JSON response as a 404

func (BasicJSONResponse) Success

func (response BasicJSONResponse) Success(writer http.ResponseWriter)

Success sends this basic JSON response as a 200

type Layout

type Layout struct {
	BaseDirectory     string
	TemplateFileNames []string
	TemplateBodies    map[string]string
}

Layout represents a directory of template files and pre-loaded bodies

func NewLayout

func NewLayout(baseDirectory string, templateFileNames []string) (Layout, error)

func (Layout) RenderView

func (this Layout) RenderView(writer http.ResponseWriter, pageName string, data interface{}) error

type SessionExpiredResponse

type SessionExpiredResponse struct {
	Success        bool `json:"success"`
	SessionExpired bool `json:"sessionExpired"`
}

func NewSessionExpiredResponse

func NewSessionExpiredResponse() SessionExpiredResponse

Creates a new SessionExpiredResponse structure.

Jump to

Keyboard shortcuts

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