webservice

package
v0.0.0-...-9648343 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2021 License: BSD-3-Clause Imports: 15 Imported by: 0

Documentation

Overview

Package webservice implements an API server that works with HTTP requests in JSON and form-request format.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handler

func Handler(f ContextFunc) http.HandlerFunc

Handler turns an ContextFunc into a HandlerFunc.

Types

type Context

type Context struct {
	Wr   http.ResponseWriter
	Req  *http.Request
	Code int
	// contains filtered or unexported fields
}

Context contains context for JSON and form requests.

func (*Context) Decode

func (c *Context) Decode(obj interface{}) error

func (*Context) EarlyResponse

func (c *Context) EarlyResponse() error

func (*Context) Error

func (c *Context) Error(code int, err string, message interface{}) error

func (*Context) File

func (c *Context) File(name string) (f multipart.File, fh *multipart.FileHeader, err error)

func (*Context) Query

func (c *Context) Query() url.Values

func (*Context) RealIP

func (c *Context) RealIP() string

RealIP returns the actual IP of the remote caller. It assumes the webserver is not exposed to an untrusted network directly (i.e., X-Forwarded-For cannot be spoofed.)

func (*Context) Respond

func (c *Context) Respond(body interface{}) error

func (*Context) ServeFile

func (c *Context) ServeFile(fname, ct string) error

func (*Context) StandardError

func (c *Context) StandardError(err error) error

func (*Context) StartResponse

func (c *Context) StartResponse()

func (*Context) Vars

func (c *Context) Vars() map[string]string

func (*Context) Writer

func (c *Context) Writer(ct string) (WebWriter, error)

type ContextFunc

type ContextFunc func(*Context) error

An ContextFunc handles a single request.

type HTTPError

type HTTPError struct {
	Code    int    `json:"code"`
	Const   string `json:"error"`
	Message string `json:"message,omitempty"`
}

HTTPError is a generic error with an associated HTTP status code.

func (HTTPError) Error

func (he HTTPError) Error() string

type HandlerFunc

type HandlerFunc = http.HandlerFunc

type Server

type Server struct {
	Router *mux.Router
	H1     *http.Server
	H2     *http2.Server
}

Server is a convenience wrapper with a router, HTTP/1 and HTTP/2 server implementation.

func New

func New() *Server

func (*Server) Run

func (s *Server) Run(bind string) error

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown gracefully terminates the server.

type WebWriter

type WebWriter interface {
	Write([]byte) (int, error)
	Flush()
}

Jump to

Keyboard shortcuts

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