apis

package
v0.0.20-beta1 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2023 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

package that provides general functions, types, structs, etc. that can be used when creating an API or HTTP server.

inspiration for adding/based on: https://www.youtube.com/watch?v=pwZuNmAzaH8

Index

Constants

View Source
const EVENT_ERROR int = 1

logging level that indicates the event is an error event.

View Source
const EVENT_INFO int = 0

logging level that indicates the event is a log event.

View Source
const EVENT_SUCCESS int = 2

logging level that indicates the event is a success event.

View Source
const EVENT_WARNING int = 3

logging level that indicates the event is a warning event.

Variables

This section is empty.

Functions

func MakeHTTPHandleFunc added in v0.0.17

func MakeHTTPHandleFunc(fnc APIFunc) http.HandlerFunc

function designed to more elegantly handle HTTP routing functions. this is, essentially, a middleware controller that extends the HandleFunc function. this takes an APIFunc as an argument and processes it.

func ReturnErrorJSON added in v0.0.17

func ReturnErrorJSON(w *http.ResponseWriter, status int, message string) (err error)

function designed to return an error JSON payload to the client. this can be passed to MakeHTTPHandleFunc and used if/when an issue occurs during the execution of an API endpoint.

func WithColoring added in v0.0.19

func WithColoring(mo *MiddlewareOptions) (err error)

function designed to set the Coloring flag of a MiddlewareOptions object. this will set the Coloring variable to true. when the flag is set, the output will change color based on the severity of the event.

func WithLogging added in v0.0.17

func WithLogging(mo *MiddlewareOptions) (err error)

function designed to set the Logging flag of a MiddlewareOptions object. this will set the Logging variable to true.

func WriteJSON added in v0.0.17

func WriteJSON(w *http.ResponseWriter, status int, v any) (err error)

function designed to handle writing a JSON payload to an HTTP response.

if there is an issue encountered while encoding the JSON payload, the error will be returned, otherwise nil will be returned.

Types

type APIFunc

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

alias of http request function that returns an error. this will be handled by middleware and is used for more control/neater code.

type AuthFunc added in v0.0.18

type AuthFunc func(*http.Request) error

alias of a function that is used to authenticate a user. this will be handled by the middleware and is used to allow/restrict a user from accessing the API's endpoints.

type ErrorStruct added in v0.0.17

type ErrorStruct struct {
	// the code associated with the given error.
	ErrorCode int `json:"code"`

	// a message detailing the error that has been thrown.
	ErrorMessage string `json:"message"`
}

struct designed to represent a JSON error return that can be used to deliver information about an error that has been thrown during execution of an API/Server function.

type MiddlewareController added in v0.0.17

type MiddlewareController struct {

	// slice holding IP addresses that are blacklisted. these
	// can be used to restrict who can contact the API.
	AddressBlacklist []string

	// authorization function (if any) to be used by the
	// middleware to restrict access to the endpoints.
	AuthorizationFunction AuthFunc

	// slice holding all the headers reuqired for a request to
	// be properly handled by the API. this is meant to be a list
	// that all endpoints require and not meant to be specific
	// to a single endpoint.
	RequiredHeaders []string
	// contains filtered or unexported fields
}

structure designed to represent a middleware controller that can be used to filter requests to the API. this will include various generic settings that can be controlled by the user.

func NewMiddlwareController added in v0.0.17

func NewMiddlwareController(optsfuncs ...MiddlewareOptsFunc) (mc *MiddlewareController, err error)

function designed to create and return a new MiddlwareController object to the user. this will return a pointer to the new MiddlwareController and an error. if the creation is successful, nil will be returned in place of an error.

func (*MiddlewareController) Blacklisted added in v0.0.17

func (mc *MiddlewareController) Blacklisted(ipaddr string) (err error)

function designed to check if a given IP address string is in the AddressBlacklist slice for the middleware. if the blacklist contains the given IP address nil will be returned, otherwise an error will be returned.

the address comparison is case-insensitive.

func (*MiddlewareController) LogEvent added in v0.0.19

func (mc *MiddlewareController) LogEvent(message string, severity int)

function designed to log a middleware event.

func (*MiddlewareController) MakeHTTPHandleFunc added in v0.0.17

func (mc *MiddlewareController) MakeHTTPHandleFunc(fnc APIFunc) http.HandlerFunc

function designed to more elegantly handle HTTP routing functions. this is, essentially, a middleware controller that extends the HandleFunc function. this takes an APIFunc as an argument and processes it.

type MiddlewareOptions added in v0.0.17

type MiddlewareOptions struct {

	// authorization function (if any) to be used by the
	// middleware to restrict access to the endpoints.
	AuthorizationFunction AuthFunc

	// flag to set logging on/off
	Logging bool

	// flag to set coloring for logging output.
	Coloring bool
}

strucuture designed to hold the various options available for a MiddlewareController.

type MiddlewareOptsFunc added in v0.0.17

type MiddlewareOptsFunc func(*MiddlewareOptions) error

alias of a function that is used to manipulate the various parts of a MiddlewareOptions object.

func WithAuthorization added in v0.0.18

func WithAuthorization(af AuthFunc) MiddlewareOptsFunc

function designed to set the authorization function of a MiddlewareOptions object. this will be assigned to the MiddlewareController that processes the MiddlewareOptions struct.

Jump to

Keyboard shortcuts

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