web

package
v1.5.8 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2020 License: MIT Imports: 34 Imported by: 0

Documentation

Index

Constants

View Source
const MAXN_RETRY_TIMES = 60

Variables

View Source
var (
	// run mode, "debug" or "release"
	RunMode         string
	CopyRequestBody bool
	HttpHead             = "Backend WebServer"
	IndentJson      bool = true
)
View Source
var Config = NewServerConfig()

Functions

func CheckServiceEnable

func CheckServiceEnable(ctx *Context) bool

检查指定服务是否可用

func Close

func Close()

Close stops the main server.

func Delete

func Delete(route string, handler interface{})

Delete adds a handler for the 'DELETE' http method in the main server.

func Get

func Get(route string, handler interface{})

Get adds a handler for the 'GET' http method in the main server.

func InitMime

func InitMime() error

func IsTrustedIP

func IsTrustedIP(ctx *Context) bool

是否是受信任的地址

func Match

func Match(route string, handler interface{}, method string)

Match adds a handler for an arbitrary http method in the main server.

func NewCookie

func NewCookie(name string, value string, age int64) *http.Cookie

NewCookie is a helper method that returns a new http.Cookie object. Duration is specified in seconds. If the duration is zero, the cookie is permanent. This can be used in conjunction with ctx.SetCookie.

func Post

func Post(route string, handler interface{})

Post adds a handler for the 'POST' http method in the main server.

func Process

func Process(c http.ResponseWriter, req *http.Request)

Process invokes the main server's routing system.

func Put

func Put(route string, handler interface{})

Put adds a handler for the 'PUT' http method in the main server.

func Run

func Run(addr string) (err error)

Run starts the web application and serves HTTP requests for the main server.

func RunFCGI

func RunFCGI(addr string) (err error)

RunFCGI starts the web application and serves FastCGI requests for the main server.

func RunSCGI

func RunSCGI(addr string) (err error)

RunSCGI starts the web application and serves SCGI requests for the main server.

func RunTLS

func RunTLS(addr string, config *tls.Config) (err error)

RunTLS starts the web application and serves HTTPS requests for the main server.

func SetCookieSecret

func SetCookieSecret(cookieSecret string) func(*ServerConfig)

func SetKeepAlive

func SetKeepAlive(keepAlive bool) func(*ServerConfig)

func SetMaxHeaderBytes

func SetMaxHeaderBytes(maxHeaderBytes int) func(*ServerConfig)

func SetMaxMemory

func SetMaxMemory(maxMemory int64) func(*ServerConfig)

func SetReadTimeout

func SetReadTimeout(readTimeout time.Duration) func(*ServerConfig)

func SetRecoverPanic

func SetRecoverPanic(recoverPanic bool) func(*ServerConfig)

func SetServerConfig

func SetServerConfig(cfg *ServerConfig) func(*Server)

func SetStaticDir

func SetStaticDir(staticDir string) func(*ServerConfig)

func SetWriteTimeout

func SetWriteTimeout(writeTimeout time.Duration) func(*ServerConfig)

func Slug

func Slug(s string, sep string) string

Slug is a helper function that returns the URL slug for string s. It's used to return clean, URL-friendly strings that can be used in routing.

func UrlEncode

func UrlEncode(data map[string]string) string

UrlEncode is a helper method that converts a map into URL-encoded form data. It is a useful when constructing HTTP POST requests.

Types

type Context

type Context struct {
	Request     *http.Request
	RequestBody []byte
	Params      map[string]string
	Server      *Server
	http.ResponseWriter
	// contains filtered or unexported fields
}

func (*Context) Abort

func (ctx *Context) Abort(status int, body string)

Abort is a helper method that sends an HTTP header and an optional body. It is useful for returning 4xx or 5xx errors. Once it has been called, any return value from the handler will not be written to the response.

func (*Context) AbortBytes

func (ctx *Context) AbortBytes(status int, body []byte)

Abort is a helper method that sends an HTTP header and an optional body. It is useful for returning 4xx or 5xx errors. Once it has been called, any return value from the handler will not be written to the response.

func (*Context) AcceptsHtml

func (ctx *Context) AcceptsHtml() bool

Checks if request accepts html response

func (*Context) AcceptsJson

func (ctx *Context) AcceptsJson() bool

Checks if request accepts json response

func (*Context) AcceptsXml

func (ctx *Context) AcceptsXml() bool

Checks if request accepts xml response

func (*Context) ContentType

func (ctx *Context) ContentType(val string) string

ContentType sets the Content-Type header for an HTTP response. For example, ctx.ContentType("json") sets the content-type to "application/json" If the supplied value contains a slash (/) it is set as the Content-Type verbatim. The return value is the content type as it was set, or an empty string if none was found.

func (*Context) Cookie

func (ctx *Context) Cookie(key string) string

Cookie returns request cookie item string by a given key. if non-existed, return empty string.

func (*Context) CopyBody

func (ctx *Context) CopyBody() []byte

CopyBody returns the raw request body data as bytes.

func (*Context) Domain

func (ctx *Context) Domain() string

Domain returns host name. Alias of Host method.

func (*Context) Forbidden

func (ctx *Context) Forbidden()

Forbidden writes a 403 HTTP response

func (*Context) GetBasicAuth

func (ctx *Context) GetBasicAuth() (string, string, error)

GetBasicAuth is a helper method of *Context that returns the decoded user and password from the *Context's authorization header

func (*Context) GetSecureCookie

func (ctx *Context) GetSecureCookie(name string) (string, bool)

func (*Context) Header

func (ctx *Context) Header(key string) string

Header returns request header item string by a given string. if non-existed, return empty string.

func (*Context) Host

func (ctx *Context) Host() string

Host returns host name. if no host info in request, return localhost.

func (*Context) IP

func (ctx *Context) IP() string

IP returns request client ip. if in proxy, return first proxy id. if error, return 127.0.0.1.

func (*Context) Is

func (ctx *Context) Is(method string) bool

Is returns boolean of this request is on given method, such as Is("POST").

func (*Context) IsAjax

func (ctx *Context) IsAjax() bool

IsAjax returns boolean of this request is generated by ajax.

func (*Context) IsDelete

func (ctx *Context) IsDelete() bool

Is this a DELETE method request?

func (*Context) IsGet

func (ctx *Context) IsGet() bool

Is this a GET method request?

func (*Context) IsHead

func (ctx *Context) IsHead() bool

Is this a Head method request?

func (*Context) IsOptions

func (ctx *Context) IsOptions() bool

Is this a OPTIONS method request?

func (*Context) IsPatch

func (ctx *Context) IsPatch() bool

Is this a PATCH method request?

func (*Context) IsPost

func (ctx *Context) IsPost() bool

Is this a POST method request?

func (*Context) IsPut

func (ctx *Context) IsPut() bool

Is this a PUT method request?

func (*Context) IsSecure

func (ctx *Context) IsSecure() bool

IsSecure returns boolean of this request is in https.

func (*Context) IsUpload

func (ctx *Context) IsUpload() bool

IsUpload returns boolean of whether file uploads in this request or not..

func (*Context) IsWebsocket

func (ctx *Context) IsWebsocket() bool

IsWebsocket returns boolean of this request is in webSocket.

func (*Context) Method

func (ctx *Context) Method() string

Method returns http request method.

func (*Context) NotFound

func (ctx *Context) NotFound(message string)

NotFound writes a 404 HTTP response

func (*Context) NotModified

func (ctx *Context) NotModified()

Notmodified writes a 304 HTTP response

func (*Context) Param

func (ctx *Context) Param(key string, def ...string) string

Param returns router param by a given key.没找到并且没有默认值则返回""字符串

func (*Context) ParamBol

func (ctx *Context) ParamBol(key string, def ...bool) bool

没找到并且没有默认值则 panic抛错

func (*Context) ParamF32

func (ctx *Context) ParamF32(key string, def ...float32) float32

没找到并且没有默认值则 panic抛错

func (*Context) ParamF64

func (ctx *Context) ParamF64(key string, def ...float64) float64

没找到并且没有默认值则 panic抛错

func (*Context) ParamI

func (ctx *Context) ParamI(key string, def ...int) int

没找到并且没有默认值则 panic抛错

func (*Context) ParamI16

func (ctx *Context) ParamI16(key string, def ...int16) int16

没找到并且没有默认值则 panic抛错

func (*Context) ParamI32

func (ctx *Context) ParamI32(key string, def ...int32) int32

没找到并且没有默认值则 panic抛错

func (*Context) ParamI64

func (ctx *Context) ParamI64(key string, def ...int64) int64

没找到并且没有默认值则 panic抛错

func (*Context) ParamI8

func (ctx *Context) ParamI8(key string, def ...int8) int8

没找到并且没有默认值则 panic抛错

func (*Context) ParamStr

func (ctx *Context) ParamStr(key string, def ...string) string

没找到并且没有默认值则 panic抛错

func (*Context) ParamU

func (ctx *Context) ParamU(key string, def ...uint) uint

没找到并且没有默认值则 panic抛错

func (*Context) ParamU16

func (ctx *Context) ParamU16(key string, def ...uint16) uint16

没找到并且没有默认值则 panic抛错

func (*Context) ParamU32

func (ctx *Context) ParamU32(key string, def ...uint32) uint32

没找到并且没有默认值则 panic抛错

func (*Context) ParamU64

func (ctx *Context) ParamU64(key string, def ...uint64) uint64

没找到并且没有默认值则 panic抛错

func (*Context) ParamU8

func (ctx *Context) ParamU8(key string, def ...uint8) uint8

没找到并且没有默认值则 panic抛错

func (*Context) ParseFormOrMutliForm

func (ctx *Context) ParseFormOrMutliForm(maxMemory int64) error

parseForm or parseMultiForm based on Content-type

func (*Context) Port

func (ctx *Context) Port() int

Port returns request client port. when error or empty, return 80.

func (*Context) Protocol

func (ctx *Context) Protocol() string

Protocol returns request protocol name, such as HTTP/1.1 .

func (*Context) Proxy

func (ctx *Context) Proxy() []string

Proxy returns proxy client ips slice.

func (*Context) Redirect

func (ctx *Context) Redirect(status int, url_ string)

Redirect is a helper method for 3xx redirects.

func (*Context) Refer

func (ctx *Context) Refer() string

Refer returns http referer header.

func (*Context) Referer

func (ctx *Context) Referer() string

Referer returns http referer header.

func (*Context) Scheme

func (ctx *Context) Scheme() string

Scheme returns request scheme as "http" or "https".

func (*Context) SetCacheControl

func (ctx *Context) SetCacheControl(expires time.Duration)

func (*Context) SetCookie

func (ctx *Context) SetCookie(cookie *http.Cookie)

SetCookie adds a cookie header to the response.

func (*Context) SetExpires

func (ctx *Context) SetExpires(expires time.Duration)

func (*Context) SetHeader

func (ctx *Context) SetHeader(hdr string, val string, unique bool)

SetHeader sets a response header. If `unique` is true, the current value of that header will be overwritten . If false, it will be appended.

func (*Context) SetLastModified

func (ctx *Context) SetLastModified(modTime time.Time)

func (*Context) SetSecureCookie

func (ctx *Context) SetSecureCookie(name string, val string, age int64)

func (*Context) Site

func (ctx *Context) Site() string

Site returns base site url as scheme://domain type.

func (*Context) SubDomains

func (ctx *Context) SubDomains() string

SubDomains returns sub domain string. if aa.bb.domain.com, returns aa.bb .

func (*Context) TraceErrorf

func (ctx *Context) TraceErrorf(format string, a ...interface{})

func (*Context) TraceFinish

func (ctx *Context) TraceFinish()

func (*Context) TracePrintf

func (ctx *Context) TracePrintf(format string, a ...interface{})

func (*Context) Unauthorized

func (ctx *Context) Unauthorized()

Unauthorized writes a 401 HTTP response

func (*Context) Uri

func (ctx *Context) Uri() string

Uri returns full request url with query string, fragment.

func (*Context) Url

func (ctx *Context) Url() string

Url returns request url path (without query string, fragment).

func (*Context) UserAgent

func (ctx *Context) UserAgent() string

UserAgent returns request client user agent string.

func (*Context) WriteBytes

func (ctx *Context) WriteBytes(content []byte)

WriteString writes string data into the response object.

func (*Context) WriteString

func (ctx *Context) WriteString(content string)

WriteString writes string data into the response object.

type ContextHandler

type ContextHandler interface {
	ContextHandle(*Context) (interface{}, error)
}

type Server

type Server struct {
	Config *ServerConfig

	Env map[string]interface{}
	// contains filtered or unexported fields
}

Server represents a web.go server.

func NewServer

func NewServer(options ...func(*Server)) *Server

func (*Server) Close

func (s *Server) Close()

Close stops server s.

func (*Server) Closed

func (s *Server) Closed() bool

func (*Server) Delete

func (s *Server) Delete(route string, handler interface{})

Delete adds a handler for the 'DELETE' http method for server s.

func (*Server) Get

func (s *Server) Get(route string, handler interface{})

Get adds a handler for the 'GET' http method for server s.

func (*Server) Handler

func (s *Server) Handler(route string, method string, httpHandler http.Handler)

Adds a custom handler. Only for webserver mode. Will have no effect when running as FCGI or SCGI.

func (*Server) Match

func (s *Server) Match(route string, handler interface{}, method string)

Match adds a handler for an arbitrary http method for server s.

func (*Server) Matches

func (s *Server) Matches(route string, handler interface{}, methods ...string)

Match adds a handler for an arbitrary http method for server s.

func (*Server) Post

func (s *Server) Post(route string, handler interface{})

Post adds a handler for the 'POST' http method for server s.

func (*Server) Process

func (s *Server) Process(c http.ResponseWriter, req *http.Request)

Process invokes the routing system for server s

func (*Server) Put

func (s *Server) Put(route string, handler interface{})

Put adds a handler for the 'PUT' http method for server s.

func (*Server) Run

func (s *Server) Run(addr string)

Run starts the web application and serves HTTP requests for s

func (*Server) RunFCGI

func (s *Server) RunFCGI(addr string)

RunFCGI starts the web application and serves FastCGI requests for s.

func (*Server) RunMux

func (s *Server) RunMux(basePattern, addr string)

func (*Server) RunSCGI

func (s *Server) RunSCGI(addr string)

RunSCGI starts the web application and serves SCGI requests for s.

func (*Server) RunTLS

func (s *Server) RunTLS(addr string, config *tls.Config)

RunTLS starts the web application and serves HTTPS requests for s.

func (*Server) RunTLSMux

func (s *Server) RunTLSMux(addr string, config *tls.Config)

RunTLS starts the web application and serves HTTPS requests for s.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(c http.ResponseWriter, req *http.Request)

ServeHTTP is the interface method for Go's http server package

type ServerConfig

type ServerConfig struct {
	StaticDir      string
	CookieSecret   string
	RecoverPanic   bool
	KeepAlive      bool
	ReadTimeout    time.Duration
	WriteTimeout   time.Duration
	MaxHeaderBytes int
	MaxMemory      int64
}

ServerConfig is configuration for server objects.

func NewServerConfig

func NewServerConfig(options ...func(*ServerConfig)) *ServerConfig

type Service

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

func InvokeService

func InvokeService(handler interface{}, methodName string) *Service

根据结构体的方法名注册为http服务方法 handler's kind must be ptr

func (*Service) ServiceCall

func (s *Service) ServiceCall(ctx *Context, args ...reflect.Value) (robj interface{}, err error)

Jump to

Keyboard shortcuts

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