forge

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2022 License: MIT Imports: 14 Imported by: 0

README

forge

A Simple HTTP Toolkit for Go Applications.

License GitHub Actions Go Report Card Documentation

Documentation

Index

Constants

View Source
const (
	LogLevelError   = "error"
	LogLevelWarning = "warning"
	LogLevelInfo    = "info"
	LogLevelDebug   = "debug"
)

Log Levels

View Source
const (
	HeaderContentType  = "Content-Type"
	HeaderCacheControl = "Cache-Control"
)

Header Constants

View Source
const (
	ResponseTextNotFound = "Not Found"
)

Response Constants

Variables

View Source
var (
	// ErrInvalidValue returned when the value passed to Unmarshal is nil or not a
	// pointer to a struct.
	ErrInvalidValue = errors.New("value must be a non-nil pointer to a struct")

	// ErrUnsupportedType returned when a field with tag "env" is unsupported.
	ErrUnsupportedType = errors.New("field is an unsupported type")

	// ErrUnexportedField returned when a field with tag "env" is not exported.
	ErrUnexportedField = errors.New("field must be exported")
)

Functions

func Env added in v0.1.3

func Env(target interface{}, dotEnvFilePaths ...string) error

Env sources .env files and parses variables into a existing struct

func EnvReadFiles added in v0.1.3

func EnvReadFiles(dotEnvFilePaths ...string)

EnvReadFiles locates and parses .env files

func EnvUnmarshal added in v0.1.3

func EnvUnmarshal(target interface{}) error

EnvUnmarshal into a target struct

func LoggerAddRequestID added in v0.1.3

func LoggerAddRequestID(r *http.Request) *http.Request

LoggerAddRequestID adds the request ID to the request

func LoggerGetRequestID added in v0.1.3

func LoggerGetRequestID(r *http.Request) string

LoggerGetRequestID gets the request ID off of the request if there is one

func RespondHTML added in v0.1.3

func RespondHTML(w http.ResponseWriter, statusCode int, body []byte)

RespondHTML responds to an http.Request with a text body

func RespondJSON added in v0.1.3

func RespondJSON(w http.ResponseWriter, statusCode int, v interface{})

RespondJSON responds to an http.Request with a JSON body

func RespondText added in v0.1.3

func RespondText(w http.ResponseWriter, statusCode int, body []byte)

RespondText responds to an http.Request with a text body

func Run added in v0.1.3

func Run(app App)

Run an App

Types

type App added in v0.1.3

type App interface {
	Handler() http.Handler
	Logger() Logger
	ListenAddress() string
}

App is the requirements for an application

type AuthorizeBearer added in v0.1.3

type AuthorizeBearer struct {
	Token string
}

AuthorizeBearer authorizes a http request using a bearer token

func (AuthorizeBearer) Authorize added in v0.1.3

func (authorizeBearer AuthorizeBearer) Authorize(request *http.Request) error

Authorize a http request using a bearer token

type HTTPLogger added in v0.1.3

type HTTPLogger struct {
	Handler http.Handler
	Log     Logger
}

HTTPLogger logs all request before passing off to the Handler

func (*HTTPLogger) ServeHTTP added in v0.1.3

func (logger *HTTPLogger) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServerHTTP satisfies the http.Handler interface

type HTTPRouter added in v0.1.3

type HTTPRouter struct {
	NotFoundHandler http.Handler
	Routes          map[string]http.Handler
}

HTTPRouter serves http.Requests for a predefined map of Paths

func (*HTTPRouter) ServeHTTP added in v0.1.3

func (router *HTTPRouter) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServerHTTP satisfies the http.Handler interface

type HTTPStatic added in v0.1.3

type HTTPStatic struct {
	FileSystem      http.FileSystem
	NotFoundHandler http.Handler
	// contains filtered or unexported fields
}

HTTPStatic servers static files without directory listings

func (*HTTPStatic) ServeHTTP added in v0.1.3

func (static *HTTPStatic) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServerHTTP satisfies the http.Handler interface

type Logger

type Logger interface {
	// Error logs an error message
	Error(r *http.Request, message string, context map[string]interface{}) error
	// Info logs an info message
	Info(r *http.Request, message string, context map[string]interface{}) error
	// Warning logs a warning message
	Warning(r *http.Request, message string, context map[string]interface{}) error
	// Debug logs a debug message
	Debug(r *http.Request, message string, context map[string]interface{}) error
}

Logger Service

type LoggerJSON added in v0.1.3

type LoggerJSON struct {
	Writer io.Writer
	// contains filtered or unexported fields
}

LoggerJSON is a JSON logger

func (*LoggerJSON) Debug added in v0.1.3

func (logger *LoggerJSON) Debug(r *http.Request, message string, context map[string]interface{}) error

Debug logs a debug message

func (*LoggerJSON) Error added in v0.1.3

func (logger *LoggerJSON) Error(r *http.Request, message string, context map[string]interface{}) error

Error logs an error message

func (*LoggerJSON) Info added in v0.1.3

func (logger *LoggerJSON) Info(r *http.Request, message string, context map[string]interface{}) error

Info logs an info message

func (*LoggerJSON) Warning added in v0.1.3

func (logger *LoggerJSON) Warning(r *http.Request, message string, context map[string]interface{}) error

Warning logs a warning message

type Response

type Response struct {
	Status  bool        `json:"status"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

Response is a basic response structure

type RestAuthorizer added in v0.1.3

type RestAuthorizer interface {
	Authorize(request *http.Request) error
}

RestAuthorizer defines how to authorize http requests

type RestClient added in v0.1.3

type RestClient struct {
	Authorizer RestAuthorizer
}

RestClient sets up a way to make http requests

func (*RestClient) Request added in v0.1.3

func (restClient *RestClient) Request(
	method string,
	url string,
	payload interface{},
	target interface{},
) error

Request makes a request using the given arguments

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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