SpringWeb

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2020 License: Apache-2.0 Imports: 8 Imported by: 13

Documentation

Index

Constants

View Source
const (
	HeaderContentDisposition = "Content-Disposition"
	HeaderContentType        = "Content-Type"
	HeaderXForwardedProto    = "X-Forwarded-Proto"
	HeaderXForwardedProtocol = "X-Forwarded-Protocol"
	HeaderXForwardedSsl      = "X-Forwarded-Ssl"
	HeaderXUrlScheme         = "X-Url-Scheme"

	CharsetUTF8 = "charset=UTF-8"

	MIMEApplicationJavaScript            = "application/javascript"
	MIMEApplicationJavaScriptCharsetUTF8 = MIMEApplicationJavaScript + "; " + CharsetUTF8
	MIMEApplicationJSON                  = "application/json"
	MIMEApplicationJSONCharsetUTF8       = MIMEApplicationJSON + "; " + CharsetUTF8
	MIMEApplicationXML                   = "application/xml"
	MIMEApplicationXMLCharsetUTF8        = MIMEApplicationXML + "; " + CharsetUTF8
	MIMEMultipartForm                    = "multipart/form-data"
	MIMETextHTML                         = "text/html"
	MIMETextHTMLCharsetUTF8              = MIMETextHTML + "; " + CharsetUTF8
)

Variables

This section is empty.

Functions

func InvokeHandler

func InvokeHandler(ctx WebContext, fn Handler, filters []Filter)

InvokeHandler 执行 Web 处理函数

func NewDefaultWebMapping

func NewDefaultWebMapping() *defaultWebMapping

NewDefaultWebMapping defaultWebMapping 的构造函数

Types

type BaseWebContainer

type BaseWebContainer struct {
	WebMapping
	// contains filtered or unexported fields
}

BaseWebContainer WebContainer 的通用部分

func NewBaseWebContainer

func NewBaseWebContainer() *BaseWebContainer

NewBaseWebContainer BaseWebContainer 的构造函数

func (*BaseWebContainer) EnableSSL

func (c *BaseWebContainer) EnableSSL() bool

EnableSSL 返回是否启用 SSL

func (*BaseWebContainer) GetCertFile

func (c *BaseWebContainer) GetCertFile() string

GetCertFile 返回 CertFile 的路径

func (*BaseWebContainer) GetIP

func (c *BaseWebContainer) GetIP() string

GetIP 返回监听的 IP

func (*BaseWebContainer) GetKeyFile

func (c *BaseWebContainer) GetKeyFile() string

GetKeyFile 返回 KeyFile 的路径

func (*BaseWebContainer) GetPort

func (c *BaseWebContainer) GetPort() int

GetPort 返回监听的 Port

func (*BaseWebContainer) SetCertFile

func (c *BaseWebContainer) SetCertFile(certFile string)

SetCertFile 设置 CertFile 的路径

func (*BaseWebContainer) SetEnableSSL

func (c *BaseWebContainer) SetEnableSSL(enable bool)

SetEnableSSL 设置是否启用 SSL

func (*BaseWebContainer) SetIP

func (c *BaseWebContainer) SetIP(ip string)

SetIP 设置监听的 IP

func (*BaseWebContainer) SetKeyFile

func (c *BaseWebContainer) SetKeyFile(keyFile string)

SetKeyFile 设置 KeyFile 的路径

func (*BaseWebContainer) SetPort

func (c *BaseWebContainer) SetPort(port int)

SetPort 设置监听的 Port

type Filter

type Filter interface {
	// Invoke 函数内部通过 chain.Next() 驱动链条向后执行
	Invoke(ctx WebContext, chain *FilterChain)
}

Filter 过滤器

func HandlerFilter

func HandlerFilter(fn Handler) Filter

HandlerFilter 把 Web 处理函数转换成过滤器

type FilterChain

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

FilterChain 过滤器链条

func NewFilterChain

func NewFilterChain(filters []Filter) *FilterChain

NewFilterChain FilterChain 的构造函数

func (*FilterChain) Next

func (chain *FilterChain) Next(ctx WebContext)

Invoke 函数内部通过 chain.Next() 驱动链条向后执行

type Handler

type Handler func(WebContext)

Handler Web 处理函数

func HTTP

func HTTP(fn http.HandlerFunc) Handler

HTTP Web HTTP 适配函数

func RPC

func RPC(fn RpcHandler) Handler

RPC Web RPC 适配函数

type Mapper

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

Mapper 路由映射器

func NewMapper

func NewMapper(method string, path string, fn Handler, filters []Filter) *Mapper

NewMapper Mapper 的构造函数

func (*Mapper) Filters

func (m *Mapper) Filters() []Filter

Filters 返回 Mapper 的过滤器列表

func (*Mapper) Handler

func (m *Mapper) Handler() Handler

Handler 返回 Mapper 的处理函数

func (*Mapper) Key

func (m *Mapper) Key() string

Key 返回 Mapper 的标识符

func (*Mapper) Method

func (m *Mapper) Method() string

Method 返回 Mapper 的方法

func (*Mapper) Path

func (m *Mapper) Path() string

Path 返回 Mapper 的路径

func (*Mapper) SetFilters

func (m *Mapper) SetFilters(filters []Filter) *Mapper

Filters 设置 Mapper 的过滤器列表

type Router

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

Router 路由分组

func NewRouter

func NewRouter(mapping WebMapping, basePath string, filters []Filter) *Router

NewRouter Router 的构造函数

func (*Router) DELETE

func (r *Router) DELETE(path string, fn Handler) *Mapper

DELETE 注册 DELETE 方法处理函数

func (*Router) GET

func (r *Router) GET(path string, fn Handler) *Mapper

GET 注册 GET 方法处理函数

func (*Router) HEAD

func (r *Router) HEAD(path string, fn Handler) *Mapper

HEAD 注册 HEAD 方法处理函数

func (*Router) OPTIONS

func (r *Router) OPTIONS(path string, fn Handler) *Mapper

OPTIONS 注册 OPTIONS 方法处理函数

func (*Router) PATCH

func (r *Router) PATCH(path string, fn Handler) *Mapper

PATCH 注册 PATCH 方法处理函数

func (*Router) POST

func (r *Router) POST(path string, fn Handler) *Mapper

POST 注册 POST 方法处理函数

func (*Router) PUT

func (r *Router) PUT(path string, fn Handler) *Mapper

PUT 注册 PUT 方法处理函数

func (*Router) Request

func (r *Router) Request(method string, path string, fn Handler) *Mapper

Request 注册任意 HTTP 方法处理函数

type RpcHandler

type RpcHandler func(WebContext) interface{}

RpcHandler Web RPC 处理函数

type WebContainer

type WebContainer interface {
	// WebMapping 路由表
	WebMapping

	// GetIP 返回监听的 IP
	GetIP() string

	// SetIP 设置监听的 IP
	SetIP(ip string)

	// GetPort 返回监听的 Port
	GetPort() int

	// SetPort 设置监听的 Port
	SetPort(port int)

	// EnableSSL 返回是否启用 SSL
	EnableSSL() bool

	// SetEnableSSL 设置是否启用 SSL
	SetEnableSSL(enable bool)

	// GetKeyFile 返回 KeyFile 的路径
	GetKeyFile() string

	// SetKeyFile 设置 KeyFile 的路径
	SetKeyFile(keyFile string)

	// GetCertFile 返回 CertFile 的路径
	GetCertFile() string

	// SetCertFile 设置 CertFile 的路径
	SetCertFile(certFile string)

	// Start 启动 Web 容器,非阻塞
	Start()

	// Stop 停止 Web 容器,阻塞
	Stop(ctx context.Context)
}

WebContainer Web 容器

type WebContext

type WebContext interface {

	// LoggerContext 日志接口上下文
	SpringLogger.LoggerContext

	// NativeContext 返回封装的底层上下文对象
	NativeContext() interface{}

	// Get retrieves data from the context.
	Get(key string) interface{}

	// Set saves data in the context.
	Set(key string, val interface{})

	// Request returns `*http.Request`.
	Request() *http.Request

	// IsTLS returns true if HTTP connection is TLS otherwise false.
	IsTLS() bool

	// IsWebSocket returns true if HTTP connection is WebSocket otherwise false.
	IsWebSocket() bool

	// Scheme returns the HTTP protocol scheme, `http` or `https`.
	Scheme() string

	// ClientIP implements a best effort algorithm to return the real client IP,
	// it parses X-Real-IP and X-Forwarded-For in order to work properly with
	// reverse-proxies such us: nginx or haproxy. Use X-Forwarded-For before
	// X-Real-Ip as nginx uses X-Real-Ip with the proxy's IP.
	ClientIP() string

	// Path returns the registered path for the handler.
	Path() string

	// Handler returns the matched handler by router.
	Handler() Handler

	// ContentType returns the Content-Type header of the request.
	ContentType() string

	// GetHeader returns value from request headers.
	GetHeader(key string) string

	// GetRawData return stream data.
	GetRawData() ([]byte, error)

	// PathParam returns path parameter by name.
	PathParam(name string) string

	// PathParamNames returns path parameter names.
	PathParamNames() []string

	// PathParamValues returns path parameter values.
	PathParamValues() []string

	// QueryParam returns the query param for the provided name.
	QueryParam(name string) string

	// QueryParams returns the query parameters as `url.Values`.
	QueryParams() url.Values

	// QueryString returns the URL query string.
	QueryString() string

	// FormValue returns the form field value for the provided name.
	FormValue(name string) string

	// FormParams returns the form parameters as `url.Values`.
	FormParams() (url.Values, error)

	// FormFile returns the multipart form file for the provided name.
	FormFile(name string) (*multipart.FileHeader, error)

	// SaveUploadedFile uploads the form file to specific dst.
	SaveUploadedFile(file *multipart.FileHeader, dst string) error

	// MultipartForm returns the multipart form.
	MultipartForm() (*multipart.Form, error)

	// Cookie returns the named cookie provided in the request.
	Cookie(name string) (*http.Cookie, error)

	// Cookies returns the HTTP cookies sent with the request.
	Cookies() []*http.Cookie

	// Bind binds the request body into provided type `i`. The default binder
	// does it based on Content-Type header.
	Bind(i interface{}) error

	// ResponseWriter returns `http.ResponseWriter`.
	ResponseWriter() http.ResponseWriter

	// Status sets the HTTP response code.
	Status(code int)

	// Header is a intelligent shortcut for c.Writer.Header().Set(key, value).
	// It writes a header in the response.
	// If value == "", this method removes the header `c.Writer.Header().Del(key)`
	Header(key, value string)

	// SetCookie adds a `Set-Cookie` header in HTTP response.
	SetCookie(cookie *http.Cookie)

	// NoContent sends a response with no body and a status code.
	NoContent(code int)

	// String writes the given string into the response body.
	String(code int, format string, values ...interface{})

	// HTML sends an HTTP response with status code.
	HTML(code int, html string)

	// HTMLBlob sends an HTTP blob response with status code.
	HTMLBlob(code int, b []byte)

	// JSON sends a JSON response with status code.
	JSON(code int, i interface{})

	// JSONPretty sends a pretty-print JSON with status code.
	JSONPretty(code int, i interface{}, indent string)

	// JSONBlob sends a JSON blob response with status code.
	JSONBlob(code int, b []byte)

	// JSONP sends a JSONP response with status code. It uses `callback`
	// to construct the JSONP payload.
	JSONP(code int, callback string, i interface{})

	// JSONPBlob sends a JSONP blob response with status code. It uses
	// `callback` to construct the JSONP payload.
	JSONPBlob(code int, callback string, b []byte)

	// XML sends an XML response with status code.
	XML(code int, i interface{})

	// XMLPretty sends a pretty-print XML with status code.
	XMLPretty(code int, i interface{}, indent string)

	// XMLBlob sends an XML blob response with status code.
	XMLBlob(code int, b []byte)

	// Blob sends a blob response with status code and content type.
	Blob(code int, contentType string, b []byte)

	// Stream sends a streaming response with status code and content type.
	Stream(code int, contentType string, r io.Reader)

	// File sends a response with the content of the file.
	File(file string)

	// Attachment sends a response as attachment, prompting client to save the
	// file.
	Attachment(file string, name string)

	// Inline sends a response as inline, opening the file in the browser.
	Inline(file string, name string)

	// Redirect redirects the request to a provided URL with status code.
	Redirect(code int, url string)

	// SSEvent writes a Server-Sent Event into the body stream.
	SSEvent(name string, message interface{})
}

WebContext 上下文接口,设计理念:为社区中优秀的 Web 服务器提供一个抽象层, 使得底层可以灵活切换,因此在功能上取这些 Web 服务器功能的交集,同时提供获取 底层对象的接口,以便在不能满足用户要求的时候使用底层实现的能力,当然要慎用。

type WebMapping

type WebMapping interface {
	// Mappers 返回映射器列表
	Mappers() map[string]*Mapper

	// Route 返回和 Mapping 绑定的路由分组
	Route(basePath string, filters ...Filter) *Router

	// Request 注册任意 HTTP 方法处理函数
	Request(method string, path string, fn Handler, filters ...Filter) *Mapper

	// GET 注册 GET 方法处理函数
	GET(path string, fn Handler, filters ...Filter) *Mapper

	// POST 注册 POST 方法处理函数
	POST(path string, fn Handler, filters ...Filter) *Mapper

	// PATCH 注册 PATCH 方法处理函数
	PATCH(path string, fn Handler, filters ...Filter) *Mapper

	// PUT 注册 PUT 方法处理函数
	PUT(path string, fn Handler, filters ...Filter) *Mapper

	// DELETE 注册 DELETE 方法处理函数
	DELETE(path string, fn Handler, filters ...Filter) *Mapper

	// HEAD 注册 HEAD 方法处理函数
	HEAD(path string, fn Handler, filters ...Filter) *Mapper

	// OPTIONS 注册 OPTIONS 方法处理函数
	OPTIONS(path string, fn Handler, filters ...Filter) *Mapper
}

WebMapping 路由表

type WebServer

type WebServer struct {
	Containers []WebContainer
}

WebServer 一个 WebServer 包含多个 WebContainer

func NewWebServer

func NewWebServer() *WebServer

NewWebServer WebServer 的构造函数

func (*WebServer) AddWebContainer

func (s *WebServer) AddWebContainer(container WebContainer)

AddWebContainer 添加 WebContainer 实例

func (*WebServer) Start

func (s *WebServer) Start()

Start 启动 Web 容器,非阻塞

func (*WebServer) Stop

func (s *WebServer) Stop(ctx context.Context)

Stop 停止 Web 容器,阻塞

Jump to

Keyboard shortcuts

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