mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-19 01:04:25 +08:00
add comments
This commit is contained in:
@@ -12,15 +12,18 @@ const requestIdFieldKey = "REQUEST_ID"
|
|||||||
const tokenClaimsFieldKey = "TOKEN_CLAIMS"
|
const tokenClaimsFieldKey = "TOKEN_CLAIMS"
|
||||||
const responseErrorFieldKey = "RESPONSE_ERROR"
|
const responseErrorFieldKey = "RESPONSE_ERROR"
|
||||||
|
|
||||||
|
// Context represents the request and response context
|
||||||
type Context struct {
|
type Context struct {
|
||||||
*gin.Context
|
*gin.Context
|
||||||
// DO NOT ADD ANY FIELD IN THIS CONTEXT, THIS CONTEXT IS JUST A WRAPPER
|
// DO NOT ADD ANY FIELD IN THIS CONTEXT, THIS CONTEXT IS JUST A WRAPPER
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetRequestId sets the given request id to context
|
||||||
func (c *Context) SetRequestId(requestId string) {
|
func (c *Context) SetRequestId(requestId string) {
|
||||||
c.Set(requestIdFieldKey, requestId)
|
c.Set(requestIdFieldKey, requestId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetRequestId returns the current request id
|
||||||
func (c *Context) GetRequestId() string {
|
func (c *Context) GetRequestId() string {
|
||||||
requestId, exists := c.Get(requestIdFieldKey)
|
requestId, exists := c.Get(requestIdFieldKey)
|
||||||
|
|
||||||
@@ -31,10 +34,12 @@ func (c *Context) GetRequestId() string {
|
|||||||
return requestId.(string)
|
return requestId.(string)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetTokenClaims sets the given user token id to context
|
||||||
func (c *Context) SetTokenClaims(claims *UserTokenClaims) {
|
func (c *Context) SetTokenClaims(claims *UserTokenClaims) {
|
||||||
c.Set(tokenClaimsFieldKey, claims)
|
c.Set(tokenClaimsFieldKey, claims)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetTokenClaims returns the current user token
|
||||||
func (c *Context) GetTokenClaims() *UserTokenClaims {
|
func (c *Context) GetTokenClaims() *UserTokenClaims {
|
||||||
claims, exists := c.Get(tokenClaimsFieldKey)
|
claims, exists := c.Get(tokenClaimsFieldKey)
|
||||||
|
|
||||||
@@ -45,6 +50,7 @@ func (c *Context) GetTokenClaims() *UserTokenClaims {
|
|||||||
return claims.(*UserTokenClaims)
|
return claims.(*UserTokenClaims)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetCurrentUid returns the current user uid by the current user token
|
||||||
func (c *Context) GetCurrentUid() int64 {
|
func (c *Context) GetCurrentUid() int64 {
|
||||||
claims := c.GetTokenClaims()
|
claims := c.GetTokenClaims()
|
||||||
|
|
||||||
@@ -61,10 +67,12 @@ func (c *Context) GetCurrentUid() int64 {
|
|||||||
return uid
|
return uid
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetResponseError sets the response error
|
||||||
func (c *Context) SetResponseError(error *errs.Error) {
|
func (c *Context) SetResponseError(error *errs.Error) {
|
||||||
c.Set(responseErrorFieldKey, error)
|
c.Set(responseErrorFieldKey, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetResponseError returns the response error
|
||||||
func (c *Context) GetResponseError() *errs.Error {
|
func (c *Context) GetResponseError() *errs.Error {
|
||||||
err, exists := c.Get(responseErrorFieldKey)
|
err, exists := c.Get(responseErrorFieldKey)
|
||||||
|
|
||||||
@@ -75,6 +83,7 @@ func (c *Context) GetResponseError() *errs.Error {
|
|||||||
return err.(*errs.Error)
|
return err.(*errs.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WrapContext returns a context wrapped by this file
|
||||||
func WrapContext(ginCtx *gin.Context) *Context {
|
func WrapContext(ginCtx *gin.Context) *Context {
|
||||||
return &Context{
|
return &Context{
|
||||||
Context: ginCtx,
|
Context: ginCtx,
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package core
|
|||||||
|
|
||||||
import "github.com/mayswind/lab/pkg/errs"
|
import "github.com/mayswind/lab/pkg/errs"
|
||||||
|
|
||||||
|
// MiddlewareHandlerFunc represents the middleware handler function
|
||||||
type MiddlewareHandlerFunc func(*Context)
|
type MiddlewareHandlerFunc func(*Context)
|
||||||
|
|
||||||
|
// ApiHandlerFunc represents the api handler function
|
||||||
type ApiHandlerFunc func(*Context) (interface{}, *errs.Error)
|
type ApiHandlerFunc func(*Context) (interface{}, *errs.Error)
|
||||||
|
|||||||
@@ -4,13 +4,16 @@ import (
|
|||||||
"github.com/dgrijalva/jwt-go"
|
"github.com/dgrijalva/jwt-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// TokenType represents token type
|
||||||
type TokenType byte
|
type TokenType byte
|
||||||
|
|
||||||
|
// Token types
|
||||||
const (
|
const (
|
||||||
USER_TOKEN_TYPE_NORMAL TokenType = 1
|
USER_TOKEN_TYPE_NORMAL TokenType = 1
|
||||||
USER_TOKEN_TYPE_REQUIRE_2FA TokenType = 2
|
USER_TOKEN_TYPE_REQUIRE_2FA TokenType = 2
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// UserTokenClaims represents user token
|
||||||
type UserTokenClaims struct {
|
type UserTokenClaims struct {
|
||||||
UserTokenId string `json:"userTokenId"`
|
UserTokenId string `json:"userTokenId"`
|
||||||
Username string `json:"username,omitempty"`
|
Username string `json:"username,omitempty"`
|
||||||
|
|||||||
Reference in New Issue
Block a user