mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-14 06:57:35 +08:00
code refactor
This commit is contained in:
+3
-158
@@ -1,161 +1,6 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/mayswind/ezbookkeeping/pkg/errs"
|
||||
)
|
||||
|
||||
const requestIdFieldKey = "REQUEST_ID"
|
||||
const textualTokenFieldKey = "TOKEN_STRING"
|
||||
const tokenClaimsFieldKey = "TOKEN_CLAIMS"
|
||||
const responseErrorFieldKey = "RESPONSE_ERROR"
|
||||
|
||||
// AcceptLanguageHeaderName represents the header name of accept language
|
||||
const AcceptLanguageHeaderName = "Accept-Language"
|
||||
|
||||
// RemoteClientPortHeader represents the header name of remote client source port
|
||||
const RemoteClientPortHeader = "X-Real-Port"
|
||||
|
||||
// ClientTimezoneOffsetHeaderName represents the header name of client timezone offset
|
||||
const ClientTimezoneOffsetHeaderName = "X-Timezone-Offset"
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
func (c *Context) ClientPort() uint16 {
|
||||
remotePort := c.GetHeader(RemoteClientPortHeader)
|
||||
|
||||
if remotePort != "" {
|
||||
remotePortNum, err := strconv.ParseInt(remotePort, 10, 32)
|
||||
|
||||
if err == nil {
|
||||
return uint16(remotePortNum)
|
||||
}
|
||||
}
|
||||
|
||||
if c.Request == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
_, remotePort, err := net.SplitHostPort(c.Request.RemoteAddr)
|
||||
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
remotePortNum, err := strconv.ParseInt(remotePort, 10, 32)
|
||||
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
return uint16(remotePortNum)
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
||||
if !exists {
|
||||
return ""
|
||||
}
|
||||
|
||||
return requestId.(string)
|
||||
}
|
||||
|
||||
// SetTextualToken sets the given user token to context
|
||||
func (c *Context) SetTextualToken(token string) {
|
||||
c.Set(textualTokenFieldKey, token)
|
||||
}
|
||||
|
||||
// GetTextualToken returns the current user textual token
|
||||
func (c *Context) GetTextualToken() string {
|
||||
token, exists := c.Get(textualTokenFieldKey)
|
||||
|
||||
if !exists {
|
||||
return ""
|
||||
}
|
||||
|
||||
return token.(string)
|
||||
}
|
||||
|
||||
// SetTokenClaims sets the given user token 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)
|
||||
|
||||
if !exists {
|
||||
return nil
|
||||
}
|
||||
|
||||
return claims.(*UserTokenClaims)
|
||||
}
|
||||
|
||||
// GetCurrentUid returns the current user uid by the current user token
|
||||
func (c *Context) GetCurrentUid() int64 {
|
||||
claims := c.GetTokenClaims()
|
||||
|
||||
if claims == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
return claims.Uid
|
||||
}
|
||||
|
||||
// GetClientLocale returns the client locale name
|
||||
func (c *Context) GetClientLocale() string {
|
||||
value := c.GetHeader(AcceptLanguageHeaderName)
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
// GetClientTimezoneOffset returns the client timezone offset
|
||||
func (c *Context) GetClientTimezoneOffset() (int16, error) {
|
||||
value := c.GetHeader(ClientTimezoneOffsetHeaderName)
|
||||
offset, err := strconv.Atoi(value)
|
||||
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return int16(offset), nil
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
||||
if !exists {
|
||||
return nil
|
||||
}
|
||||
|
||||
return err.(*errs.Error)
|
||||
}
|
||||
|
||||
// WrapContext returns a context wrapped by this file
|
||||
func WrapContext(ginCtx *gin.Context) *Context {
|
||||
return &Context{
|
||||
Context: ginCtx,
|
||||
}
|
||||
// Context is the base context of ezBookkeeping
|
||||
type Context interface {
|
||||
GetContextId() string
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
// CliContext represents the command-line context
|
||||
type CliContext struct {
|
||||
*cli.Context
|
||||
}
|
||||
|
||||
// GetContextId returns the current context id
|
||||
func (c *CliContext) GetContextId() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// WrapCliContext returns a context wrapped by this file
|
||||
func WrapCilContext(cliCtx *cli.Context) *CliContext {
|
||||
return &CliContext{
|
||||
Context: cliCtx,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// CronContext represents the cron job context
|
||||
type CronContext struct {
|
||||
context.Context
|
||||
contextId string
|
||||
}
|
||||
|
||||
// GetContextId returns the current context id
|
||||
func (c *CronContext) GetContextId() string {
|
||||
return c.contextId
|
||||
}
|
||||
|
||||
// NewCronJobContext returns a new cron job context
|
||||
func NewCronJobContext(cronJobName string) *CronContext {
|
||||
return &CronContext{
|
||||
Context: context.Background(),
|
||||
contextId: generateNewRandomCronContextId(cronJobName),
|
||||
}
|
||||
}
|
||||
|
||||
func generateNewRandomCronContextId(cronJobName string) string {
|
||||
var ret strings.Builder
|
||||
ret.WriteString("cron-job-")
|
||||
ret.WriteString(strings.ToLower(cronJobName))
|
||||
ret.WriteRune('-')
|
||||
ret.WriteString(strconv.FormatInt(time.Now().Unix(), 10))
|
||||
|
||||
return ret.String()
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package core
|
||||
|
||||
import "context"
|
||||
|
||||
const nullContextId = "00000000-0000-0000-0000-00000000"
|
||||
|
||||
// NullContext represents the null context
|
||||
type NullContext struct {
|
||||
context.Context
|
||||
}
|
||||
|
||||
// GetContextId returns the current context id
|
||||
func (c *NullContext) GetContextId() string {
|
||||
return nullContextId
|
||||
}
|
||||
|
||||
// NewCronJobContext returns a new null context
|
||||
func NewNullContext() *NullContext {
|
||||
return &NullContext{
|
||||
Context: context.Background(),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/mayswind/ezbookkeeping/pkg/errs"
|
||||
)
|
||||
|
||||
const webContextRequestIdFieldKey = "REQUEST_ID"
|
||||
const webContextTextualTokenFieldKey = "TOKEN_STRING"
|
||||
const webContextTokenClaimsFieldKey = "TOKEN_CLAIMS"
|
||||
const webContextResponseErrorFieldKey = "RESPONSE_ERROR"
|
||||
|
||||
// AcceptLanguageHeaderName represents the header name of accept language
|
||||
const AcceptLanguageHeaderName = "Accept-Language"
|
||||
|
||||
// RemoteClientPortHeader represents the header name of remote client source port
|
||||
const RemoteClientPortHeader = "X-Real-Port"
|
||||
|
||||
// ClientTimezoneOffsetHeaderName represents the header name of client timezone offset
|
||||
const ClientTimezoneOffsetHeaderName = "X-Timezone-Offset"
|
||||
|
||||
// WebContext represents the request and response context
|
||||
type WebContext struct {
|
||||
*gin.Context
|
||||
// DO NOT ADD ANY FIELD IN THIS CONTEXT, THIS CONTEXT IS JUST A WRAPPER
|
||||
}
|
||||
|
||||
func (c *WebContext) ClientPort() uint16 {
|
||||
remotePort := c.GetHeader(RemoteClientPortHeader)
|
||||
|
||||
if remotePort != "" {
|
||||
remotePortNum, err := strconv.ParseInt(remotePort, 10, 32)
|
||||
|
||||
if err == nil {
|
||||
return uint16(remotePortNum)
|
||||
}
|
||||
}
|
||||
|
||||
if c.Request == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
_, remotePort, err := net.SplitHostPort(c.Request.RemoteAddr)
|
||||
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
remotePortNum, err := strconv.ParseInt(remotePort, 10, 32)
|
||||
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
return uint16(remotePortNum)
|
||||
}
|
||||
|
||||
// SetContextId sets the given request id to context
|
||||
func (c *WebContext) SetContextId(requestId string) {
|
||||
c.Set(webContextRequestIdFieldKey, requestId)
|
||||
}
|
||||
|
||||
// GetContextId returns the current request id
|
||||
func (c *WebContext) GetContextId() string {
|
||||
requestId, exists := c.Get(webContextRequestIdFieldKey)
|
||||
|
||||
if !exists {
|
||||
return ""
|
||||
}
|
||||
|
||||
return requestId.(string)
|
||||
}
|
||||
|
||||
// SetTextualToken sets the given user token to context
|
||||
func (c *WebContext) SetTextualToken(token string) {
|
||||
c.Set(webContextTextualTokenFieldKey, token)
|
||||
}
|
||||
|
||||
// GetTextualToken returns the current user textual token
|
||||
func (c *WebContext) GetTextualToken() string {
|
||||
token, exists := c.Get(webContextTextualTokenFieldKey)
|
||||
|
||||
if !exists {
|
||||
return ""
|
||||
}
|
||||
|
||||
return token.(string)
|
||||
}
|
||||
|
||||
// SetTokenClaims sets the given user token to context
|
||||
func (c *WebContext) SetTokenClaims(claims *UserTokenClaims) {
|
||||
c.Set(webContextTokenClaimsFieldKey, claims)
|
||||
}
|
||||
|
||||
// GetTokenClaims returns the current user token
|
||||
func (c *WebContext) GetTokenClaims() *UserTokenClaims {
|
||||
claims, exists := c.Get(webContextTokenClaimsFieldKey)
|
||||
|
||||
if !exists {
|
||||
return nil
|
||||
}
|
||||
|
||||
return claims.(*UserTokenClaims)
|
||||
}
|
||||
|
||||
// GetCurrentUid returns the current user uid by the current user token
|
||||
func (c *WebContext) GetCurrentUid() int64 {
|
||||
claims := c.GetTokenClaims()
|
||||
|
||||
if claims == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
return claims.Uid
|
||||
}
|
||||
|
||||
// GetClientLocale returns the client locale name
|
||||
func (c *WebContext) GetClientLocale() string {
|
||||
value := c.GetHeader(AcceptLanguageHeaderName)
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
// GetClientTimezoneOffset returns the client timezone offset
|
||||
func (c *WebContext) GetClientTimezoneOffset() (int16, error) {
|
||||
value := c.GetHeader(ClientTimezoneOffsetHeaderName)
|
||||
offset, err := strconv.Atoi(value)
|
||||
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return int16(offset), nil
|
||||
}
|
||||
|
||||
// SetResponseError sets the response error
|
||||
func (c *WebContext) SetResponseError(error *errs.Error) {
|
||||
c.Set(webContextResponseErrorFieldKey, error)
|
||||
}
|
||||
|
||||
// GetResponseError returns the response error
|
||||
func (c *WebContext) GetResponseError() *errs.Error {
|
||||
err, exists := c.Get(webContextResponseErrorFieldKey)
|
||||
|
||||
if !exists {
|
||||
return nil
|
||||
}
|
||||
|
||||
return err.(*errs.Error)
|
||||
}
|
||||
|
||||
// WrapWebContext returns a context wrapped by this file
|
||||
func WrapWebContext(ginCtx *gin.Context) *WebContext {
|
||||
return &WebContext{
|
||||
Context: ginCtx,
|
||||
}
|
||||
}
|
||||
+8
-5
@@ -6,17 +6,20 @@ import (
|
||||
"github.com/mayswind/ezbookkeeping/pkg/errs"
|
||||
)
|
||||
|
||||
// CliHandlerFunc represents the cli handler function
|
||||
type CliHandlerFunc func(*CliContext) error
|
||||
|
||||
// MiddlewareHandlerFunc represents the middleware handler function
|
||||
type MiddlewareHandlerFunc func(*Context)
|
||||
type MiddlewareHandlerFunc func(*WebContext)
|
||||
|
||||
// ApiHandlerFunc represents the api handler function
|
||||
type ApiHandlerFunc func(*Context) (any, *errs.Error)
|
||||
type ApiHandlerFunc func(*WebContext) (any, *errs.Error)
|
||||
|
||||
// DataHandlerFunc represents the handler function that returns file data byte array and file name
|
||||
type DataHandlerFunc func(*Context) ([]byte, string, *errs.Error)
|
||||
type DataHandlerFunc func(*WebContext) ([]byte, string, *errs.Error)
|
||||
|
||||
// ImageHandlerFunc represents the handler function that returns image byte array and content type
|
||||
type ImageHandlerFunc func(*Context) ([]byte, string, *errs.Error)
|
||||
type ImageHandlerFunc func(*WebContext) ([]byte, string, *errs.Error)
|
||||
|
||||
// ProxyHandlerFunc represents the reverse proxy handler function
|
||||
type ProxyHandlerFunc func(*Context) (*httputil.ReverseProxy, *errs.Error)
|
||||
type ProxyHandlerFunc func(*WebContext) (*httputil.ReverseProxy, *errs.Error)
|
||||
|
||||
Reference in New Issue
Block a user