httpd

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2019 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ALL    = ""
	HEAD   = "HEAD"
	GET    = "GET"
	POST   = "POST"
	PUT    = "PUT"
	DELETE = "DELETE"
)

Variables

View Source
var (
	ErrRouteNotFound    = errors.New("router: Route not found")
	ErrMethodNotAllowed = errors.New("router: Method not allowed")
)

Functions

This section is empty.

Types

type BadRequestError

type BadRequestError struct {
	Text string
}

func NewBadRequestError

func NewBadRequestError(text string) BadRequestError

func (BadRequestError) Error

func (r BadRequestError) Error() string

type Handler

type Handler func(ctx context.Context, req *Req, resp *Resp) error

func NewStaticHandler

func NewStaticHandler(root http.Dir) Handler

NewStaticHandler returns a handler which serves static files from a given directory.

type Middleware

type Middleware func(ctx context.Context, req *Req, resp *Resp, next Handler) error

type Params

type Params map[string]string

func (Params) Int

func (p Params) Int(name string) int

func (Params) Int32

func (p Params) Int32(name string) int32

func (Params) Int64

func (p Params) Int64(name string) int64

type Req

type Req struct {
	*http.Request
	Router *Router
	Params Params
}

func (*Req) DecodeJSON

func (r *Req) DecodeJSON(dst interface{}) error

DecodeJSON decodes a JSON body into a given destination.

func (*Req) FormInt

func (r *Req) FormInt(key string) int

func (*Req) FormInt32

func (r *Req) FormInt32(key string) int32

func (*Req) FormInt64

func (r *Req) FormInt64(key string) int64

func (*Req) Int

func (r *Req) Int(param string) int

func (*Req) Int32

func (r *Req) Int32(param string) int32

func (*Req) Int64

func (r *Req) Int64(param string) int64

func (*Req) Param

func (r *Req) Param(param string) string

func (*Req) SSEStream

func (r *Req) SSEStream(ctx context.Context, resp *Resp) (*SSEStream, error)

func (*Req) WebSocket

func (r *Req) WebSocket(ctx context.Context, resp *Resp) (*WebSocket, error)

type Resp

type Resp struct {
	Router *Router
	http.ResponseWriter

	Status     int
	TotalBytes int64
}

func (*Resp) Attachment

func (r *Resp) Attachment(req *Req, name string, modtime time.Time, content io.ReadSeeker) error

Attachment serves a file as a downloadable attachment.

func (*Resp) Error

func (r *Resp) Error(text string, status int) error

func (*Resp) Error404

func (r *Resp) Error404(text string) error

func (*Resp) Error500

func (r *Resp) Error500(text string) error

func (*Resp) ErrorBadRequest

func (r *Resp) ErrorBadRequest() error

func (*Resp) ErrorInternal

func (r *Resp) ErrorInternal() error

func (*Resp) File

func (r *Resp) File(req *Req, name string, modtime time.Time, content io.ReadSeeker) error

File serves a file usign http.ServeContent.

func (*Resp) JSON

func (r *Resp) JSON(src interface{}) error

JSON serves an OK JSON response, returns an error on JSON encoding errors, when no response is written yet.

func (*Resp) JSONBytes

func (r *Resp) JSONBytes(bytes []byte, status int) error

JSONBytes serves a JSON response.

func (*Resp) JSONStatus

func (r *Resp) JSONStatus(v interface{}, status int) error

JSONStatus returns a JSON response, returns an error on JSON encoding errors, when no response is written yet.

func (*Resp) SetContentLength

func (r *Resp) SetContentLength(length int64)

func (*Resp) SetContentType

func (r *Resp) SetContentType(ctype string)

func (*Resp) SetCookie

func (r *Resp) SetCookie(cookie *http.Cookie)

func (*Resp) Text

func (r *Resp) Text(text string) error

func (*Resp) TextStatus

func (r *Resp) TextStatus(text string, status int) error

func (*Resp) Write

func (r *Resp) Write(b []byte) (int, error)

func (*Resp) WriteHeader

func (r *Resp) WriteHeader(status int)

type Route

type Route struct {
	Name     string
	Param    string
	Middle   []Middleware
	Handlers map[string]Handler // map[method]Handler
	Children map[string]*Route  // map[pattern]*Route
}

Route is a linked tree of routes with handlers and middleware.

func NewRoute

func NewRoute() *Route

NewRoute creates a root route.

func (*Route) ALL

func (r *Route) ALL(pattern string, h Handler)

func (*Route) Add

func (r *Route) Add(pattern string, child *Route)

func (*Route) DELETE

func (r *Route) DELETE(pattern string, h Handler)

func (*Route) GET

func (r *Route) GET(pattern string, h Handler)

func (*Route) HEAD

func (r *Route) HEAD(pattern string, h Handler)

func (*Route) Handler

func (r *Route) Handler(method string, p string, h Handler)

func (*Route) Match

func (r *Route) Match(method string, path string) ([]Middleware, Handler, Params, error)

func (*Route) Middleware

func (r *Route) Middleware(pattern string, m Middleware)

func (*Route) POST

func (r *Route) POST(pattern string, h Handler)

func (*Route) PUT

func (r *Route) PUT(pattern string, h Handler)

func (*Route) Resolve

func (r *Route) Resolve(path string) ([]*Route, Params, error)

func (*Route) Static

func (r *Route) Static(pattern string, dir http.Dir)

type Router

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

func NewRouter

func NewRouter(log logs.Log) *Router

func (*Router) ALL

func (r *Router) ALL(p string, h Handler)

func (*Router) Add

func (r *Router) Add(p string, child *Route)

func (*Router) Close

func (r *Router) Close() <-chan struct{}

func (*Router) Closed

func (r *Router) Closed() <-chan struct{}

func (*Router) DELETE

func (r *Router) DELETE(p string, h Handler)

func (*Router) GET

func (r *Router) GET(p string, h Handler)

func (*Router) HEAD

func (r *Router) HEAD(p string, h Handler)

func (*Router) Handler

func (r *Router) Handler(m string, p string, h Handler)

func (*Router) Middleware

func (r *Router) Middleware(p string, m Middleware)

func (*Router) OnSSEStreamClosed

func (r *Router) OnSSEStreamClosed(s *SSEStream)

func (*Router) OnSSEStreamOpened

func (r *Router) OnSSEStreamOpened(s *SSEStream)

func (*Router) OnWebSocketClosed

func (r *Router) OnWebSocketClosed(ws *WebSocket)

func (*Router) OnWebSocketOpened

func (r *Router) OnWebSocketOpened(ws *WebSocket)

func (*Router) POST

func (r *Router) POST(p string, h Handler)

func (*Router) PUT

func (r *Router) PUT(p string, h Handler)

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(w http.ResponseWriter, httpReq *http.Request)

func (*Router) Static

func (r *Router) Static(p string, root http.Dir)

type SSEStream

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

func NewSSEStream

func NewSSEStream(ctx context.Context, log logs.Log, w http.ResponseWriter, r *http.Request) (*SSEStream, error)

func (*SSEStream) Close

func (s *SSEStream) Close() <-chan struct{}

func (*SSEStream) Closed

func (s *SSEStream) Closed() <-chan struct{}

func (*SSEStream) Outgoing

func (s *SSEStream) Outgoing() chan<- sse.Event

func (*SSEStream) Send

func (s *SSEStream) Send(ctx context.Context, text string)

func (*SSEStream) SendJSON

func (s *SSEStream) SendJSON(ctx context.Context, v interface{}) error

type SSEStreamListener

type SSEStreamListener interface {
	OnSSEStreamOpened(s *SSEStream)
	OnSSEStreamClosed(s *SSEStream)
}

type WebSocket

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

func NewWebSocket

func NewWebSocket(ctx context.Context, log logs.Log, w http.ResponseWriter, r *http.Request) (*WebSocket, error)

func (*WebSocket) Close

func (ws *WebSocket) Close() <-chan struct{}

func (*WebSocket) Closed

func (ws *WebSocket) Closed() <-chan struct{}

func (*WebSocket) Incoming

func (ws *WebSocket) Incoming() <-chan []byte

func (*WebSocket) Outgoing

func (ws *WebSocket) Outgoing() chan<- []byte

func (*WebSocket) Send

func (ws *WebSocket) Send(ctx context.Context, text string)

func (*WebSocket) SendJSON

func (ws *WebSocket) SendJSON(ctx context.Context, v interface{}) error

type WebSocketListener

type WebSocketListener interface {
	OnWebSocketOpened(ws *WebSocket)
	OnWebSocketClosed(ws *WebSocket)
}

Jump to

Keyboard shortcuts

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