uhttp

package module
v1.0.49 Latest Latest
Warning

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

Go to latest
Published: May 1, 2020 License: MIT Imports: 23 Imported by: 7

README

Build Status GoDoc codecov

uhttp

A simple http-framework. Working but not yet well documented...

Documentation

Index

Constants

View Source
const (
	CtxKeyPostModel string = "uhttp.postModel"
	CtxKeyGetParams string = "uhttp.getParams"
)
View Source
const (
	STRING       string = "string"
	BOOL         string = "bool"
	INT          string = "int"
	INT32        string = "int32"
	INT64        string = "int64"
	FLOAT32      string = "float32"
	FLOAT64      string = "float64"
	SHORT_DATE   string = "shortDate" // 2006-01-02
	RFC3339_DATE string = "rfc3339Date"
)

Variables

Functions

func AddContext

func AddContext(key string, value interface{})

func AddLogOutput added in v1.0.41

func AddLogOutput(w interface{}, key, value string) error

func AddLoggingMiddleware added in v1.0.41

func AddLoggingMiddleware(next http.HandlerFunc) http.HandlerFunc

Logging log time, method and path of an HTTP-Request

func AddMiddleware

func AddMiddleware(mw Middleware) error

func ENUM added in v1.0.41

func ENUM(values ...string) []string

func ExecuteHandler added in v1.0.41

func ExecuteHandler(
	handler Handler,
	method string,
	expectedStatus int,
	requestBody []byte,
	expectedResponseBody []byte,
	t *testing.T,
)

func ExecuteHandlerWithGzipRequestAndResponse added in v1.0.42

func ExecuteHandlerWithGzipRequestAndResponse(
	handler Handler,
	method string,
	expectedStatus int,
	requestBody []byte,
	expectedResponseBody []byte,
	t *testing.T,
)

func ExecuteHandlerWithGzipResponse added in v1.0.42

func ExecuteHandlerWithGzipResponse(
	handler Handler,
	method string,
	expectedStatus int,
	requestBody []byte,
	expectedResponseBody []byte,
	t *testing.T,
)

func GetAsBool added in v1.0.41

func GetAsBool(key string, r *http.Request) *bool

func GetAsBoolFromContext added in v1.0.41

func GetAsBoolFromContext(key string, ctx context.Context) *bool

func GetAsFloat32 added in v1.0.41

func GetAsFloat32(key string, r *http.Request) *float32

func GetAsFloat32FromContext added in v1.0.41

func GetAsFloat32FromContext(key string, ctx context.Context) *float32

func GetAsFloat64 added in v1.0.41

func GetAsFloat64(key string, r *http.Request) *float64

func GetAsFloat64FromContext added in v1.0.41

func GetAsFloat64FromContext(key string, ctx context.Context) *float64

func GetAsInt added in v1.0.41

func GetAsInt(key string, r *http.Request) *int

func GetAsInt32 added in v1.0.41

func GetAsInt32(key string, r *http.Request) *int32

func GetAsInt32FromContext added in v1.0.41

func GetAsInt32FromContext(key string, ctx context.Context) *int32

func GetAsInt64 added in v1.0.41

func GetAsInt64(key string, r *http.Request) *int64

func GetAsInt64FromContext added in v1.0.41

func GetAsInt64FromContext(key string, ctx context.Context) *int64

func GetAsIntFromContext added in v1.0.41

func GetAsIntFromContext(key string, ctx context.Context) *int

func GetAsString added in v1.0.41

func GetAsString(key string, r *http.Request) *string

func GetAsStringFromContext added in v1.0.41

func GetAsStringFromContext(key string, ctx context.Context) *string

func GetAsTime added in v1.0.41

func GetAsTime(key string, r *http.Request) *time.Time

func GetAsTimeFromContext added in v1.0.41

func GetAsTimeFromContext(key string, ctx context.Context) *time.Time

func GetParamsMiddleware added in v1.0.41

func GetParamsMiddleware(optionalGet R, requiredGet R) func(next http.HandlerFunc) http.HandlerFunc

func GzipDecodeRequestBody added in v1.0.42

func GzipDecodeRequestBody(r *http.Request, model interface{}) error

func GzipDecodeResponseBody added in v1.0.42

func GzipDecodeResponseBody(res *http.Response) ([]byte, error)

func GzipEncodeRequestBody added in v1.0.42

func GzipEncodeRequestBody(body []byte) (io.ReadCloser, error)

func Handle

func Handle(pattern string, handler Handler)

Handle configuration

func ParseBool added in v1.0.41

func ParseBool(value string, key string, validatedMap R, errors *[]error)

func ParseDate added in v1.0.41

func ParseDate(value string, key string, format string, validatedMap R, errors *[]error)

func ParseEnum added in v1.0.41

func ParseEnum(value string, enum []string, key string, validatedMap R, errors *[]error)

func ParseFloat added in v1.0.41

func ParseFloat(value string, key string, bits int, validatedMap R, errors *[]error)

func ParseInt added in v1.0.41

func ParseInt(value string, key string, bits int, validatedMap R, errors *[]error)

func ParseModelMiddleware added in v1.0.41

func ParseModelMiddleware(postModel interface{}, getModel interface{}, deleteModel interface{}) func(next http.HandlerFunc) http.HandlerFunc

ParseModel parses and adds a model from a requestbody if wanted

func ParseString added in v1.0.41

func ParseString(value string, key string, validatedMap R, errors *[]error)

func ParsedModel

func ParsedModel(r *http.Request) interface{}

func PreProcessMiddleware added in v1.0.41

func PreProcessMiddleware(preProcess func(ctx context.Context) error) func(next http.HandlerFunc) http.HandlerFunc

func RegisterStaticFilesHandler added in v1.0.49

func RegisterStaticFilesHandler(root string) error

RegisterStaticFilesHandler which serves content from a directory and redirects all requests to non-existant files to index.html index.html must be present! - read all files from root directory - create cache for these files containing original, gzip, br - register handlers for main http-mux

func Render

func Render(w http.ResponseWriter, r *http.Request, model interface{})

func RenderError

func RenderError(w http.ResponseWriter, r *http.Request, err error)

func RenderErrorWithStatusCode

func RenderErrorWithStatusCode(w http.ResponseWriter, r *http.Request, statusCode int, err error)

func RenderMessage

func RenderMessage(w http.ResponseWriter, r *http.Request, msg string)

func RenderMessageWithStatusCode

func RenderMessageWithStatusCode(w http.ResponseWriter, r *http.Request, statusCode int, msg string)

func RenderWithStatusCode

func RenderWithStatusCode(w http.ResponseWriter, r *http.Request, statusCode int, model interface{})

func SelectMethod

func SelectMethod(chain Middleware, handler Handler) http.HandlerFunc

func ServeMux added in v1.0.49

func ServeMux() *http.ServeMux

func SetConfig

func SetConfig(_config Config)

SetConfig set config for all handlers

func SetCorsMiddleware added in v1.0.41

func SetCorsMiddleware(CORSString *string) func(next http.HandlerFunc) http.HandlerFunc

SetCors set response headers

func SetJSONResponseMiddleware added in v1.0.41

func SetJSONResponseMiddleware(next http.HandlerFunc) http.HandlerFunc

SetJSONResponse set response headers

func ValidateParams added in v1.0.41

func ValidateParams(requirement R, actual map[string]string, destination R, required bool) error

Types

type Config

type Config struct {
	CORS                    *string
	CustomLog               ulog.ULogger
	GzipCompressionLevel    *int
	EncodingErrorLogLevel   *ulog.LogLevel
	ParseModelErrorLogLevel *ulog.LogLevel
	Mux                     *http.ServeMux
}

func GetConfig

func GetConfig() Config

type ContextMap

type ContextMap map[string]interface{}

func RequestContext

func RequestContext() ContextMap

type Handler

type Handler struct {
	Pattern        string
	PostHandler    http.HandlerFunc
	PostModel      interface{}
	GetHandler     http.HandlerFunc
	GetModel       interface{}
	DeleteHandler  http.HandlerFunc
	DeleteModel    interface{}
	RequiredGet    R
	OptionalGet    R
	AddMiddlewares []Middleware
	AddMiddleware  *Middleware
	PreProcess     func(ctx context.Context) error
	Timeout        *time.Duration
	TimeoutMessage *string
}

Handler configured

func (Handler) HandlerFunc

func (h Handler) HandlerFunc() http.HandlerFunc

func (Handler) WsReady added in v1.0.32

func (h Handler) WsReady() Middleware

type LoggingResponseWriter added in v1.0.41

type LoggingResponseWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

func (*LoggingResponseWriter) AddLogOutput added in v1.0.41

func (lrw *LoggingResponseWriter) AddLogOutput(key, value string)

func (*LoggingResponseWriter) WriteHeader added in v1.0.41

func (lrw *LoggingResponseWriter) WriteHeader(code int)

type Middleware

type Middleware func(next http.HandlerFunc) http.HandlerFunc

func AdditionalMiddlewares

func AdditionalMiddlewares() []Middleware

func Chain

func Chain(mw ...Middleware) Middleware

Chain chain multiple middlewares copied from: https://hackernoon.com/simple-http-middleware-with-go-79a4ad62889b

func WithContextMiddleware added in v1.0.41

func WithContextMiddleware(key string, value interface{}) Middleware

WithContext attaches any object to the context

type R added in v1.0.41

type R map[string]interface{}

func (R) Printable added in v1.0.41

func (r R) Printable() (map[string]string, error)

Directories

Path Synopsis
cli module
uhttpcli module

Jump to

Keyboard shortcuts

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