web

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2023 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cors

func Cors() gin.HandlerFunc

Types

type Context

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

func (*Context) Bad

func (c *Context) Bad(code int, msg string) ICustomResp

Bad http状态码为400的错误返回,可以指定code和msg

http status: 400

example:

{
  "code": 1001,
  "msg": "用户名或密码错误"
}

func (*Context) BadCode

func (c *Context) BadCode(err errorx.ErrorCode) ICustomResp

BadCode http状态码为400的错误返回,自定义code

http status: 400

example:

{
  "code": 1001,
  "msg": "用户名或密码错误"
}

func (*Context) BadError

func (c *Context) BadError(err error) ICustomResp

BadError http状态码为400的错误返回,code=400

http status: 400

example:

{
  "code": 400,
  "msg": "用户名或密码错误"
}

func (*Context) Bind

func (c *Context) Bind(v any) error

func (*Context) BindForm

func (c *Context) BindForm(v any) error

func (*Context) BindHeader

func (c *Context) BindHeader(v any) error

func (*Context) BindJson

func (c *Context) BindJson(v any) error

func (*Context) BindJsonpb

func (c *Context) BindJsonpb(v proto.Message) error

BindJsonpb 绑定数据到protobuf struct

func (*Context) BindQuery

func (c *Context) BindQuery(v any) error

func (*Context) BindUri

func (c *Context) BindUri(v any) error

func (*Context) Biz

func (c *Context) Biz(code int, msg string, data any) ICustomResp

Biz 业务数据,可以指定code和msg

http status: 200

example:

{
  "code": 0,
  "msg": "success",
  "data": {
    "id": 1,
    "name": "张三"
  }
}

func (*Context) BizBad

func (c *Context) BizBad(code int, msg string) ICustomResp

BizBad 业务错误,可以指定code和msg

http status: 200

example:

{
  "code": 1001,
  "msg": "用户名或密码错误"
}

func (*Context) BizBadCode

func (c *Context) BizBadCode(err errorx.ErrorCode) ICustomResp

BizBadCode 业务错误,自定义错误码

http status: 200

example:

{
  "code": 1001,
  "msg": "用户名或密码错误"
}

func (*Context) BizBadError

func (c *Context) BizBadError(err error) ICustomResp

BizBadError 业务错误,code=400

http status: 200

example:

{
  "code": 400,
  "msg": "用户名或密码错误"
}

func (*Context) BizData

func (c *Context) BizData(data any) ICustomResp

BizData 业务数据

http status: 200

example:

{
  "code": 0,
  "msg": "success",
  "data": {
    "id": 1,
    "name": "张三"
  }
}

func (*Context) Context

func (c *Context) Context() context.Context

func (*Context) Data

func (c *Context) Data(data any) ICustomResp

Data http状态码为200的数据返回

http status: 200

example:

{
  "id": 1,
  "name": "张三"
}

func (*Context) Forbidden

func (c *Context) Forbidden() ICustomResp

Forbidden 禁止访问

http status: 403

example:

{
  "code": 403,
  "msg": "禁止访问"
}

func (*Context) Get

func (c *Context) Get(key string) (value any, exists bool)

func (*Context) GetReqHeader

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

GetReqHeader get header

func (*Context) GetRespHeader

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

GetRespHeader get header

func (*Context) Set

func (c *Context) Set(key string, value any)

func (*Context) SetReqHeader

func (c *Context) SetReqHeader(key string, value string)

SetReqHeader set header

func (*Context) SetRespHeader

func (c *Context) SetRespHeader(key string, value string)

SetRespHeader set header

func (*Context) UnAuth

func (c *Context) UnAuth() ICustomResp

type ICustomResp

type ICustomResp interface {
	Status() int
	GetData() any
}

type IRoute

type IRoute interface {
	Routes(ctx *RouteContext)
}

type RouteContext

type RouteContext struct {
	*gin.Engine
	Auth gin.IRoutes
}

func (*RouteContext) Handle

func (r *RouteContext) Handle(fn func(ctx *Context) any) gin.HandlerFunc

type Server

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

func NewServer

func NewServer(opts ...ServerOption) (*Server, error)

NewServer 创建一个新的Server,默认debug模式

func (*Server) GIN

func (s *Server) GIN() *gin.Engine

func (*Server) Host

func (s *Server) Host() string

func (*Server) Run

func (s *Server) Run()

Run 运行Server

type ServerOption

type ServerOption func(*Server)

ServerOption Server Option type

func WithAuthMiddleware

func WithAuthMiddleware(authMiddleware gin.HandlerFunc) ServerOption

WithAuthMiddleware 设置认证中间件

func WithRoutes

func WithRoutes(routes ...IRoute) ServerOption

WithRoutes 设置路由

func WithServerHost

func WithServerHost(host string) ServerOption

WithServerHost 设置host

Jump to

Keyboard shortcuts

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