web

package
v0.0.0-...-a7413d3 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2020 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// HeaderContentType is a HTTP header name that states the structure of the
	// response body
	HeaderContentType = "Content-Type"
	// HeaderContentTypeValJSON is a value for the content type header for JSON.
	HeaderContentTypeValJSON = "application/json"
)
View Source
const (
	// ErrorAuthentication is the generic error message given when the user
	// failed to authenticate.
	ErrorAuthentication = "error authenticating user"

	// ErrorPathVariableParsing is the generic error message given when some path
	// variable could not be parsed properly.
	ErrorPathVariableParsing = "error parsing path variable"

	// ErrorRequestBodyReading is the generic error message given when HTTP
	// request body could not be read.
	ErrorRequestBodyReading = "error reading request body"

	// ErrorRequestBodyParsing is the generic error message given when HTTP
	// request body could not be parsed.
	ErrorRequestBodyParsing = "error parsing request body"

	// ErrorInternalServer is the generic error message given when an error was
	// encountered in the server.
	ErrorInternalServer = "error within server"
)

Variables

This section is empty.

Functions

func EncodeResponseBody

func EncodeResponseBody(body interface{}, w http.ResponseWriter)

EncodeResponseBody encodes the given value into the response body of the given ResponseWriter.

func EncodeResponseError

func EncodeResponseError(err string, debug error, statusCode int, w http.ResponseWriter)

EncodeResponseError encodes an error response into the response body of the given ResponseWriter.

func EncodeResponseErrorBadRequest

func EncodeResponseErrorBadRequest(err string, debug error, w http.ResponseWriter)

EncodeResponseErrorBadRequest encodes an error response with status code BadRequest.

func EncodeResponseErrorInternalServer

func EncodeResponseErrorInternalServer(err string, debug error, w http.ResponseWriter)

EncodeResponseErrorInternalServer encodes an error response with status code InternalServerError.

func EncodeResponseErrorUnauthorized

func EncodeResponseErrorUnauthorized(err string, debug error, w http.ResponseWriter)

EncodeResponseErrorUnauthorized encodes an error response with status code Unauthorized.

func ParsePathVar

func ParsePathVar(varName string, ps *httprouter.Params) (value string, err error)

ParsePathVar returns the string value of a path variable with the given name.

func ParsePathVarInt

func ParsePathVarInt(varName string, ps *httprouter.Params) (value int, err error)

ParsePathVarInt returns the int value of a path variable with the given name.

func ReadRequestBody

func ReadRequestBody(r *http.Request) ([]byte, error)

ReadRequestBody reads and returns the request body of the given HTTP request.

Types

type AuthenticationError

type AuthenticationError struct {
	Debug string
}

AuthenticationError is raised when the user fails to authenticate.

func (*AuthenticationError) Error

func (err *AuthenticationError) Error() string

type ErrorResponse

type ErrorResponse struct {
	Time  *time.Time `json:"time"`
	Error string     `json:"error"`
	Debug string     `json:"debug"`
}

ErrorResponse represents an error message to be returned to the client if an error is encountered.

func ErrorResponseNew

func ErrorResponseNew(err string, debug error) *ErrorResponse

ErrorResponseNew returns a new instance of errorResponse for the current time.

type HTTPReciever

type HTTPReciever = func(http.ResponseWriter, *http.Request, httprouter.Params)

HTTPReciever is a type alias for functions that handle HTTP requests.

type Handler

type Handler struct {
	Method          string
	Path            []string
	Func            HTTPReciever
	ResponseHeaders map[string]string
}

Handler is a single HTTP request handler

func (*Handler) HandlerFunc

func (h *Handler) HandlerFunc() func(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

HandlerFunc returns a HTTP handler function that implements the handler's logic.

func (*Handler) PathString

func (h *Handler) PathString() string

PathString returns the full string form of the path of the handler.

type HandlerGroup

type HandlerGroup interface {
	Handlers() []Handler
}

HandlerGroup is a group of handlers that have some shared properties.

type Server

type Server struct {
	Router  *httprouter.Router
	Address string
}

Server represents the API controller layer.

func NewServer

func NewServer(address string) Server

NewServer returns a new instance of Controller.

func (*Server) HTTPServer

func (s *Server) HTTPServer() http.Server

HTTPServer returns a new http.Server object for the server.

func (*Server) RegisterHandler

func (s *Server) RegisterHandler(h Handler)

RegisterHandler registers the given handler with the server.

func (*Server) RegisterHandlerGroup

func (s *Server) RegisterHandlerGroup(g HandlerGroup)

RegisterHandlerGroup registers all the handlers in the given handler group with the server

func (*Server) StatusHandler

func (s *Server) StatusHandler() Handler

StatusHandler returns an endpoint handler that returns the status of the server.

type Status

type Status struct {
	Version string     `json:"version"`
	Time    *time.Time `json:"time"`
}

Status contains information about the API at the current time.

func CurrentStatus

func CurrentStatus() *Status

CurrentStatus retrieves information about the API at the current time and returns it as an APIStatus object.

Jump to

Keyboard shortcuts

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