httpserver

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2023 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

实现服务中心规定的服务注册要求,遵循注册协议 engine.Container

分组路由注册

静态路由注册

Index

Constants

View Source
const Name = "httpserver"

Variables

This section is empty.

Functions

func Cross added in v0.10.0

func Cross(response http.ResponseWriter)

Types

type Context

type Context struct {

	// 配置服务
	Req    IRequest
	Resp   RespStruct
	Config config.Service
	Cache  cache.Service
	I18n   func(string) string
	// contains filtered or unexported fields
}

实现标准库的 Context 基本现在所有第三方库函数都会根据官方的建议将第一个参数设置为标准 Context 接口, 所以定制一个自己的 context 很有用,这里将 request 和 response 封装到 context, 这样就可以在整条请求链路中随时处理输入输出

func NewContext

func NewContext(r *http.Request, w http.ResponseWriter, holder core.Container) *Context

func (*Context) BaseContext

func (ctx *Context) BaseContext() context.Context

func (*Context) Deadline

func (ctx *Context) Deadline() (deadline time.Time, ok bool)

#region implement context.Context

func (*Context) Done

func (ctx *Context) Done() <-chan struct{}

func (*Context) Err

func (ctx *Context) Err() error

func (*Context) GetResponse

func (ctx *Context) GetResponse() http.ResponseWriter

func (*Context) GetVal added in v0.10.0

func (ctx *Context) GetVal(key string) *castkit.GoodleVal

func (*Context) HasTimeout

func (ctx *Context) HasTimeout() bool

func (*Context) Holder added in v0.10.0

func (ctx *Context) Holder() core.Container

对用户暴露服务者中心

func (*Context) NewInstanceProvider

func (ctx *Context) NewInstanceProvider(name string, params ...interface{}) interface{}

func (*Context) NewSingleProvider

func (ctx *Context) NewSingleProvider(name string) interface{}

将服务注册到服务中心

func (*Context) Next

func (ctx *Context) Next() error

按顺序执行中间件

func (*Context) Request added in v0.10.0

func (ctx *Context) Request() *http.Request

func (*Context) SetHasTimeout

func (ctx *Context) SetHasTimeout()

func (*Context) SetMiddwares added in v0.10.0

func (ctx *Context) SetMiddwares(handlers []MiddlewareHandler)

请求时中间件

func (*Context) SetVal added in v0.10.0

func (ctx *Context) SetVal(key string, value interface{})

往 context 上设置值/获取值

func (*Context) Value

func (ctx *Context) Value(key interface{}) interface{}

func (*Context) WriterMux

func (ctx *Context) WriterMux() *sync.Mutex

对外暴露锁

type Engine added in v0.10.0

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

框架核心结构体

func (*Engine) AddRoute added in v0.10.0

func (self *Engine) AddRoute(methoad, prefix, uri string, routeType int8, handler RequestHandler, middlewares ...MiddlewareHandler)

添加路由到 map prefix 主要用于请求进来时匹配分组中间件

func (*Engine) Delete added in v0.10.0

func (this *Engine) Delete(url string, handler RequestHandler, middlewares ...MiddlewareHandler)

func (*Engine) FindRouteHandler added in v0.10.0

func (this *Engine) FindRouteHandler(request *http.Request) t3WebRoute

匹配路由,如果没有匹配到,返回 nil

func (*Engine) Get added in v0.10.0

func (this *Engine) Get(url string, handler RequestHandler, middlewares ...MiddlewareHandler)

func (*Engine) NewHttpEngine added in v0.10.0

func (self *Engine) NewHttpEngine(serviceCenter core.Container) *Engine

初始化框架核心结构

func (*Engine) Post added in v0.10.0

func (this *Engine) Post(url string, handler RequestHandler, middlewares ...MiddlewareHandler)

func (*Engine) Prefix added in v0.10.0

func (hc *Engine) Prefix(prefix string) IGroup

实现 Group 方法

func (*Engine) Put added in v0.10.0

func (this *Engine) Put(url string, handler RequestHandler, middlewares ...MiddlewareHandler)

func (*Engine) ServeHTTP added in v0.10.0

func (self *Engine) ServeHTTP(response http.ResponseWriter, request *http.Request)

框架核心结构实现 Handler 接口

func (*Engine) UseCross added in v0.10.0

func (self *Engine) UseCross()

跨域

func (*Engine) UseMiddleware added in v0.10.0

func (self *Engine) UseMiddleware(handlers ...MiddlewareHandler)

注册全局中间件

type HttpServerProvider

type HttpServerProvider struct {
	core.ServiceProvider
	HttpServer *Engine
}

func (*HttpServerProvider) AfterInit added in v0.10.0

func (*HttpServerProvider) AfterInit(instance any) error

func (*HttpServerProvider) BeforeInit added in v0.10.0

func (self *HttpServerProvider) BeforeInit(c core.Container) error

func (*HttpServerProvider) InitOnBoot added in v0.10.0

func (*HttpServerProvider) InitOnBoot() bool

func (*HttpServerProvider) Name

func (self *HttpServerProvider) Name() string

func (*HttpServerProvider) Params

func (sp *HttpServerProvider) Params(c core.Container) []interface{}

func (*HttpServerProvider) RegisterProviderInstance

func (sp *HttpServerProvider) RegisterProviderInstance(c core.Container) core.NewInstanceFunc

type HttpServerService

type HttpServerService struct {
	*Engine
	// contains filtered or unexported fields
}

type IGroup

IGroup 路由分组接口

type IRequest

type IRequest interface {
	JsonScan(s any) error          // json body
	BindXml(obj interface{}) error // xml body
	GetRawData() ([]byte, error)   // 其他格式

	// 获取查询字符串中的参数,如: xxx.com?a=foo&b=bar&c[]=barbar
	Get(key string) interface{}
	GetInt(key string, defaultValue ...int) int
	GetInt64(key string, defaultValue ...int64) (int64, bool)
	GetFloat64(key string, defaultValue ...float64) (float64, bool)
	GetFloat32(key string, defaultValue ...float32) (float32, bool)
	GetBool(key string, defaultValue ...bool) (bool, bool)
	GetString(key string, defaultValue ...string) string
	GetStringSlice(key string, defaultValue ...[]string) ([]string, bool)

	PostInt(key string, defaultValue ...int) (int, bool)
	PostInt32(key string, defaultValue ...int32) (int32, bool)
	PostInt64(key string, defaultValue ...int64) (int64, bool)
	PostFloat32(key string, defaultValue ...float32) (float32, bool)
	PostFloat64(key string, defaultValue ...float64) (float64, bool)
	PostString(key string, defaultValue ...string) (string, bool)
	PostBool(key string, defaultValue ...bool) (bool, bool)

	// form 表单中的参数
	Form(key string) interface{}
	FormInt(key string, defaultValue ...int) (int, bool)
	FormInt64(key string, defaultValue ...int64) (int64, bool)
	FormFloat64(key string, defaultValue ...float64) (float64, bool)
	FormFloat32(key string, defaultValue ...float32) (float32, bool)
	FormBool(key string, defaultValue ...bool) (bool, bool)
	FormString(key string, defaultValue ...string) (string, bool)
	FormStringSlice(key string, defaultValue ...[]string) ([]string, bool)
	FormFile(key string, args ...int) (multipart.File, *multipart.FileHeader, url.Values, error)

	// 其他格式
	Uri() string
	Method() string
	Host() string
	ClientIp() string

	// header
	Headers() map[string][]string
	Header(key string) (string, bool)

	// cookie
	Cookie(key string) (string, bool)
	Cookies() map[string]string
	// contains filtered or unexported methods
}

为请求封装方法,在 Context 上实现接口 统一返回值中的 bool 代表请求方是否有传递这个数据过来

type IResponse

type IResponse interface {
	Json(obj interface{}) IResponse
	Html(template string, obj interface{}) IResponse
	Jsonp(obj interface{}) IResponse
	Xml(obj interface{}) IResponse
	Text(format string, values ...interface{}) IResponse
	Redirect(path string) IResponse // 重定向
	SetHeader(key string, val string) IResponse
	SetCookie(key string, val string, maxAge int, path, domain string, secure, httpOnly bool) IResponse
	SetOkStatus() IResponse       // 设置 200 状态
	SetStatus(code int) IResponse // 设置其他状态码
}

为响应封装方法 方法返回 IResponse 本身代表支持链式调用,例如:res.SetOkStatus().Json("success")

type MiddlewareHandler added in v0.10.0

type MiddlewareHandler func(c *Context) error

type Prefix

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

实现了 IGroup,按前缀分组

func NewPrefix

func NewPrefix(core *Engine, prefix string) *Prefix

初始化前缀分组

func (*Prefix) Delete

func (p *Prefix) Delete(uri string, handler RequestHandler, middlewares ...MiddlewareHandler)

func (*Prefix) Get

func (p *Prefix) Get(uri string, handler RequestHandler, middlewares ...MiddlewareHandler)

func (*Prefix) Post

func (p *Prefix) Post(uri string, handler RequestHandler, middlewares ...MiddlewareHandler)

func (*Prefix) Put

func (p *Prefix) Put(uri string, handler RequestHandler, middlewares ...MiddlewareHandler)

func (*Prefix) UseMiddleware

func (p *Prefix) UseMiddleware(middlewares ...MiddlewareHandler) IGroup

type ReqStruct

type ReqStruct struct {
	IRequest
	// contains filtered or unexported fields
}

func (*ReqStruct) BindXml

func (req *ReqStruct) BindXml(obj interface{}) error

xml body

func (*ReqStruct) ClientIp

func (req *ReqStruct) ClientIp() string

func (*ReqStruct) Cookie

func (req *ReqStruct) Cookie(key string) (string, bool)

func (*ReqStruct) Cookies

func (req *ReqStruct) Cookies() map[string]string

cookie

func (*ReqStruct) Form

func (req *ReqStruct) Form(key string) interface{}

func (*ReqStruct) FormAll

func (req *ReqStruct) FormAll() map[string][]string

func (*ReqStruct) FormBool

func (req *ReqStruct) FormBool(key string, defaultValue ...bool) (bool, bool)

func (*ReqStruct) FormFile

func (req *ReqStruct) FormFile(key string, limit ...int) (multipart.File, *multipart.FileHeader, url.Values, error)

limit[0] 单位:byte

func (*ReqStruct) FormFloat32

func (req *ReqStruct) FormFloat32(key string, defaultValue ...float32) (float32, bool)

func (*ReqStruct) FormFloat64

func (req *ReqStruct) FormFloat64(key string, defaultValue ...float64) (float64, bool)

func (*ReqStruct) FormInt

func (req *ReqStruct) FormInt(key string, defaultValue ...int) (int, bool)

func (*ReqStruct) FormInt64

func (req *ReqStruct) FormInt64(key string, defaultValue ...int64) (int64, bool)

func (*ReqStruct) FormString

func (req *ReqStruct) FormString(key string, defaultValue ...string) (string, bool)

func (*ReqStruct) FormStringSlice

func (req *ReqStruct) FormStringSlice(key string, defaultValue ...[]string) ([]string, bool)

func (*ReqStruct) Get

func (req *ReqStruct) Get(key string) interface{}

func (*ReqStruct) GetBool

func (req *ReqStruct) GetBool(key string, defaultValue ...bool) (bool, bool)

func (*ReqStruct) GetFloat32

func (req *ReqStruct) GetFloat32(key string, defaultValue ...float32) (float32, bool)

func (*ReqStruct) GetFloat64

func (req *ReqStruct) GetFloat64(key string, defaultValue ...float64) (float64, bool)

func (*ReqStruct) GetInt

func (req *ReqStruct) GetInt(key string, defaultValue ...int) int

func (*ReqStruct) GetInt64

func (req *ReqStruct) GetInt64(key string, defaultValue ...int64) (int64, bool)

func (*ReqStruct) GetRawData

func (req *ReqStruct) GetRawData() ([]byte, error)

其他格式

func (*ReqStruct) GetString

func (req *ReqStruct) GetString(key string, defaultValue ...string) string

func (*ReqStruct) GetStringSlice

func (req *ReqStruct) GetStringSlice(key string, defaultValue ...[]string) ([]string, bool)

请求 /xxx?a=11&a=22 中的参数 a 是能组成数组的

func (*ReqStruct) Header

func (req *ReqStruct) Header(key string) (string, bool)

func (*ReqStruct) Headers

func (req *ReqStruct) Headers() map[string][]string

header

func (*ReqStruct) Host

func (req *ReqStruct) Host() string

func (*ReqStruct) JsonScan added in v0.10.0

func (req *ReqStruct) JsonScan(s any) error

将body文本解析到 obj 结构体中 params := &paramsStruct{} ctx.Req.BindJson(params)

func (*ReqStruct) Method

func (req *ReqStruct) Method() string

func (*ReqStruct) PostBool added in v0.10.0

func (req *ReqStruct) PostBool(key string, defaultValue ...bool) (bool, bool)

func (*ReqStruct) PostFloat32 added in v0.10.0

func (req *ReqStruct) PostFloat32(key string, defaultValue ...float32) (float32, bool)

func (*ReqStruct) PostFloat64 added in v0.10.0

func (req *ReqStruct) PostFloat64(key string, defaultValue ...float64) (float64, bool)

func (*ReqStruct) PostInt added in v0.10.0

func (req *ReqStruct) PostInt(key string, defaultValue ...int) (int, bool)

func (*ReqStruct) PostInt32 added in v0.10.0

func (req *ReqStruct) PostInt32(key string, defaultValue ...int32) (int32, bool)

func (*ReqStruct) PostInt64 added in v0.10.0

func (req *ReqStruct) PostInt64(key string, defaultValue ...int64) (int64, bool)

func (*ReqStruct) PostString added in v0.10.0

func (req *ReqStruct) PostString(key string, defaultValue ...string) (string, bool)

func (*ReqStruct) QueryAll

func (req *ReqStruct) QueryAll() map[string][]string

获取请求地址中所有参数

func (*ReqStruct) Uri

func (req *ReqStruct) Uri() string

基础信息

type RequestHandler

type RequestHandler func(c *Context) // API / 控制器函数

type RespStruct

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

func (*RespStruct) Html

func (res *RespStruct) Html(file string, obj interface{}) IResponse

html输出

func (*RespStruct) Json

func (res *RespStruct) Json(obj interface{}) IResponse

func (*RespStruct) Jsonp

func (res *RespStruct) Jsonp(obj interface{}) IResponse

Jsonp输出

func (*RespStruct) Redirect

func (res *RespStruct) Redirect(path string) IResponse

重定向

func (*RespStruct) SetCookie

func (res *RespStruct) SetCookie(key string, val string, maxAge int, path string, domain string, secure bool, httpOnly bool) IResponse

Cookie

func (*RespStruct) SetHeader

func (res *RespStruct) SetHeader(key string, val string) IResponse

header

func (*RespStruct) SetOkStatus

func (res *RespStruct) SetOkStatus() IResponse

设置200状态

func (*RespStruct) SetStatus

func (res *RespStruct) SetStatus(code int) IResponse

设置状态码

func (*RespStruct) Text

func (res *RespStruct) Text(format string, values ...interface{}) IResponse

string

func (*RespStruct) Xml

func (res *RespStruct) Xml(obj interface{}) IResponse

xml输出

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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