v1.1.41 Latest Latest

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

Go to latest
Published: Apr 28, 2024 License: Apache-2.0 Imports: 25 Imported by: 0



Package http 提供基本的http服务



View Source
const (
	CacheControl  = "Cache-Control"
	XCacheControl = "X-" + CacheControl

http header


This section is empty.


func CheckRedirectError

func CheckRedirectError(err error) (url string, ok bool)

CheckRedirectError if err is RedirectError ,then return redirect error

func CheckRequestError

func CheckRequestError(err error) (status int, body []byte, ok bool)

CheckRequestError if err is RequestError,then return response status code and body

func ErrorFromRequestContext

func ErrorFromRequestContext(req *http.Request) (bool, error)

ErrorFromRequestContext 从req的context取得错误值

func FromRequestContext

func FromRequestContext(req *http.Request, key interface{}) interface{}

FromRequestContext 从req的context中取得key值

func GetFloat32Parameter

func GetFloat32Parameter(r url.Values, name string) (val float32, err error)

GetFloat32Parameter 取得由name指定的32位浮点数参数值

func GetFloat64Parameter

func GetFloat64Parameter(r url.Values, name string) (val float64, err error)

GetFloat64Parameter 取得由name指定的64位浮点数参数值

func GetInt32Parameter

func GetInt32Parameter(r url.Values, name string) (val int32, err error)

GetInt32Parameter 取得由name指定的32位整数参数值

func GetInt64Parameter

func GetInt64Parameter(r url.Values, name string) (val int64, err error)

GetInt64Parameter 取得由name指定的64位整数参数值

func GetParameter

func GetParameter(r url.Values, name string) string

GetParameter 取得由name指定的参数值

func GetURL

func GetURL(client *http.Client, url string, params url.Values) (string, error)


func GetURLRaw

func GetURLRaw(client *http.Client, url string, params url.Values, reqHeader http.Header, cookies map[string]string) (header http.Header, body []byte, err error)


func GetURLRawToWriter

func GetURLRawToWriter(client *http.Client, url string, params url.Values, reqHeader http.Header, cookies map[string]string, writer io.Writer) (header http.Header, err error)

GetURLRawToWriter 请求URL

func GetURLRawToWriterWithContext

func GetURLRawToWriterWithContext(ctx context.Context, client *http.Client, url string, params url.Values, reqHeader http.Header, cookies map[string]string, writer io.Writer) (header http.Header, err error)

GetURLRawToWriterWithContext 请求URL

func GetURLWithCookie

func GetURLWithCookie(client *http.Client, url string, params url.Values, cookies map[string]string) (string, error)

GetURLWithCookie 请求URL

func NewRedirectError

func NewRedirectError(req *http.Request, via []*http.Request) error

NewRedirectError redirect error

func ParseParams

func ParseParams(r url.Values, dest interface{}) error

ParseParams 从r中解析参数,并填充到dest中,params应该是struct指针

func Post added in v1.1.35

func Post(client *http.Client, url string, params url.Values, header map[string]string, contentType string, requestBody io.Reader, cookies map[string]string, prepare PostPrepare) ([]byte, http.Header, error)

Post Http Post

func PostURL

func PostURL(client *http.Client, url string, params url.Values, contentType string, requestBody io.Reader) ([]byte, http.Header, error)


func PostURLWithCookie

func PostURLWithCookie(client *http.Client, url string, params url.Values, contentType string, requestBody io.Reader, cookies map[string]string) ([]byte, http.Header, error)

PostURLWithCookie 请求URL

func PostURLWithCookieAndHeader

func PostURLWithCookieAndHeader(client *http.Client, url string, params url.Values, header map[string]string, contentType string, requestBody io.Reader, cookies map[string]string) ([]byte, http.Header, error)

PostURLWithCookieAndHeader 请求URL

func RegJSONMarshaler

func RegJSONMarshaler(marshaler JSONMarshaler)

RegJSONMarshaler 注册JSON Marshaler

func RenderHTML added in v1.1.31

func RenderHTML(w http.ResponseWriter, data []byte)


func RenderJSON

func RenderJSON(w http.ResponseWriter, jsonData interface{})


func RenderTemplate

func RenderTemplate(w http.ResponseWriter, templateDir, tmpl string, data interface{})

RenderTemplate 渲染模板

func RenderText

func RenderText(w http.ResponseWriter, text string)

RenderText 渲染Text

func RequestWithContext

func RequestWithContext(req *http.Request, key, val interface{}) *http.Request

RequestWithContext 向req的context中设置key = val,返回新的request

func RequestWithError

func RequestWithError(req *http.Request, err error) *http.Request

RequestWithError 向req中设置当前处理的错误,返回新的request

func ToUnderlineName

func ToUnderlineName(camelName string) string

ToUnderlineName 将驼峰命名改为小写的下划线命名


type AuthService

type AuthService interface {
	//AuthToken 使用token认证
	AuthToken(token string) (principal perm.Principal, err error)

AuthService 验证服务

type BaseController

type BaseController struct {
	Name               string                  // Controller的名称
	Path               string                  // Controller的路径
	HandlerMiddlewares map[string][]Middleware // Controller中需要使用middleware封装的方法

BaseController 表示一个控制器

func (*BaseController) GetHandlerMiddlewares

func (p *BaseController) GetHandlerMiddlewares() map[string][]Middleware

GetHandlerMiddlewares handler的middleware

func (*BaseController) GetName

func (p *BaseController) GetName() string

GetName controller的名称

func (*BaseController) GetPath

func (p *BaseController) GetPath() string

GetPath controller的path

type CheckPermMiddleware

type CheckPermMiddleware struct {
	PermService PermServcie `inject:"_,optional"` //认证服务
	// contains filtered or unexported fields

CheckPermMiddleware 用于检查用户的权限

func NewCheckPermMiddleware

func NewCheckPermMiddleware(useServcie bool) *CheckPermMiddleware

NewCheckPermMiddleware new

func (*CheckPermMiddleware) Handle

Handle 检查权限

type Config

type Config struct {
	Addr         string        //Http监听地址
	ReadTimeout  time.Duration //读超时,单位秒
	WriteTimeout time.Duration //写超时,单位秒
	MaxConns     int           //最大的并发连接数
	// contains filtered or unexported fields

Config Http配置

func NewConfig

func NewConfig(addr string) *Config

NewConfig 创建配置

func (*Config) InitWithInjector

func (p *Config) InitWithInjector(injector *inject.Injector) error

InitWithInjector 初始化操作

func (*Config) RegController

func (p *Config) RegController(controller Controller) error

RegController 注册controller中的所有处理函数

func (*Config) RegHandleFunc

func (p *Config) RegHandleFunc(patternPath string, handlerFunc http.HandlerFunc) error

RegHandleFunc 注册patternPath的处理函数handlerFunc

func (*Config) RegMiddleware

func (p *Config) RegMiddleware(middleware Middleware) error

RegMiddleware 注册middleware,middleware的注册需要在RegController和RegHandleFunc之前完成

func (*Config) RegStaticFunc

func (p *Config) RegStaticFunc(patternAndPath map[string]string) error

RegStaticFunc 注册静态资源patternPath的处理函数handlerFunc

type Controller

type Controller interface {
	// GetName 控制器的名称
	GetName() string
	// GetPath 路径前缀,以'/'结束,同一个控制下的http.Handler都
	GetPath() string
	// GetHandlerMiddlewares 返回controller的处理方法中,需要增加middleware封装的方法,key是controller中的方法名
	GetHandlerMiddlewares() map[string][]Middleware

Controller 接口定义http处理器

type GraceableHandler

type GraceableHandler struct {
	// contains filtered or unexported fields

GraceableHandler 安全地关闭的处理器

func (*GraceableHandler) ServeHTTP

func (p *GraceableHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type JSONMarshaler

type JSONMarshaler interface {
	Marshal(interface{}) ([]byte, error)

JSONMarshaler json marshaler

type Middleware

type Middleware interface {
	// Handle处理
	Handle(next MiddlewareFunc) MiddlewareFunc

Middleware 接口

func Merge

func Merge(middlewares ...[]Middleware) []Middleware

Merge 合并Middleware

type MiddlewareFunc

type MiddlewareFunc func(http.ResponseWriter, *http.Request)

MiddlewareFunc middleware函数

type NoDirFS

type NoDirFS struct {
	Fs http.FileSystem

NoDirFS 不输出目录列表的FS

func (NoDirFS) Open

func (fs NoDirFS) Open(name string) (http.File, error)

Open 取得指定的文件,如果name指向的是个目录,且目录下没有index.html,返回os.ErrPermission

type PermBindMiddleware

type PermBindMiddleware struct {
	ReqPerm []*perm.Perm

PermBindMiddleware 用于给controller中的handler方法注入需要的权限

func NewPermBindMiddleware

func NewPermBindMiddleware(perms ...*perm.Perm) *PermBindMiddleware

NewPermBindMiddleware 用perms构建middleware

func (*PermBindMiddleware) Handle

Handle 绑定权限到ctx

type PermServcie

type PermServcie interface {
	//HasPerm 坚持prinpical是否有需要的权限
	HasPerm(principal perm.Principal, perms []*perm.Perm) (hasPerm bool, err error)

PermServcie 权限服务

type PostPrepare added in v1.1.35

type PostPrepare func(req *http.Request, body io.ReadSeeker) (err error)

PostPrepare prepare the post request before call client.Do(req)

type RedirectError

type RedirectError struct {
	RedirectURL string

RedirectError redirect error

func (*RedirectError) Error

func (p *RedirectError) Error() string

type RequestError

type RequestError struct {
	Status int
	Err    error
	Body   []byte

RequestError is the http request response error

func (*RequestError) Error

func (p *RequestError) Error() string

type RequestMehotdMiddleware

type RequestMehotdMiddleware struct {
	AllowsMethods map[string]struct{}

RequestMehotdMiddleware http请求方法

func NewRequestMetodMiddleware

func NewRequestMetodMiddleware(methods ...string) *RequestMehotdMiddleware

NewRequestMetodMiddleware 用methods构建middleware

func (*RequestMehotdMiddleware) Handle

Handle 校验Http请求的方法

type Resp

type Resp struct {
	Success bool        `json:"success"`
	Data    interface{} `json:"data"`
	Msg     string      `json:"msg"`
	Code    int32       `json:"code"`
	ExtData any         `json:"ext_data,omitempty"`

Resp JSON Http响应

func NewFailResp

func NewFailResp(msg string) *Resp

NewFailResp 构建失败的响应

func NewSuccResp

func NewSuccResp(data interface{}) *Resp

NewSuccResp 构建成功的响应

type ResponseHandler

type ResponseHandler struct {
	Success bool
	Data    interface{}
	Msg     string
	Cancel  bool
	Code    int32
	ExtData any
	// contains filtered or unexported fields

ResponseHandler 响应处理

func NewResponseHandler

func NewResponseHandler(w http.ResponseWriter) *ResponseHandler

NewResponseHandler 构建响应处理

func (*ResponseHandler) FailWithMsg

func (p *ResponseHandler) FailWithMsg(msg string)

FailWithMsg 设置失败及出错的消息

func (*ResponseHandler) Run

func (p *ResponseHandler) Run()

Run exec run

func (*ResponseHandler) SuccessWith added in v1.1.5

func (p *ResponseHandler) SuccessWith(data interface{}, msg string)

SuccessWith 设置成功,数据,消息

func (*ResponseHandler) SuccessWithData

func (p *ResponseHandler) SuccessWithData(data interface{})

SuccessWithData 设置成功及数据

type RetrivedCookieJar

type RetrivedCookieJar struct {
	// contains filtered or unexported fields

RetrivedCookieJar 可持久化的Cookie

func NewRetrivedCookieJar

func NewRetrivedCookieJar(o *cookiejar.Options) *RetrivedCookieJar

NewRetrivedCookieJar 构建PersistCookieJar

func (*RetrivedCookieJar) Cookies

func (p *RetrivedCookieJar) Cookies(u *url.URL) []*http.Cookie

Cookies implements CookeJar.Cookies

func (*RetrivedCookieJar) SetCookies

func (p *RetrivedCookieJar) SetCookies(u *url.URL, cookies []*http.Cookie)

SetCookies implements CookieJar.SetCookies

func (*RetrivedCookieJar) SetURLAndCookies

func (p *RetrivedCookieJar) SetURLAndCookies(all map[string][]*http.Cookie) error

SetURLAndCookies 设置所有的URL和Cookie

func (*RetrivedCookieJar) URLAndCookies

func (p *RetrivedCookieJar) URLAndCookies() map[string][]*http.Cookie

URLAndCookies 取得所有的URL和Cookie

type Service

type Service struct {
	Conf *Config
	// contains filtered or unexported fields

Service Http服务

func (*Service) Init

func (p *Service) Init() bool

Init 初始化Http服务

func (*Service) Start

func (p *Service) Start() bool

Start 启动Http服务,开始端口监听和服务处理

func (*Service) Stop

func (p *Service) Stop() bool

Stop 停止Http服务,关闭端口监听和服务处理

type TokenMiddleware

type TokenMiddleware struct {
	AuthService AuthService `inject:"_"`
	TokenName string
	TokenHeaderName string

TokenMiddleware 用于从cookie中解析token,从中取得请求的principal

func (*TokenMiddleware) Handle

Handle 解析token

Jump to

Keyboard shortcuts

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