middleware

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CrossDomain = func(ctx *context.Context) {
	ctx.Header("Access-Control-Allow-Origin", ctx.Request().Header.Get("Origin"))
	ctx.Header("Access-Control-Allow-Headers", "Content-Type,X-Request-Id,X-Request-Some")
	ctx.Header("Access-Control-Allow-Methods", "GET,POST,OPTIONS,PUT,DELETE,PATCH,HEAD,TRACE")
	ctx.Header("Access-Control-Allow-Credentials", "true")
	ctx.Header("Access-Control-Max-Age", "7200")
	if ctx.Request().Method == "OPTIONS" {
		ctx.StatusCode(200)
	} else {
		ctx.Next()
	}
}
View Source
var Default = func(ctx *context.Context) {
	traceId := ctx.GetHeader("X-Request-Id")
	if traceId == "" {
		traceId = uuid.New().String()
	}
	threadlocal.SetTraceId(traceId)
	userId := ctx.GetHeader("X-Request-UserId")
	if userId != "" {
		threadlocal.SetUserId(userId)
	}
	validator.TransInit(ctx)
	ctx.Next()
}
View Source
var GobalRecover = func(ctx *context.Context) {
	defer func() {
		if err := recover(); err != nil {
			if ctx.IsStopped() {
				return
			}

			var stacktrace string
			for i := 1; ; i++ {
				_, f, l, got := runtime.Caller(i)
				if !got {
					break
				}
				stacktrace += fmt.Sprintf("%s:%d\n", f, l)
			}

			errMsg := fmt.Sprintf("%s", err)
			zlog.Info("异常Ctl入口:{}", ctx.HandlerName())
			zlog.Error("ErrorInfo:{}", errMsg)

			if strings.HasPrefix(errMsg, "") {
				ctx.JSON(response.Error(common_error.RPC_CALL_ERROR, errMsg))
			} else {
				ctx.JSON(response.Error(common_error.SYSTEM_ERROR, errMsg))
			}
			ctx.StatusCode(500)
			ctx.StopExecution()
		}
	}()
	ctx.Next()
}
View Source
var NeedLogin = func(ctx *context.Context) {
	userId := ctx.GetHeader("X-Request-UserId")
	if userId == "" {
		ctx.JSON(response.Error(common_error.NOT_LOGGED_IN, nil))
	} else {
		if userId != "" {
			threadlocal.SetUserId(userId)
			ctx.Next()
		} else {
			ctx.JSON(response.Error(common_error.NOT_LOGGED_IN, nil))
		}
	}
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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