add comments

This commit is contained in:
MaysWind
2020-12-26 00:02:55 +08:00
parent c9190e2427
commit 864ef7c17c
3 changed files with 14 additions and 0 deletions
+9
View File
@@ -12,15 +12,18 @@ const requestIdFieldKey = "REQUEST_ID"
const tokenClaimsFieldKey = "TOKEN_CLAIMS"
const responseErrorFieldKey = "RESPONSE_ERROR"
// Context represents the request and response context
type Context struct {
*gin.Context
// 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) {
c.Set(requestIdFieldKey, requestId)
}
// GetRequestId returns the current request id
func (c *Context) GetRequestId() string {
requestId, exists := c.Get(requestIdFieldKey)
@@ -31,10 +34,12 @@ func (c *Context) GetRequestId() string {
return requestId.(string)
}
// SetTokenClaims sets the given user token id to context
func (c *Context) SetTokenClaims(claims *UserTokenClaims) {
c.Set(tokenClaimsFieldKey, claims)
}
// GetTokenClaims returns the current user token
func (c *Context) GetTokenClaims() *UserTokenClaims {
claims, exists := c.Get(tokenClaimsFieldKey)
@@ -45,6 +50,7 @@ func (c *Context) GetTokenClaims() *UserTokenClaims {
return claims.(*UserTokenClaims)
}
// GetCurrentUid returns the current user uid by the current user token
func (c *Context) GetCurrentUid() int64 {
claims := c.GetTokenClaims()
@@ -61,10 +67,12 @@ func (c *Context) GetCurrentUid() int64 {
return uid
}
// SetResponseError sets the response error
func (c *Context) SetResponseError(error *errs.Error) {
c.Set(responseErrorFieldKey, error)
}
// GetResponseError returns the response error
func (c *Context) GetResponseError() *errs.Error {
err, exists := c.Get(responseErrorFieldKey)
@@ -75,6 +83,7 @@ func (c *Context) GetResponseError() *errs.Error {
return err.(*errs.Error)
}
// WrapContext returns a context wrapped by this file
func WrapContext(ginCtx *gin.Context) *Context {
return &Context{
Context: ginCtx,
+2
View File
@@ -2,6 +2,8 @@ package core
import "github.com/mayswind/lab/pkg/errs"
// MiddlewareHandlerFunc represents the middleware handler function
type MiddlewareHandlerFunc func(*Context)
// ApiHandlerFunc represents the api handler function
type ApiHandlerFunc func(*Context) (interface{}, *errs.Error)
+3
View File
@@ -4,13 +4,16 @@ import (
"github.com/dgrijalva/jwt-go"
)
// TokenType represents token type
type TokenType byte
// Token types
const (
USER_TOKEN_TYPE_NORMAL TokenType = 1
USER_TOKEN_TYPE_REQUIRE_2FA TokenType = 2
)
// UserTokenClaims represents user token
type UserTokenClaims struct {
UserTokenId string `json:"userTokenId"`
Username string `json:"username,omitempty"`