jiaweb

package module
v0.0.0-...-7d0d6c4 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2018 License: Apache-2.0 Imports: 26 Imported by: 0

README

jiaweb

jiaweb

Documentation

Index

Constants

View Source
const (
	LogTarget_Default     = "jiaweb_default"
	LogTarget_HttpRequest = "jiaweb_request"
	LogTarget_HttpServer  = "jiaweb_server"

	LogLevel_Debug = "debug"
	LogLevel_Info  = "info"
	LogLevel_Warn  = "warn"
	LogLevel_Error = "error"
)

Log define

View Source
const (
	CharsetUTF8       = "text/plain; charset=utf-8"
	DefaultServerName = "jiaweb"
)

Http define

View Source
const (
	MIMEApplicationJSON                  = "application/json"
	MIMEApplicationJSONCharsetUTF8       = MIMEApplicationJSON + "; " + CharsetUTF8
	MIMEApplicationJavaScript            = "application/javascript"
	MIMEApplicationJavaScriptCharsetUTF8 = MIMEApplicationJavaScript + "; " + CharsetUTF8
	MIMEApplicationXML                   = "application/xml"
	MIMEApplicationXMLCharsetUTF8        = MIMEApplicationXML + "; " + CharsetUTF8
	MIMEApplicationForm                  = "application/x-www-form-urlencoded"
	MIMEApplicationProtobuf              = "application/protobuf"
	MIMEApplicationMsgpack               = "application/msgpack"
	MIMETextHTML                         = "text/html"
	MIMETextHTMLCharsetUTF8              = MIMETextHTML + "; " + CharsetUTF8
	MIMETextPlain                        = "text/plain"
	MIMETextPlainCharsetUTF8             = MIMETextPlain + "; " + CharsetUTF8
	MIMEMultipartForm                    = "multipart/form-data"
	MIMEOctetStream                      = "application/octet-stream"
)

MIME types

View Source
const (
	HeaderAcceptEncoding                = "Accept-Encoding"
	HeaderAllow                         = "Allow"
	HeaderAuthorization                 = "Authorization"
	HeaderContentDisposition            = "Content-Disposition"
	HeaderContentEncoding               = "Content-Encoding"
	HeaderContentLength                 = "Content-Length"
	HeaderContentType                   = "Content-Type"
	HeaderCookie                        = "Cookie"
	HeaderSetCookie                     = "Set-Cookie"
	HeaderIfModifiedSince               = "If-Modified-Since"
	HeaderLastModified                  = "Last-Modified"
	HeaderLocation                      = "Location"
	HeaderUpgrade                       = "Upgrade"
	HeaderVary                          = "Vary"
	HeaderWWWAuthenticate               = "WWW-Authenticate"
	HeaderXRequestedWith                = "X-Requested-With"
	HeaderXForwardedProto               = "X-Forwarded-Proto"
	HeaderXHTTPMethodOverride           = "X-HTTP-Method-Override"
	HeaderXForwardedFor                 = "X-Forwarded-For"
	HeaderXRealIP                       = "X-Real-IP"
	HeaderServer                        = "Server"
	HeaderOrigin                        = "Origin"
	HeaderAccessControlRequestMethod    = "Access-Control-Request-Method"
	HeaderAccessControlRequestHeaders   = "Access-Control-Request-Headers"
	HeaderAccessControlAllowOrigin      = "Access-Control-Allow-Origin"
	HeaderAccessControlAllowMethods     = "Access-Control-Allow-Methods"
	HeaderAccessControlAllowHeaders     = "Access-Control-Allow-Headers"
	HeaderAccessControlAllowCredentials = "Access-Control-Allow-Credentials"
	HeaderAccessControlExposeHeaders    = "Access-Control-Expose-Headers"
	HeaderAccessControlMaxAge           = "Access-Control-Max-Age"
	HeaderP3P                           = "P3P"
	HeaderCacheControl                  = "Cache-control"

	// Security
	HeaderStrictTransportSecurity = "Strict-Transport-Security"
	HeaderXContentTypeOptions     = "X-Content-Type-Options"
	HeaderXXSSProtection          = "X-XSS-Protection"
	HeaderXFrameOptions           = "X-Frame-Options"
	HeaderContentSecurityPolicy   = "Content-Security-Policy"
	HeaderXCSRFToken              = "X-CSRF-Token"
)

Headers

View Source
const (
	DefaultHTTPPort    = 8082
	RunModeDevelopment = "development"
	RunModeProduction  = "production"
)
View Source
const (
	HTTPMethod_Any       = "ANY"
	HTTPMethod_GET       = "GET"
	HTTPMethod_POST      = "POST"
	HTTPMethod_PUT       = "PUT"
	HTTPMethod_DELETE    = "DELETE"
	HTTPMethod_PATCH     = "PATCH"
	HTTPMethod_HiJack    = "HIJACK"
	HTTPMethod_WebSocket = "WEBSOCKET"
	HTTPMethod_HEAD      = "HEAD"
	HTTPMethod_OPTIONS   = "OPTIONS"
)

Variables

View Source
var (
	SupportHTTPMethod map[string]bool
)

Functions

func LoadConfig

func LoadConfig(configFile, configType string)

func NewRoute

func NewRoute(server *HttpServer) *route

func NewView

func NewView(s *HttpServer) *view

Types

type BaseMiddleware

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

func (*BaseMiddleware) Next

func (bm *BaseMiddleware) Next(ctx Context) error

func (*BaseMiddleware) SetNext

func (bm *BaseMiddleware) SetNext(m Middleware)

type Context

type Context interface {
	HttpServer() *HttpServer
	Response() *Response
	Request() *Request
	Store() *base.Store
	RouteNode() RouteNode
	Handler() HttpHandle
	Hijack() (*HijackConn, error)
	RemoteIP() string
	IsHijack() bool
	End()
	NotFound()
	QueryRouteParam(key string) string
	RenderHtml(viewPath []string, locals map[string]interface{}) error

	WriteJSON(i interface{}) (int, error)
	WriteJSONAndStatus(status int, i interface{}) (int, error)
	WriteJSONBlob(b []byte) (int, error)
	WriteJSONBlobAndStatus(status int, b []byte) (int, error)
	WriteJSONP(callback string, i interface{}) (int, error)
	WriteJSONPBlob(callback string, b []byte) (size int, err error)

	WriteHtml(content ...interface{}) (int, error)
	WriteHtmlAndStatus(status int, content ...interface{}) (int, error)
	WriteString(content ...interface{}) (int, error)
	WriteStringAndStatus(status int, content ...interface{}) (int, error)
	WriteBlob(contentType string, b []byte) (int, error)
	WriteBlobAndStatus(status int, contentType string, b []byte) (int, error)

	GenerateToken(v jwt.MapClaims)
	CleanToken()
	GenerateSeesionToken(v jwt.MapClaims)
	CostTime() string
	VerifyToken(v *map[string]interface{}) bool
	Redirect(url string, code int)
}

type ExceptionHandle

type ExceptionHandle func(Context, error)

自定义异常处理

type Group

type Group interface {
	Use(m ...Middleware) Group
	Group(prefix string, m ...Middleware) Group
	DELETE(path string, h HttpHandle) RouteNode
	GET(path string, h HttpHandle) RouteNode
	GETPOST(path string, h HttpHandle)
	HEAD(path string, h HttpHandle) RouteNode
	OPTIONS(path string, h HttpHandle) RouteNode
	PATCH(path string, h HttpHandle) RouteNode
	POST(path string, h HttpHandle) RouteNode
	PUT(path string, h HttpHandle) RouteNode
	RegisterRoute(method, path string, h HttpHandle) RouteNode
}

func NewGroup

func NewGroup(prefix string, server *HttpServer) Group

type HijackConn

type HijackConn struct {
	ReadWriter *bufio.ReadWriter
	Conn       net.Conn
	// contains filtered or unexported fields
}

func (*HijackConn) Close

func (h *HijackConn) Close() error

func (*HijackConn) SetHedader

func (h *HijackConn) SetHedader(key, value string)

func (*HijackConn) WriteBlob

func (h *HijackConn) WriteBlob(p []byte) (size int, err error)

func (*HijackConn) WriteString

func (h *HijackConn) WriteString(content string) (int, error)

type HttpContext

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

func (*HttpContext) CleanToken

func (ctx *HttpContext) CleanToken()

func (*HttpContext) CostTime

func (ctx *HttpContext) CostTime() string

func (*HttpContext) End

func (ctx *HttpContext) End()

func (*HttpContext) GenerateSeesionToken

func (ctx *HttpContext) GenerateSeesionToken(v jwt.MapClaims)

func (*HttpContext) GenerateToken

func (ctx *HttpContext) GenerateToken(v jwt.MapClaims)

func (*HttpContext) Handler

func (ctx *HttpContext) Handler() HttpHandle

func (*HttpContext) Hijack

func (ctx *HttpContext) Hijack() (*HijackConn, error)

func (*HttpContext) HttpServer

func (ctx *HttpContext) HttpServer() *HttpServer

func (*HttpContext) IsEnd

func (ctx *HttpContext) IsEnd() bool

func (*HttpContext) IsHijack

func (ctx *HttpContext) IsHijack() bool

func (*HttpContext) NotFound

func (ctx *HttpContext) NotFound()

func (*HttpContext) QueryRouteParam

func (ctx *HttpContext) QueryRouteParam(key string) string

func (*HttpContext) Redirect

func (ctx *HttpContext) Redirect(url string, code int)

func (*HttpContext) RemoteIP

func (ctx *HttpContext) RemoteIP() string

func (*HttpContext) RenderHtml

func (ctx *HttpContext) RenderHtml(viewPath []string, locals map[string]interface{}) error

func (*HttpContext) Request

func (ctx *HttpContext) Request() *Request

func (*HttpContext) Response

func (ctx *HttpContext) Response() *Response

func (*HttpContext) RouteNode

func (ctx *HttpContext) RouteNode() RouteNode

func (*HttpContext) Store

func (ctx *HttpContext) Store() *base.Store

func (*HttpContext) VerifyToken

func (ctx *HttpContext) VerifyToken(v *map[string]interface{}) bool

func (*HttpContext) WriteBlob

func (ctx *HttpContext) WriteBlob(contentType string, b []byte) (int, error)

func (*HttpContext) WriteBlobAndStatus

func (ctx *HttpContext) WriteBlobAndStatus(code int, contentType string, b []byte) (int, error)

func (*HttpContext) WriteHtml

func (ctx *HttpContext) WriteHtml(content ...interface{}) (int, error)

func (*HttpContext) WriteHtmlAndStatus

func (ctx *HttpContext) WriteHtmlAndStatus(status int, content ...interface{}) (int, error)

func (*HttpContext) WriteJSON

func (ctx *HttpContext) WriteJSON(i interface{}) (int, error)

func (*HttpContext) WriteJSONAndStatus

func (ctx *HttpContext) WriteJSONAndStatus(status int, i interface{}) (int, error)

func (*HttpContext) WriteJSONBlob

func (ctx *HttpContext) WriteJSONBlob(b []byte) (int, error)

func (*HttpContext) WriteJSONBlobAndStatus

func (ctx *HttpContext) WriteJSONBlobAndStatus(status int, b []byte) (int, error)

func (*HttpContext) WriteJSONP

func (ctx *HttpContext) WriteJSONP(callback string, i interface{}) (int, error)

func (*HttpContext) WriteJSONPBlob

func (ctx *HttpContext) WriteJSONPBlob(callback string, b []byte) (size int, err error)

func (*HttpContext) WriteString

func (ctx *HttpContext) WriteString(content ...interface{}) (int, error)

func (*HttpContext) WriteStringAndStatus

func (ctx *HttpContext) WriteStringAndStatus(status int, content ...interface{}) (int, error)

type HttpHandle

type HttpHandle func(httpCtx Context) error

type HttpModule

type HttpModule struct {
	Name           string
	OnBeginRequest func(Context)
	OnEndRequest   func(Context)
}

type HttpServer

type HttpServer struct {
	JiaWeb  *JiaWeb
	Jwt     *base.MJwt
	Modules []*HttpModule
	Render  Viewer
	// contains filtered or unexported fields
}

func NewHttpServer

func NewHttpServer() *HttpServer

func (*HttpServer) Group

func (s *HttpServer) Group(prefix string) Group

func (*HttpServer) JwtConfig

func (s *HttpServer) JwtConfig() *config.JwtNode

func (*HttpServer) ListenAndServe

func (s *HttpServer) ListenAndServe(addr string) error

func (*HttpServer) RegisterModule

func (s *HttpServer) RegisterModule(module *HttpModule)

func (*HttpServer) Route

func (s *HttpServer) Route() Router

func (*HttpServer) ServeHTTP

func (s *HttpServer) ServeHTTP(rw http.ResponseWriter, req *http.Request)

func (*HttpServer) ServerConfig

func (s *HttpServer) ServerConfig() *config.ServerNode

func (*HttpServer) SetEnableDetailRequestData

func (s *HttpServer) SetEnableDetailRequestData(enable bool)

func (*HttpServer) SetEnableIgnoreFavicon

func (s *HttpServer) SetEnableIgnoreFavicon(enable bool)

func (*HttpServer) SetEnableJwt

func (s *HttpServer) SetEnableJwt()

func (*HttpServer) SetJiaWeb

func (s *HttpServer) SetJiaWeb(jiaweb *JiaWeb)

func (*HttpServer) TemplateConfig

func (s *HttpServer) TemplateConfig() *config.TemplateNode

type JiaWeb

type JiaWeb struct {
	HttpServer              *HttpServer
	Config                  *config.Config
	Logger                  logger.JiaLogger
	Middlewares             []Middleware
	ExceptionHandler        ExceptionHandle
	NotFoundHandler         StandardHandle
	MethodNotAllowedHandler StandardHandle
	// contains filtered or unexported fields
}

func Classic

func Classic(fn func(app *JiaWeb)) *JiaWeb

func New

func New(fn func(app *JiaWeb)) *JiaWeb

func (*JiaWeb) DefaultHTTPErrorHandler

func (app *JiaWeb) DefaultHTTPErrorHandler(ctx Context, err error)

func (*JiaWeb) DefaultMethodNotAllowedHandler

func (app *JiaWeb) DefaultMethodNotAllowedHandler(ctx Context)

func (*JiaWeb) DefaultNotFoundHandler

func (app *JiaWeb) DefaultNotFoundHandler(ctx Context)

func (*JiaWeb) IsDevelopmentMode

func (app *JiaWeb) IsDevelopmentMode() bool

func (*JiaWeb) ListenAndServe

func (app *JiaWeb) ListenAndServe(addr string) error

func (*JiaWeb) RunMode

func (app *JiaWeb) RunMode() string

func (*JiaWeb) SetDevelopmentMode

func (app *JiaWeb) SetDevelopmentMode()

func (*JiaWeb) SetEnableDetailRequestData

func (app *JiaWeb) SetEnableDetailRequestData(enable bool)

func (*JiaWeb) SetEnableLog

func (app *JiaWeb) SetEnableLog(enableLog bool)

func (*JiaWeb) SetExceptionHandle

func (app *JiaWeb) SetExceptionHandle(handler ExceptionHandle)

func (*JiaWeb) SetLogPath

func (app *JiaWeb) SetLogPath(path string)

func (*JiaWeb) SetMethodNotAllowedHandle

func (app *JiaWeb) SetMethodNotAllowedHandle(handler StandardHandle)

func (*JiaWeb) SetNotFoundHandle

func (app *JiaWeb) SetNotFoundHandle(handler StandardHandle)

func (*JiaWeb) SetPProfConfig

func (app *JiaWeb) SetPProfConfig(enablePProf bool, port int)

func (*JiaWeb) SetProductionMode

func (app *JiaWeb) SetProductionMode()

func (*JiaWeb) StartServer

func (app *JiaWeb) StartServer(port int) error

func (*JiaWeb) Use

func (app *JiaWeb) Use(m ...Middleware)

func (*JiaWeb) UseRequestLog

func (app *JiaWeb) UseRequestLog()

type KValue

type KValue struct {
	Key   string
	Value interface{}
}

type LogJson

type LogJson struct {
	RequestUrl string
	HttpHeader string
	HttpBody   string
}

type Middleware

type Middleware interface {
	Handle(ctx Context) error
	SetNext(m Middleware)
	Next(ctx Context) error
}

type MiddlewareFunc

type MiddlewareFunc func() Middleware

type Node

type Node struct {
	Path     string
	NodeType int

	Children []*Node
	Params   Params
	// contains filtered or unexported fields
}

func NewTree

func NewTree() *Node

func (*Node) GetValue

func (n *Node) GetValue(path string) (node *Node, handle RouteHandle, paramsValue map[string]string)

func (*Node) Middleware

func (n *Node) Middleware() []Middleware

func (*Node) Middlewares

func (n *Node) Middlewares() []Middleware

func (*Node) Node

func (n *Node) Node() *Node

func (*Node) Use

func (n *Node) Use(m ...Middleware) *Node

Use 添加中间件

type Param

type Param struct {
	Value string
}

type Params

type Params []Param

type Request

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

func (*Request) Body

func (req *Request) Body() []byte

func (*Request) FormValues

func (req *Request) FormValues() map[string][]string

func (*Request) Get

func (req *Request) Get(key string) string

func (*Request) IsAJAX

func (req *Request) IsAJAX() bool

func (*Request) IsPost

func (req *Request) IsPost() bool

func (*Request) Path

func (req *Request) Path() string

func (*Request) Post

func (req *Request) Post(key string) string

func (*Request) RemoteIP

func (req *Request) RemoteIP() string

func (*Request) Url

func (req *Request) Url() string

type RequestLogMiddleware

type RequestLogMiddleware struct {
	BaseMiddleware
}

func (*RequestLogMiddleware) Handle

func (m *RequestLogMiddleware) Handle(ctx Context) error

type Response

type Response struct {
	Status int
	Size   int64
	// contains filtered or unexported fields
}

func NewResponse

func NewResponse(rw http.ResponseWriter) (r *Response)

func (*Response) Body

func (r *Response) Body() []byte

func (*Response) BodyString

func (r *Response) BodyString() string

func (*Response) Header

func (r *Response) Header() http.Header

func (*Response) Hijack

func (r *Response) Hijack() (net.Conn, *bufio.ReadWriter, error)

func (*Response) HttpStatus

func (r *Response) HttpStatus() int

func (*Response) QueryHeader

func (r *Response) QueryHeader(key string) string

func (*Response) Redirect

func (r *Response) Redirect(code int, targetUrl string) error

func (*Response) ResponseWriter

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

func (*Response) SetContentType

func (r *Response) SetContentType(contentType string)

func (*Response) SetHeader

func (r *Response) SetHeader(key, val string)

func (*Response) SetStatusCode

func (r *Response) SetStatusCode(code int) error

func (*Response) Write

func (r *Response) Write(code int, b []byte) (n int, err error)

func (*Response) WriteHeader

func (r *Response) WriteHeader(code int) error

type RouteHandle

type RouteHandle func(ctx *HttpContext)

type RouteNode

type RouteNode interface {
	Use(m ...Middleware) *Node
	Middlewares() []Middleware
	Node() *Node
}

type Router

type Router interface {
	ServeHTTP(ctx *HttpContext)
	ServerFile(path, fileRoot string) RouteNode
	RegisterRoute(method string, path string, handle HttpHandle) RouteNode
	HEAD(path string, handle HttpHandle) RouteNode
	POST(path string, handle HttpHandle) RouteNode
	GETPOST(path string, handle HttpHandle)
	Any(path string, handle HttpHandle)
	GET(path string, handle HttpHandle) RouteNode
	PUT(path string, handle HttpHandle) RouteNode
	DELETE(path string, handle HttpHandle) RouteNode
	PATCH(path string, handle HttpHandle) RouteNode
	OPTIONS(path string, handle HttpHandle) RouteNode
}

Middleware func(httpCtx *HttpContext)

type StandardHandle

type StandardHandle func(Context)

type Viewer

type Viewer interface {
	AppendTpl(tpl ...string)
	AppendFunc(funcMap template.FuncMap)
	RenderHtml(rw *Response, viewPath []string, locals map[string]interface{}) error
	AddLocals(val ...KValue)
	Tpls() []string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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