httpmux

package
v0.0.0-...-d5b31fc Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2020 License: MIT Imports: 20 Imported by: 0

README

HTTPMux

a simple HTTP web framework for self-study

Features

  • Context
  • Route Registry
  • Path Parameters
  • Query Parameters
  • Dynamic Pre & Post Midwares
  • Custom Panic Recovery & Log & NotFound Handler
  • Share Datas Between Midwares & Handlers Through Context
  • Response Render
  • Debug Switch

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewResponse

func NewResponse(w http.ResponseWriter) http.ResponseWriter

NewResponse is exported

Types

type Context

type Context struct {
	Req       *http.Request                      // raw http request
	Res       http.ResponseWriter                // raw http response writer
	Query     Params                             // query params + post forms + multipart form values
	Path      Params                             // path params
	FormFiles map[string][]*multipart.FileHeader // form files
	// contains filtered or unexported fields
}

Context for request scope

func (*Context) Abort

func (ctx *Context) Abort()

Abort ...

func (*Context) AutoError

func (ctx *Context) AutoError(data interface{})

AutoError ...

func (*Context) BadRequest

func (ctx *Context) BadRequest(data interface{})

BadRequest ...

func (*Context) Bind

func (ctx *Context) Bind(data interface{}) error

Bind ...

func (*Context) ClientIP

func (ctx *Context) ClientIP() string

ClientIP return current request http client ip

func (*Context) Conflict

func (ctx *Context) Conflict(data interface{})

Conflict ...

func (*Context) Data

func (ctx *Context) Data(code int, data []byte)

Data ...

func (*Context) DumpRequest

func (ctx *Context) DumpRequest() ([]byte, error)

DumpRequest ...

func (*Context) Forbidden

func (ctx *Context) Forbidden(data interface{})

Forbidden ...

func (*Context) GetFormFile

func (ctx *Context) GetFormFile(key string) (*FormFile, error)

GetFormFile is exported

func (*Context) GetKey

func (ctx *Context) GetKey(key string) interface{}

GetKey ...

func (*Context) Gone

func (ctx *Context) Gone(data interface{})

Gone ...

func (*Context) HTML

func (ctx *Context) HTML(code int, data string)

HTML ...

func (*Context) InternalServerError

func (ctx *Context) InternalServerError(data interface{})

InternalServerError ...

func (*Context) JSON

func (ctx *Context) JSON(code int, data interface{})

JSON ...

func (*Context) Locked

func (ctx *Context) Locked(data interface{})

Locked ...

func (*Context) MatchedHandlers

func (ctx *Context) MatchedHandlers() []HandleFunc

MatchedHandlers ...

func (*Context) NotFound

func (ctx *Context) NotFound(data interface{})

NotFound ...

func (*Context) PaymentRequired

func (ctx *Context) PaymentRequired(data interface{})

PaymentRequired ...

func (*Context) Redirect

func (ctx *Context) Redirect(url string, code int)

Redirect ...

func (*Context) SetKey

func (ctx *Context) SetKey(key string, val interface{})

SetKey ...

func (*Context) ShowError

func (ctx *Context) ShowError(code int, data interface{})

ShowError write http error response

func (*Context) StartAt

func (ctx *Context) StartAt() time.Time

StartAt return current context start time

func (*Context) Status

func (ctx *Context) Status(code int)

Status ...

func (*Context) Text

func (ctx *Context) Text(code int, data string)

Text ...

func (*Context) TooManyRequests

func (ctx *Context) TooManyRequests(data interface{})

TooManyRequests ...

func (*Context) Unauthorized

func (ctx *Context) Unauthorized(data interface{})

Unauthorized ...

type FileRoute

type FileRoute struct {
	FullPath string // request full path (with prefix)
	HTTPDir  string // local http dir path
}

FileRoute represents single user defined httpdir files route to serving static files

type FormFile

type FormFile struct {
	Keyname  string              `json:"keyname"`
	Filename string              `json:"filename"`
	Size     int64               `json:"size"`
	Headers  map[string][]string `json:"headers"`
	Bytes    []byte              `json:"bytes"`
}

FormFile is an parsed & loaded MultipartForm.File

type HTTPError

type HTTPError struct {
	Error interface{} `json:"error"`
}

HTTPError is an internal http error body

type HandleFunc

type HandleFunc func(*Context)

HandleFunc is exported ...

type Mux

type Mux struct {
	sync.RWMutex // protect Routes & FileRoutes

	Routes       []*Route     // all http routes
	FileRoutes   []*FileRoute // all httpdir file routes
	PreMidwares  []HandleFunc // pre global midwares
	PostMidwares []HandleFunc // post global midwares triggered afterwards
	NotFound     HandleFunc   // not found handler
	CatchPanic   HandleFunc   // panic handler
	AuditLog     HandleFunc   // audit handler
	// contains filtered or unexported fields
}

Mux is a minimal http router implement

func New

func New(prefix string) *Mux

New create an instance of Mux

func (*Mux) ANY

func (m *Mux) ANY(pattern string, hs ...HandleFunc)

ANY is exported ...

func (*Mux) AddFileRoute

func (m *Mux) AddFileRoute(uri, httpdir string)

AddFileRoute ...

func (*Mux) AddRoute

func (m *Mux) AddRoute(method, pattern string, handlers []HandleFunc)

AddRoute ...

func (*Mux) AllFileRoutes

func (m *Mux) AllFileRoutes() []*FileRoute

AllFileRoutes is exported

func (*Mux) AllRoutes

func (m *Mux) AllRoutes() []*Route

AllRoutes ...

func (*Mux) DELETE

func (m *Mux) DELETE(pattern string, hs ...HandleFunc)

DELETE is exported ...

func (*Mux) DelGlobalPostMidware

func (m *Mux) DelGlobalPostMidware(h HandleFunc)

DelGlobalPostMidware ...

func (*Mux) DelGlobalPreMidware

func (m *Mux) DelGlobalPreMidware(h HandleFunc)

DelGlobalPreMidware ...

func (*Mux) FILE

func (m *Mux) FILE(uri, httpdir string)

FILE is exported ...

func (*Mux) GET

func (m *Mux) GET(pattern string, hs ...HandleFunc)

GET is exported ...

func (*Mux) HEAD

func (m *Mux) HEAD(pattern string, hs ...HandleFunc)

HEAD is exported ...

func (*Mux) OPTIONS

func (m *Mux) OPTIONS(pattern string, hs ...HandleFunc)

OPTIONS is exported ...

func (*Mux) PATCH

func (m *Mux) PATCH(pattern string, hs ...HandleFunc)

PATCH is exported ...

func (*Mux) POST

func (m *Mux) POST(pattern string, hs ...HandleFunc)

POST is exported ...

func (*Mux) PUT

func (m *Mux) PUT(pattern string, hs ...HandleFunc)

PUT is exported ...

func (*Mux) ServeHTTP

func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implement http.Handler

func (*Mux) SetAuditLog

func (m *Mux) SetAuditLog(h HandleFunc)

SetAuditLog ...

func (*Mux) SetCatchPanic

func (m *Mux) SetCatchPanic(h HandleFunc)

SetCatchPanic ...

func (*Mux) SetDebug

func (m *Mux) SetDebug(flag bool)

SetDebug is exported

func (*Mux) SetGlobalPostMidware

func (m *Mux) SetGlobalPostMidware(h HandleFunc)

SetGlobalPostMidware ...

func (*Mux) SetGlobalPreMidware

func (m *Mux) SetGlobalPreMidware(h HandleFunc)

SetGlobalPreMidware ...

func (*Mux) SetNotFound

func (m *Mux) SetNotFound(h HandleFunc)

SetNotFound ...

type Params

type Params map[string]string

Params is a map of name/value pairs for path or query params.

type Response

type Response struct {
	// contains filtered or unexported fields
}

Response re-implement the http.ResponseWriter by rewrap an exists http.ResponseWriter thus to obtain the code / body size replied to the client See: https://golang.org/pkg/net/http/#ResponseWriter

func (*Response) CloseNotify

func (r *Response) CloseNotify() <-chan bool

CloseNotify is exported

func (*Response) ErrMsg

func (r *Response) ErrMsg() string

ErrMsg is exported

func (*Response) Flush

func (r *Response) Flush()

Flush is exported

func (*Response) Header

func (r *Response) Header() http.Header

Header re-implement http.ResponseWriter

func (*Response) Hijack

func (r *Response) Hijack() (net.Conn, *bufio.ReadWriter, error)

Hijack is exported

func (*Response) Size

func (r *Response) Size() int64

Size is exported

func (*Response) StatusCode

func (r *Response) StatusCode() int

StatusCode is exported

func (*Response) Write

func (r *Response) Write(bs []byte) (n int, err error)

Write re-implement http.ResponseWriter

func (*Response) WriteHeader

func (r *Response) WriteHeader(code int)

WriteHeader re-implement http.ResponseWriter

type Route

type Route struct {
	Method  string
	Pattern string
	// contains filtered or unexported fields
}

Route represents single user defined http route

Jump to

Keyboard shortcuts

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