utils

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2020 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// The key that attached the SessionUser in the gin Context.
	SessionUserKey = "user"
)

Variables

View Source
var (
	ErrNS                    = errorx.NewNamespace("error.api")
	ErrOther                 = ErrNS.NewType("other")
	ErrUnauthorized          = ErrNS.NewType("unauthorized")
	ErrInsufficientPrivilege = ErrNS.NewType("insufficient_privilege")
	ErrInvalidRequest        = ErrNS.NewType("invalid_request")
)

Functions

func GetTiDBConnection

func GetTiDBConnection(c *gin.Context) *gorm.DB

GetTiDBConnection gets the TiDB connection stored in the gin context by `MWConnectTiDB` middleware.

The connection will be closed automatically after all handlers are finished. Thus you must not use it outside the request lifetime. If you want to extend the lifetime, use `TakeTiDBConnection`.

func MWConnectTiDB

func MWConnectTiDB(tidbForwarder *tidb.Forwarder) gin.HandlerFunc

MWConnectTiDB creates a middleware that attaches TiDB connection to the context, according to the identity information attached in the context. If a connection cannot be established, subsequent handlers will be skipped and errors will be generated.

This middleware must be placed after the `MWAuthRequired()` middleware, otherwise it will panic.

func MWHandleErrors

func MWHandleErrors() gin.HandlerFunc

MWHandleErrors creates a middleware that turns (last) error in the context into an APIError json response. In handlers, `c.Error(err)` can be used to attach the error to the context. When error is attached in the context: - The handler can optionally assign the HTTP status code. - The handler must not self-generate a response body.

func MakeInsufficientPrivilegeError

func MakeInsufficientPrivilegeError(c *gin.Context)

func MakeUnauthorizedError

func MakeUnauthorizedError(c *gin.Context)

func NewJWTString

func NewJWTString(issuer string, data string) (string, error)

NewJWTString create a JWT string by given data

func ParseJWTString

func ParseJWTString(requiredIssuer string, tokenStr string) (string, error)

ParseJWTString parse the JWT string and return the raw data

func TakeTiDBConnection

func TakeTiDBConnection(c *gin.Context) *gorm.DB

TakeTiDBConnection takes out the TiDB connection stored in the gin context by `MWConnectTiDB` middleware. Subsequent handlers in this context cannot access the TiDB connection any more.

The TiDB connection will no longer be closed automatically after all handlers are finished. You must manually close the taken out connection.

Types

type APIEmptyResponse

type APIEmptyResponse struct{}

type APIError

type APIError struct {
	Error    bool   `json:"error"`
	Message  string `json:"message"`
	Code     string `json:"code"`
	FullText string `json:"full_text"`
}

type Claims

type Claims struct {
	Data string `json:"data"`
	jwt.StandardClaims
}

Claims is a struct that will be encoded to a JWT.

type NodeKind

type NodeKind string
const (
	NodeKindTiDB NodeKind = "tidb"
	NodeKindTiKV NodeKind = "tikv"
	NodeKindPD   NodeKind = "pd"
)

type RequestTargetNode

type RequestTargetNode struct {
	Kind        NodeKind `json:"kind" gorm:"size:8" example:"tidb"`
	DisplayName string   `json:"display_name" gorm:"size:32" example:"127.0.0.1:4000"`
	IP          string   `json:"ip" gorm:"size:32" example:"127.0.0.1"`
	Port        int      `json:"port" example:"4000"`
}

func (*RequestTargetNode) FileName

func (n *RequestTargetNode) FileName() string

func (*RequestTargetNode) String

func (n *RequestTargetNode) String() string

type RequestTargetStatistics

type RequestTargetStatistics struct {
	NumTiKVNodes int `json:"num_tikv_nodes"`
	NumTiDBNodes int `json:"num_tidb_nodes"`
	NumPDNodes   int `json:"num_pd_nodes"`
}

func NewRequestTargetStatisticsFromArray

func NewRequestTargetStatisticsFromArray(arr *[]RequestTargetNode) RequestTargetStatistics

type SessionUser

type SessionUser struct {
	IsTiDBAuth   bool
	TiDBUsername string
	TiDBPassword string
}

Jump to

Keyboard shortcuts

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