web

package module
v0.0.0-...-492d98f Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2017 License: Apache-2.0 Imports: 17 Imported by: 0

README

web

Documentation

Overview

Package web 主要用于处理浏览器/服务器之间通信。

Index

Constants

This section is empty.

Variables

View Source
var DefaultRouterGroup = &RouterGroup{
	pattern: "/",
	routers: make([]*Router, 0),
	groups:  make([]*RouterGroup, 0),
}

DefaultRouterGroup 是用于收集默认路由的集合

Functions

This section is empty.

Types

type ActionView

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

func (*ActionView) ContentType

func (v *ActionView) ContentType() string

type Application

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

Application ...

func Default

func Default() *Application

Default returns a default(global) Engine instance.

func New

func New() *Application

New returns a new Application.

func (*Application) AddMiddleware

func (app *Application) AddMiddleware(name string, ware Middleware, global ...bool)

AddMiddleware method add a Middleware to Application.

func (*Application) Attr

func (app *Application) Attr(key string) interface{}

func (*Application) AttrString

func (app *Application) AttrString(key string, defaultValue ...string) string

func (*Application) Attrs

func (app *Application) Attrs() utils.Attribute

func (*Application) Run

func (app *Application) Run(addr ...string) (err error)

Run attaches the router to a http.Server and starts listening and serving HTTP requests. It is a shortcut for http.ListenAndServe(addr, router) Note: this method will block the calling goroutine indefinitely unless an error happens.

func (*Application) ServeHTTP

func (app *Application) ServeHTTP(writer http.ResponseWriter, request *http.Request)

ServeHTTP is implements for http.Handler interface

func (*Application) Use

func (app *Application) Use(registrations ...interface{})

Use 设置或注册全局变量、中间件、服务等

type ContentView

type ContentView struct {
	*ActionView
	Content string
}

func (*ContentView) ContentType

func (v *ContentView) ContentType() string

func (*ContentView) Render

func (ar *ContentView) Render(ctx Context) error

type Context

type Context interface {
	App() *Application

	//Response 返回响应写入器
	Response() *Response

	Request() *Request

	// Attr Gets or sets the HttpApplication object for the current HTTP request.
	Attr(name string, value ...interface{}) interface{}

	// Data Gets or sets the Context object for the current HTTP request.
	Data(name string, value ...interface{}) interface{}
	ViewData() map[string]interface{}

	//PathValue returns a route path data with given name
	PathValue(name string) (value string, ok bool)

	Param(name string) string

	Content(content string, contentType ...string) View
	JSON(data interface{}, contentType ...string) View
	Empty() View
	View(template string, contentType ...string) View

	//APIResult is 推荐的Web API返回结果。
	APIResult(status int, message string, data ...interface{}) View
}

Context Encapsulates all HTTP-specific information about an individual HTTP request.

type GoTemplateViewEngine

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

func GoTemplate

func GoTemplate(ilpath string) *GoTemplateViewEngine

GoTemplate 将使用 GO 原生模板技术作为视图引擎

func (*GoTemplateViewEngine) Render

func (engine *GoTemplateViewEngine) Render(data map[string]interface{}, path string, out io.Writer) error

type HTTPHandler

type HTTPHandler interface {

	// Init 初始化,并使其为处理请求做好准备。
	Init(app *Application) error

	// Handle 定义处理 HTTP 请求的方法。
	Handle(writer http.ResponseWriter, request *http.Request) (complated bool, err error)
}

HTTPHandler 定义 HTTP 请求的处理程序。

type Handler

type Handler interface{}

Handler 定义的路由处理逻辑,它将生成一个 HandlerFunc 对象。

type HandlerFunc

type HandlerFunc func(Context) (int, View)

HandlerFunc 是经过预处理的 Handler 包裹,并作为请求的结果返回或进一步处理。

type I18N

type I18N interface {
	Lang(local, name string, args ...interface{}) (lang string)
}

type JsonView

type JsonView struct {
	*ActionView
	Data interface{}
}

func (*JsonView) Render

func (v *JsonView) Render(ctx Context) error

type Middleware

type Middleware func(Context, HandlerFunc) (int, View)

type OtplViewEngine

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

func Otpl

func Otpl(ilpath string) *OtplViewEngine

Otpl 视图引擎

func (*OtplViewEngine) Render

func (engine *OtplViewEngine) Render(data map[string]interface{}, path string, out io.Writer) error

type Request

type Request struct {
	*http.Request
	// contains filtered or unexported fields
}

Request is a http.Request wrapper.

func (*Request) Cookie

func (r *Request) Cookie(name string) string

func (*Request) Cookies

func (r *Request) Cookies() []*http.Cookie

func (*Request) Query

func (r *Request) Query(name string) string

func (*Request) RawRequest

func (req *Request) RawRequest() *http.Request

TODO:APIs Form FormValue() FormFile()

type RequestContext

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

func (*RequestContext) APIResult

func (ctx *RequestContext) APIResult(status int, message string, data ...interface{}) View

func (*RequestContext) App

func (ctx *RequestContext) App() *Application

func (*RequestContext) Attr

func (ctx *RequestContext) Attr(name string, value ...interface{}) interface{}

func (*RequestContext) Content

func (ctx *RequestContext) Content(content string, contentType ...string) View

func (*RequestContext) Data

func (ctx *RequestContext) Data(name string, value ...interface{}) interface{}

func (*RequestContext) Empty

func (ctx *RequestContext) Empty() View

func (*RequestContext) JSON

func (ctx *RequestContext) JSON(data interface{}, contentType ...string) View

func (*RequestContext) Param

func (ctx *RequestContext) Param(name string) string

func (*RequestContext) PathValue

func (ctx *RequestContext) PathValue(name string) (value string, ok bool)

func (*RequestContext) Request

func (ctx *RequestContext) Request() *Request

func (*RequestContext) Response

func (ctx *RequestContext) Response() *Response

func (*RequestContext) View

func (ctx *RequestContext) View(template string, contentType ...string) View

func (*RequestContext) ViewData

func (ctx *RequestContext) ViewData() map[string]interface{}

type Response

type Response struct {
	*http.Response
	// contains filtered or unexported fields
}

func (*Response) AddCookie

func (r *Response) AddCookie(c *http.Cookie)

AddCookie adds a cookie to the response.

func (*Response) AppendHeader

func (r *Response) AppendHeader(key, value string) *Response

func (*Response) Writer

func (r *Response) Writer() http.ResponseWriter

type RouteData

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

type RouteEntry

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

type RouteHttpHandler

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

func URLRouting

func URLRouting(groups ...*RouterGroup) *RouteHttpHandler

func (*RouteHttpHandler) Handle

func (handler *RouteHttpHandler) Handle(writer http.ResponseWriter, request *http.Request) (complated bool, err error)

func (*RouteHttpHandler) Init

func (handler *RouteHttpHandler) Init(app *Application) error

type RouteTable

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

func (*RouteTable) Match

func (rt *RouteTable) Match(method http.HttpMethod, url *url.URL) (*RouteData, bool)

Match 匹配

func (*RouteTable) Register

func (rt *RouteTable) Register(method http.HttpMethod, pattern *regexp.Regexp, paramNames []string, handler HandlerFunc, segments int, endsWildcard bool, middlewares []Middleware)

type Router

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

func Get

func Get(pattern string, handler Handler, middlewares ...string) *Router

Get registers a new GET request handle and middleware with the given pattern.

type RouterGroup

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

func Group

func Group(pattern string, controller interface{}, middlewares ...string) *RouterGroup

func NewGroup

func NewGroup(pattern string, middlewares ...string) *RouterGroup

func (*RouterGroup) AppendController

func (group *RouterGroup) AppendController(controller interface{}) *RouterGroup

func (*RouterGroup) Get

func (group *RouterGroup) Get(pattern string, handler Handler, middlewares ...string) *Router

func (*RouterGroup) Group

func (group *RouterGroup) Group(pattern string, controller interface{}, middlewares ...string) *RouterGroup

func (*RouterGroup) Route

func (group *RouterGroup) Route(method http.HttpMethod, pattern string, handler Handler, middlewares []string) *Router

type ServiceFunc

type ServiceFunc func(Context) reflect.Value

ServiceFunc 用于获取一个参数(可能改名)

type StaticFileHandler

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

func Static

func Static(root, prefix string, suffix ...string) *StaticFileHandler

func (*StaticFileHandler) Handle

func (handler *StaticFileHandler) Handle(writer http.ResponseWriter, request *http.Request) (complated bool, err error)

func (*StaticFileHandler) Init

func (handler *StaticFileHandler) Init(app *Application) error

type TemplateView

type TemplateView struct {
	*ActionView
	Template string
}

func (*TemplateView) Render

func (v *TemplateView) Render(ctx Context) error

type View

type View interface {
	// ContentType returns HTTP Content-type
	ContentType() string
	// Render the view given the specified model.
	Render(ctx Context) error
}

View for a web interaction. Implementations are responsible for rendering content, and exposing the model.

type ViewEngine

type ViewEngine interface {
	Render(data map[string]interface{}, path string, out io.Writer) error
}

ViewEngine 提供用于将特定模板为渲染为相应输出格式的一组方法

Directories

Path Synopsis
aes

Jump to

Keyboard shortcuts

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