cosweb

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2024 License: Apache-2.0 Imports: 35 Imported by: 2

README

cosgo

go game 开发中

Documentation

Index

Constants

View Source
const (
	HeaderAccept              = "Accept"
	HeaderAcceptEncoding      = "Accept-Encoding"
	HeaderAllow               = "Allow"
	HeaderAuthorization       = "Authorization"
	HeaderContentDisposition  = "Content-Disposition"
	HeaderContentEncoding     = "Content-Encoding"
	HeaderContentLength       = "Content-Length"
	HeaderContentType         = "Content-Type"
	HeaderCookie              = "Cookie"
	HeaderSetCookie           = "connect-Cookie"
	HeaderIfModifiedSince     = "If-Modified-Since"
	HeaderLastModified        = "Last-Modified"
	HeaderLocation            = "Location"
	HeaderUpgrade             = "Upgrade"
	HeaderVary                = "Vary"
	HeaderWWWAuthenticate     = "WWW-Authenticate"
	HeaderXForwardedFor       = "X-Forwarded-For"
	HeaderXForwardedProto     = "X-Forwarded-Code"
	HeaderXForwardedProtocol  = "X-Forwarded-Protocol"
	HeaderXForwardedSsl       = "X-Forwarded-Ssl"
	HeaderXUrlScheme          = "X-Url-Protocol"
	HeaderXHTTPMethodOverride = "X-HTTP-value-Override"
	HeaderXRealIP             = "X-Real-Addr"
	HeaderXRequestID          = "X-Request-Index"
	HeaderXRequestedWith      = "X-Requested-With"
	HeaderServer              = "engine"
	HeaderOrigin              = "origin"

	// Access control
	HeaderAccessControlRequestMethod    = "Access-Control-Request-value"
	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"

	// Security
	HeaderStrictTransportSecurity         = "Strict-Transport-Security"
	HeaderXContentTypeOptions             = "X-Content-Type-options"
	HeaderXXSSProtection                  = "X-XSS-Protection"
	HeaderXFrameOptions                   = "X-Frame-options"
	HeaderContentSecurityPolicy           = "Content-Security-Policy"
	HeaderContentSecurityPolicyReportOnly = "Content-Security-Policy-Report-Only"
	HeaderXCSRFToken                      = "X-CSRF-Token"
	HeaderReferrerPolicy                  = "Referrer-Policy"
)

Headers

View Source
const (
	ContentTypeTextHTML            ContentType = "text/html"
	ContentTypeTextPlain                       = "text/plain"
	ContentTypeTextXML                         = "text/xml"
	ContentTypeApplicationJS                   = "application/javascript"
	ContentTypeApplicationXML                  = "application/xml"
	ContentTypeApplicationJSON                 = "application/json"
	ContentTypeApplicationProtobuf             = "application/protobuf"
	ContentTypeApplicationMsgpack              = "application/msgpack"
	MIMEPROTOBUF                               = "application/x-protobuf"
	MIMEMSGPACKX                               = "application/x-msgpack"
	MIMEMSGPACK                                = "application/msgpack"
	ContentTypeOctetStream                     = "application/octet-stream"
	ContentTypeMultipartForm                   = "multipart/form-store"
	ContentTypeApplicationForm                 = "application/x-www-form-urlencoded"
)

MIME types

Variables

View Source
var (
	ErrUnsupportedMediaType        = NewHTTPError(http.StatusUnsupportedMediaType)
	ErrNotFound                    = NewHTTPError(http.StatusNotFound, "404 page not found")
	ErrUnauthorized                = NewHTTPError(http.StatusUnauthorized)
	ErrForbidden                   = NewHTTPError(http.StatusForbidden)
	ErrMethodNotAllowed            = NewHTTPError(http.StatusMethodNotAllowed)
	ErrStatusRequestEntityTooLarge = NewHTTPError(http.StatusRequestEntityTooLarge)
	ErrTooManyRequests             = NewHTTPError(http.StatusTooManyRequests)
	ErrBadRequest                  = NewHTTPError(http.StatusBadRequest)
	ErrBadGateway                  = NewHTTPError(http.StatusBadGateway)
	ErrInternalServerError         = NewHTTPError(http.StatusInternalServerError)
	ErrRequestTimeout              = NewHTTPError(http.StatusRequestTimeout)
	ErrServiceUnavailable          = NewHTTPError(http.StatusServiceUnavailable)
	ErrValidatorNotRegistered      = errors.New("validator not registered")
	ErrRendererNotRegistered       = errors.New("renderer not registered")
	ErrInvalidRedirectCode         = errors.New("invalid redirect status code")
	ErrCookieNotFound              = errors.New("cookie not found")
	ErrInvalidCertOrKeyType        = errors.New("invalid cert or key type, must be string or []byte")
	ErrArgsNotFound                = errors.New("args not found")
	ErrMimeTypeNotFound            = errors.New("mime type not found")
	ErrHandlerError                = errors.New("handler type error")
)

Errors

View Source
var Charset = "UTF-8"

Functions

func GetContentTypeCharset

func GetContentTypeCharset(contentType ContentType) string

GetContentTypeCharset

func NewRender

func NewRender(options *render.Options) *render.Render

func RegisterListener added in v0.0.3

func RegisterListener(network string, ml MakeListener)

RegisterListener registers a MakeListener for network.

func TLSConfigAutocert

func TLSConfigAutocert() (c *tls.Config, err error)

createTLS starts an HTTPS server using certificates automatically installed from https://letsencrypt.org.

func TLSConfigParse

func TLSConfigParse(certFile, keyFile interface{}) (TLSConfig *tls.Config, err error)

通过文件或者证书内容获取TLSConfig

func TLSConfigTransform

func TLSConfigTransform(key, pem string) (c *tls.Config, err error)

Types

type Body

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

func NewBody

func NewBody() *Body

func (*Body) Bind

func (this *Body) Bind(i interface{}) error

func (*Body) Bytes

func (this *Body) Bytes() []byte

func (*Body) Get

func (this *Body) Get(key string) (val interface{}, ok bool)

func (*Body) Reader

func (this *Body) Reader() (r io.Reader, err error)

func (*Body) Values

func (this *Body) Values() (values.Values, error)

type ContentType

type ContentType string

type Context

type Context struct {
	Body     *Body
	Binder   binder.Interface
	Cookie   *Cookie
	Session  *session.Session
	Request  *http.Request
	Response http.ResponseWriter
	// contains filtered or unexported fields
}

Context API上下文.

func NewContext

func NewContext(s *Server) *Context

NewContext returns a Context instance.

func (*Context) Abort

func (c *Context) Abort()

func (*Context) Attachment

func (c *Context) Attachment(file, name string) error

Attachment 最终走File

func (*Context) Bind

func (c *Context) Bind(i interface{}) error

Bind 绑定JSON XML

func (*Context) Bytes

func (c *Context) Bytes(contentType ContentType, b []byte) (err error)

func (*Context) File

func (c *Context) File(file string) (err error)

func (*Context) Flush

func (c *Context) Flush()

Flush implements the http.Flusher interface to allow an HTTP handler to flush buffered store to the client. See http.Flusher(https://golang.org/pkg/net/http/#Flusher)

func (*Context) Get

func (c *Context) Get(key string, dts ...RequestDataType) interface{}

Get 获取参数,优先路径中的params 其他方式直接使用c.Request...

func (*Context) GetFloat

func (c *Context) GetFloat(key string, dts ...RequestDataType) (r float64)

func (*Context) GetInt

func (c *Context) GetInt(key string, dts ...RequestDataType) (r int)

func (*Context) GetInt32 added in v0.1.0

func (c *Context) GetInt32(key string, dts ...RequestDataType) (r int32)

func (*Context) GetInt64 added in v0.1.0

func (c *Context) GetInt64(key string, dts ...RequestDataType) (r int64)

func (*Context) GetString

func (c *Context) GetString(key string, dts ...RequestDataType) (r string)

func (*Context) HTML

func (c *Context) HTML(html string) (err error)

func (*Context) Header

func (c *Context) Header() http.Header

func (*Context) Hijack

func (c *Context) Hijack() (net.Conn, *bufio.ReadWriter, error)

Hijack implements the http.Hijacker interface to allow an HTTP handler to take over the connection. See http.Hijacker(https://golang.org/pkg/net/http/#Hijacker)

func (*Context) Inline

func (c *Context) Inline(file, name string) error

Inline 最终走File

func (*Context) IsWebSocket

func (c *Context) IsWebSocket() bool

IsWebSocket 判断是否WebSocket

func (*Context) JSON

func (c *Context) JSON(i interface{}) error

func (*Context) JSONP

func (c *Context) JSONP(callback string, i interface{}) error

func (*Context) Protocol

func (c *Context) Protocol() string

Protocol 协议

func (*Context) Redirect

func (c *Context) Redirect(url string) error

func (*Context) RemoteAddr

func (c *Context) RemoteAddr() string

RemoteAddr 客户端地址

func (*Context) Render

func (c *Context) Render(name string, data interface{}) (err error)

func (*Context) Route

func (c *Context) Route() string

Route 当期匹配的路由

func (*Context) Stream

func (c *Context) Stream(contentType ContentType, r io.Reader) (err error)

func (*Context) String

func (c *Context) String(s string) (err error)

func (*Context) Writable

func (c *Context) Writable() bool

Writable 是否可写,如果已经写入头则返回FALSE

func (*Context) Write

func (c *Context) Write(b []byte) (n int, err error)

Write writes the store to the connection as part of an HTTP reply.

func (*Context) WriteHeader

func (c *Context) WriteHeader(code int)

Status sends an HTTP Response header with status code. If Status is not called explicitly, the first call to Write will trigger an implicit Status(http.StatusOK). Thus explicit calls to Status are mainly used to send error codes.

func (*Context) XML

func (c *Context) XML(i interface{}, indent string) (err error)
type Cookie struct {
	// contains filtered or unexported fields
}

func NewCookie

func NewCookie(c *Context) *Cookie

func (*Cookie) Get

func (this *Cookie) Get(key string) string

func (*Cookie) GetCookie

func (this *Cookie) GetCookie(key string) (*http.Cookie, error)

func (*Cookie) Set

func (this *Cookie) Set(key, val string)

func (*Cookie) SetCookie

func (this *Cookie) SetCookie(cookie *http.Cookie)

type HTTPError

type HTTPError struct {
	Code    int         `json:"-"`
	Message interface{} `json:"message"`
}

HTTPError represents an error that occurred while handling a Request.

func NewHTTPError

func NewHTTPError(code int, message ...interface{}) *HTTPError

NewHTTPError creates a new HTTPError instance.

func NewHTTPError500

func NewHTTPError500(message interface{}) *HTTPError

func (*HTTPError) Error

func (he *HTTPError) Error() string

Error makes it compatible with `error` interface.

func (*HTTPError) String

func (he *HTTPError) String() string

type HTTPErrorHandler

type HTTPErrorHandler func(*Context, error)

HTTPErrorHandler is a centralized HTTP error handler.

type Handler added in v0.0.3

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

func (*Handler) Caller added in v0.0.3

func (h *Handler) Caller(node *registry.Node, c *Context) (reply interface{}, err error)

func (*Handler) Filter added in v0.0.3

func (h *Handler) Filter(node *registry.Node) bool

func (*Handler) Serialize added in v0.0.3

func (this *Handler) Serialize(c *Context, reply interface{}) (err error)

func (*Handler) Use added in v0.0.3

func (h *Handler) Use(src interface{})

type HandlerCaller added in v0.0.3

type HandlerCaller func(node *registry.Node, c *Context) (interface{}, error)

type HandlerFilter added in v0.0.3

type HandlerFilter func(node *registry.Node) bool

type HandlerFunc

type HandlerFunc func(*Context, Next) error

HandlerFunc defines a function to serve HTTP requests.

type HandlerSerialize added in v0.0.3

type HandlerSerialize func(c *Context, reply interface{}) (interface{}, error)

type MakeListener added in v0.0.3

type MakeListener func(address string, tlsConfig *tls.Config) (ln net.Listener, err error)

MakeListener defines a listener generator.

func Listener added in v0.0.3

func Listener(network string) MakeListener

type MiddlewareFunc

type MiddlewareFunc func(*Context, Next) error

MiddlewareFunc defines a function to process middleware.

type Next

type Next func() error

type Proxy

type Proxy struct {
	GetTarget func(*Context, []*url.URL) url.URL //获取目标服务器地址,适用于负载均衡
	// contains filtered or unexported fields
}

func NewProxy

func NewProxy(address ...string) *Proxy

func (*Proxy) AddTarget

func (this *Proxy) AddTarget(addr string) error

添加代理服务器地址

func (*Proxy) Route

func (this *Proxy) Route(s *Server, prefix string, method ...string)

type Render

type Render interface {
	Render(io.Writer, string, interface{}) error
}

Render is the interface that wraps the Render function.

type RequestDataType

type RequestDataType int
const (
	RequestDataTypeParam  RequestDataType = iota //params
	RequestDataTypeBody                          //POST json, xml,pb,form....
	RequestDataTypeQuery                         //GET
	RequestDataTypeCookie                        //COOKIES
	RequestDataTypeHeader                        //HEADER
)

type RequestDataTypeMap

type RequestDataTypeMap []RequestDataType

func (*RequestDataTypeMap) Add

func (r *RequestDataTypeMap) Add(keys ...RequestDataType)

func (RequestDataTypeMap) IndexOf

func (r RequestDataTypeMap) IndexOf(v RequestDataType) int

func (*RequestDataTypeMap) Reset

func (r *RequestDataTypeMap) Reset(keys ...RequestDataType)

type Server

type Server struct {
	SCC *scc.SCC

	Binder           binder.Interface //默认序列化方式
	Render           Render
	Server           *http.Server
	Router           *registry.Router
	Registry         *registry.Registry
	RequestDataType  RequestDataTypeMap //使用GET获取数据时默认的查询方式
	HTTPErrorHandler HTTPErrorHandler
	// contains filtered or unexported fields
}

Server is the top-level framework instance.

func New added in v0.0.6

func New(ctx context.Context) (s *Server)

New creates an instance of Server.

func (*Server) Acquire

func (srv *Server) Acquire(w http.ResponseWriter, r *http.Request) *Context

Acquire returns an empty `Context` instance from the pool. You must return the Context by calling `ReleaseContext()`.

func (*Server) Close

func (srv *Server) Close() error

func (*Server) DefaultHTTPErrorHandler

func (srv *Server) DefaultHTTPErrorHandler(c *Context, err error)

DefaultHTTPErrorHandler is the default HTTP error handler. It sends a JSON Response with status code.

func (*Server) GET

func (srv *Server) GET(path string, h HandlerFunc)

GET registers a new GET Register for a path with matching handler in the Router with optional Register-level middleware.

func (*Server) Listen added in v0.0.7

func (srv *Server) Listen(ln net.Listener) (err error)

func (*Server) POST

func (srv *Server) POST(path string, h HandlerFunc)

POST registers a new POST Register for a path with matching handler in the Router with optional Register-level middleware.

func (*Server) Proxy

func (srv *Server) Proxy(prefix, address string, method ...string) *Proxy

代理服务器

func (*Server) Register

func (srv *Server) Register(route string, handler HandlerFunc, method ...string)

Register AddTarget registers a new Register for an HTTP value and path with matching handler in the Router with optional Register-level middleware.

func (*Server) Release

func (srv *Server) Release(c *Context)

Release returns the `Context` instance back to the pool. You must call it after `AcquireContext()`.

func (*Server) ServeHTTP

func (srv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements `http.Handler` interface, which serves HTTP requests.

func (*Server) Service added in v0.0.3

func (srv *Server) Service(name string, handler ...interface{}) *registry.Service

Service 使用Registry的Service批量注册struct

func (*Server) Start

func (srv *Server) Start(address string, tlsConfig ...*tls.Config) (err error)

Start starts an HTTP server.

func (*Server) Static

func (srv *Server) Static(prefix, root string, method ...string) *Static

Static registers a new Register with path prefix to serve static files from the provided root directory. 如果root 不是绝对路径 将以程序的WorkDir为根目录

func (*Server) Use

func (srv *Server) Use(middleware ...MiddlewareFunc)

Use adds middleware to the chain which is run after Router.

type Static

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

func NewStatic

func NewStatic(root string) *Static

func (*Static) Route

func (this *Static) Route(s *Server, prefix string, method ...string)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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