server

package
v0.0.0-...-7429660 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2023 License: Apache-2.0 Imports: 41 Imported by: 1

Documentation

Index

Constants

View Source
const (
	HTTPReadTimeout  = 60 * time.Second
	HTTPWriteTimeout = 60 * time.Second
	HTTPIdleTimeout  = 90 * time.Second
)

Variables

This section is empty.

Functions

func RegisterOnGlobalStage

func RegisterOnGlobalStage(plugFunc ...HandlerFunc)

plugins will be effect always

func RegisterOnRequestStage

func RegisterOnRequestStage(plugFunc ...HandlerFunc)

plugins will be effect for a http request or a http route

Types

type Context

type Context struct {
	Request   *http.Request
	Response  Responser
	Params    Params
	Path      string // raw match path
	Peer      string // 包含app_name的上游service_name
	Namespace string
	Ctx       context.Context // for trace or others store

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

func (*Context) Abort

func (c *Context) Abort()

func (*Context) AbortErr

func (c *Context) AbortErr(err error)

func (*Context) Baggage

func (c *Context) Baggage(key string) string

获取已有的用户自定义baggage信息

func (*Context) Bind

func (c *Context) Bind(r *http.Request, model interface{}, atom ...interface{}) error

func (*Context) BusiCode

func (c *Context) BusiCode() int32

func (*Context) DefaultQuery

func (c *Context) DefaultQuery(key, defaultValue string) string

func (*Context) Err

func (c *Context) Err() error

func (*Context) ForeachBaggage

func (c *Context) ForeachBaggage(handler func(key, val string) error) error

遍历处理用户自定义的baggage信息

func (*Context) Get

func (c *Context) Get(key string) (value interface{}, exists bool)

func (*Context) GetBool

func (c *Context) GetBool(key string) (b bool)

func (*Context) GetDuration

func (c *Context) GetDuration(key string) (d time.Duration)

func (*Context) GetFloat64

func (c *Context) GetFloat64(key string) (f64 float64)

func (*Context) GetInt

func (c *Context) GetInt(key string) (i int)

func (*Context) GetInt64

func (c *Context) GetInt64(key string) (i64 int64)

func (*Context) GetQuery

func (c *Context) GetQuery(key string) (string, bool)

func (*Context) GetQueryArray

func (c *Context) GetQueryArray(key string) ([]string, bool)

func (*Context) GetString

func (c *Context) GetString(key string) (s string)

func (*Context) GetStringMap

func (c *Context) GetStringMap(key string) (sm map[string]interface{})

func (*Context) GetStringMapString

func (c *Context) GetStringMapString(key string) (sms map[string]string)

func (*Context) GetStringMapStringSlice

func (c *Context) GetStringMapStringSlice(key string) (smss map[string][]string)

func (*Context) GetStringSlice

func (c *Context) GetStringSlice(key string) (ss []string)

func (*Context) GetTime

func (c *Context) GetTime(key string) (t time.Time)

func (*Context) JSON

func (c *Context) JSON(data interface{}, err error)

write response, error include business code and error msg

func (*Context) JSONAbort

func (c *Context) JSONAbort(data interface{}, err error)

wrap on JSON

func (*Context) JSONOrError

func (c *Context) JSONOrError(data interface{}, err error)

JSONOrError will handle error and write either JSON or error to response. good example: ```

resp, err := svc.MessageService()
c.JSONOrError(resp, err)

``` bad example: ```

resp, err := svc.MessageService()
c.JSON(resp, err)

```

func (*Context) LoggingExtra

func (c *Context) LoggingExtra(vals ...interface{})

func (*Context) MustGet

func (c *Context) MustGet(key string) interface{}

func (*Context) Next

func (c *Context) Next()

func (*Context) Query

func (c *Context) Query(key string) string

Query returns the keyed url query value if it exists, otherwise it returns an empty string `("")`. It is shortcut for `c.Request.URL.Query().Get(key)`

    GET /path?id=1234&name=Manu&value=
	   c.Query("id") == "1234"
	   c.Query("name") == "Manu"
	   c.Query("value") == ""
	   c.Query("wtf") == ""

func (*Context) QueryArray

func (c *Context) QueryArray(key string) []string

func (*Context) QueryInt

func (c *Context) QueryInt(key string) int

func (*Context) QueryInt64

func (c *Context) QueryInt64(key string) int64

func (*Context) Raw

func (c *Context) Raw(data interface{}, code int32)

write response, data include error info

func (*Context) Set

func (c *Context) Set(key string, value interface{})

func (*Context) SetBaggage

func (c *Context) SetBaggage(key, value string) context.Context

设置用户自定义信息到span baggage item中

func (*Context) SetBusiCode

func (c *Context) SetBusiCode(code int32)

func (*Context) TraceID

func (c *Context) TraceID() string

type HandlerFunc

type HandlerFunc func(c *Context)

core plugin encapsulation

func PrintBodyLog

func PrintBodyLog(printReq, printResp bool) HandlerFunc

func PrintRespBody deprecated

func PrintRespBody(b bool) HandlerFunc

Deprecated: PrintRespBody func should not use anymore. Use PrintBodyLog func instead

func (HandlerFunc) Do

func (p HandlerFunc) Do(ctx context.Context, flow core.Core)

type Option

type Option func(*Options)

func Breaker

func Breaker(brk *breaker.Config) Option

func CertFile

func CertFile(file string) Option

func IdleTimeout

func IdleTimeout(d time.Duration) Option

func KeyFile

func KeyFile(file string) Option

func Limiter

func Limiter(lim *ratelimit.Config) Option

func Logger

func Logger(logger log.Kit) Option

func Manager

func Manager(re *registry.ServiceManager) Option

func Name

func Name(serviceName string) Option

func Port

func Port(port int) Option

func ReadTimeout

func ReadTimeout(d time.Duration) Option

从连接被接受(accept)到request body完全被读取(如果你不读取body,那么时间截止到读完header为止) 包括了TCP消耗的时间,读header时间 对于 https请求,ReadTimeout 包括了TLS握手的时间

func RecoverPanic

func RecoverPanic(rp bool) Option

func Registry

func Registry(r registry.Backend) Option

func RequestBodyLogOff

func RequestBodyLogOff(b bool) Option

关闭req body 打印

func RespBodyLogMaxSize

func RespBodyLogMaxSize(size int) Option

控制resp body打印大小

func Tags

func Tags(tags map[string]string) Option

func Tracer

func Tracer(tracer opentracing.Tracer) Option

func WriteTimeout

func WriteTimeout(d time.Duration) Option

从request header的读取结束开始,到response write结束为止 (也就是 ServeHTTP 方法的声明周期)

type Options

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

type Param

type Param struct {
	Key   string
	Value string
}

Param is a single URL parameter, consisting of a key and a value.

type Params

type Params []Param

func (Params) ByName

func (ps Params) ByName(name string) (va string)

func (Params) Get

func (ps Params) Get(name string) (string, bool)

type Responser

type Responser interface {
	http.ResponseWriter
	Size() int
	Status() int
	Writer() http.ResponseWriter
	WriteString(string) (int, error)
	WriteJSON(interface{}) (int, error)
	ByteBody() []byte
	StringBody() string
	// contains filtered or unexported methods
}

type Router

type Router interface {
	GROUP(string, ...HandlerFunc) *RouterMgr
	ANY(string, ...HandlerFunc) Router
	GET(string, ...HandlerFunc) Router
	POST(string, ...HandlerFunc) Router
	GETPOST(string, ...HandlerFunc) Router
	DELETE(string, ...HandlerFunc) Router
	PATCH(string, ...HandlerFunc) Router
	PUT(string, ...HandlerFunc) Router
	OPTIONS(string, ...HandlerFunc) Router
	HEAD(string, ...HandlerFunc) Router
}

router

type RouterMgr

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

func (*RouterMgr) ANY

func (mgr *RouterMgr) ANY(relativePath string, handlers ...HandlerFunc) Router

func (*RouterMgr) DELETE

func (mgr *RouterMgr) DELETE(relativePath string, handlers ...HandlerFunc) Router

func (*RouterMgr) GET

func (mgr *RouterMgr) GET(relativePath string, handlers ...HandlerFunc) Router

func (*RouterMgr) GETPOST

func (mgr *RouterMgr) GETPOST(relativePath string, handlers ...HandlerFunc) Router

func (*RouterMgr) GROUP

func (mgr *RouterMgr) GROUP(relativePath string, handleFunc ...HandlerFunc) *RouterMgr

实现Router

func (*RouterMgr) HEAD

func (mgr *RouterMgr) HEAD(relativePath string, handlers ...HandlerFunc) Router

func (*RouterMgr) OPTIONS

func (mgr *RouterMgr) OPTIONS(relativePath string, handlers ...HandlerFunc) Router

func (*RouterMgr) PATCH

func (mgr *RouterMgr) PATCH(relativePath string, handlers ...HandlerFunc) Router

func (*RouterMgr) POST

func (mgr *RouterMgr) POST(relativePath string, handlers ...HandlerFunc) Router

func (*RouterMgr) PUT

func (mgr *RouterMgr) PUT(relativePath string, handlers ...HandlerFunc) Router

type Server

type Server interface {
	Router
	Run(addr ...string) error
	Stop() error
	Use(p ...HandlerFunc)
	ServeHTTP(w http.ResponseWriter, r *http.Request)
}

func NewServer

func NewServer(options ...Option) Server

Jump to

Keyboard shortcuts

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