xhttp

package
v0.0.0-...-82e7740 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2019 License: MIT Imports: 34 Imported by: 13

README

web-xhttp GoDoc

web library for Go

Installation

go get gopkg.in/goyy/goyy.v0/web/xhttp

Documentation

Overview

Package xhttp implements web utility functions.

Usage

xhttp.GET("/", func(ctx xhttp.Context) {
	ctx.TEXT(xhttp.StatusOK, "index")
})
xhttp.Run()

Index

Constants

View Source
const (

	// AbortIndex abort index.
	AbortIndex int8 = math.MaxInt8 / 2

	// MIMEJSON application/json.
	MIMEJSON = "application/json"
	// MIMEHTML text/html.
	MIMEHTML = "text/html"
	// MIMEXML application/xml.
	MIMEXML = "application/xml"
	// MIMEXML2 text/xml.
	MIMEXML2 = "text/xml"
	// MIMEPlain text/plain.
	MIMEPlain = "text/plain"
	// MIMEPOSTForm application/x-www-form-urlencoded.
	MIMEPOSTForm = "application/x-www-form-urlencoded"
	// MIMEMultipartPOSTForm multipart/form-data.
	MIMEMultipartPOSTForm = "multipart/form-data"
)
View Source
const (
	StatusContinue           = 100
	StatusSwitchingProtocols = 101

	StatusOK                   = 200
	StatusCreated              = 201
	StatusAccepted             = 202
	StatusNonAuthoritativeInfo = 203
	StatusNoContent            = 204
	StatusResetContent         = 205
	StatusPartialContent       = 206

	StatusMultipleChoices   = 300
	StatusMovedPermanently  = 301
	StatusFound             = 302
	StatusSeeOther          = 303
	StatusNotModified       = 304
	StatusUseProxy          = 305
	StatusTemporaryRedirect = 307

	StatusBadRequest                   = 400
	StatusUnauthorized                 = 401
	StatusPaymentRequired              = 402
	StatusForbidden                    = 403
	StatusNotFound                     = 404
	StatusMethodNotAllowed             = 405
	StatusNotAcceptable                = 406
	StatusProxyAuthRequired            = 407
	StatusRequestTimeout               = 408
	StatusConflict                     = 409
	StatusGone                         = 410
	StatusLengthRequired               = 411
	StatusPreconditionFailed           = 412
	StatusRequestEntityTooLarge        = 413
	StatusRequestURITooLong            = 414
	StatusUnsupportedMediaType         = 415
	StatusRequestedRangeNotSatisfiable = 416
	StatusExpectationFailed            = 417
	StatusTeapot                       = 418

	StatusInternalServerError     = 500
	StatusNotImplemented          = 501
	StatusBadGateway              = 502
	StatusServiceUnavailable      = 503
	StatusGatewayTimeout          = 504
	StatusHTTPVersionNotSupported = 505
)

HTTP status codes, defined in RFC 2616.

Variables

View Source
var Conf = conf.Conf

Conf web.Conf.

Functions

func DELETE

func DELETE(path string, handle Handle, permissions ...*xtype.Permission)

DELETE adds a route for a HTTP DELETE request to the specified matching pattern.

func GET

func GET(path string, handle Handle, permissions ...*xtype.Permission)

GET adds a route for a HTTP GET request to the specified matching pattern.

func HEAD(path string, handle Handle, permissions ...*xtype.Permission)

HEAD adds a route for a HTTP HEAD request to the specified matching pattern.

func NewRequest

func NewRequest(method, urlStr string, values url.Values) (*http.Request, error)

NewRequest returns a new Request given a method, URL, and optional body.

If the provided body is also an io.Closer, the returned Request.Body is set to body and will be closed by the Client methods Do, Post, and PostForm, and Transport.RoundTrip.

NewRequest returns a Request suitable for use with Client.Do or Transport.RoundTrip. To create a request for use with testing a Server Handler use either ReadRequest or manually update the Request fields. See the Request type's documentation for the difference between inbound and outbound request fields.

func OPTIONS

func OPTIONS(path string, handle Handle, permissions ...*xtype.Permission)

OPTIONS adds a route for a HTTP OPTIONS request to the specified matching pattern.

func PATCH

func PATCH(path string, handle Handle, permissions ...*xtype.Permission)

PATCH adds a route for a HTTP PATCH request to the specified matching pattern.

func POST

func POST(path string, handle Handle, permissions ...*xtype.Permission)

POST adds a route for a HTTP POST request to the specified matching pattern.

func PUT

func PUT(path string, handle Handle, permissions ...*xtype.Permission)

PUT adds a route for a HTTP PUT request to the specified matching pattern.

func RegisterPreRun

func RegisterPreRun(preRun func())

RegisterPreRun the registration function is executed before running.

func Run

func Run() error

Run the http server. Listening on Conf.Addr or 9090 by default.

func SetLocale

func SetLocale(locale string)

SetLocale set the i18n locale.

func SetPriority

func SetPriority(value int)

SetPriority set the priority of the logger.

func StatusText

func StatusText(code int) string

StatusText returns a text for the HTTP status code. It returns the empty string if the code is unknown.

Types

type Context

type Context interface {
	Request() *http.Request
	Session() session.Interface
	ResponseWriter() http.ResponseWriter
	Params() url.Values

	Param(key string) string
	Bind(out entity.Interface) error

	Attribute(key string) interface{}
	Attributes() map[string]interface{}
	SetAttribute(key string, value interface{})

	Next()
	IsAborted() bool
	Abort()
	AbortWithStatus(code int)

	HTML(status int, name string, v interface{}) error
	JSON(status int, v interface{}) error
	JSONP(status int, callback string, v interface{}) error
	XML(status int, v interface{}) error
	Text(status int, format string, values ...interface{}) error
	Error(status int) error
	Redirect(location string, status ...int) error
}

Context xhttp.Context.

func NewContext

func NewContext(w http.ResponseWriter, r *http.Request, h ...Handler) Context

NewContext new xhttp.Context from http.ResponseWriter and http.Request.

type Handle

type Handle func(c Context)

Handle is a function that can be registered to a route to handle HTTP requests. Like http.HandleFunc.

type Handler

type Handler func(Context)

Handler xhttp.Handler.

func Recovery

func Recovery() Handler

Recovery returns a middleware that recovers from any panics and writes a 500 if there was one.

type Handlers

type Handlers []Handler

Handlers xhttp.Handlers.

func (Handlers) Last

func (me Handlers) Last() Handler

Last get the lash xhttp.Handler.

type Router

type Router interface {
	Use(middlewares ...Handler) Router

	GET(pattern string, handle Handle, permissions ...*xtype.Permission)
	POST(pattern string, handle Handle, permissions ...*xtype.Permission)
	PUT(pattern string, handle Handle, permissions ...*xtype.Permission)
	DELETE(pattern string, handle Handle, permissions ...*xtype.Permission)
	PATCH(pattern string, handle Handle, permissions ...*xtype.Permission)
	HEAD(pattern string, handle Handle, permissions ...*xtype.Permission)
	OPTIONS(pattern string, handle Handle, permissions ...*xtype.Permission)

	ServeHTTP(w http.ResponseWriter, req *http.Request)
}

Router xhttp.Router.

func Use

func Use(middlewares ...Handler) Router

Use adds a middleware Handler to the stack.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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