zhttp

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ProxyAuthorizationHeader  = "Proxy-Authorization"
	OpenAiAuthorizationHeader = "Authorization"
	ProxyUserHeader           = "Fast-user"
	ProxyPasswdHeader         = "Fast-passwd"

	ClaimsKey        = "claims"
	EmailKey         = "email"
	RequestIDKey     = "Request-Id"
	GRPCRequestIDKey = "request-id"
	RequestIDLogKey  = "requestIDLog"
)

Variables

View Source
var (
	OK             = StatusItf(status{httpStatusCode: http.StatusOK, code: 0, message: "OK"})
	ErrUnknown     = StatusItf(status{httpStatusCode: http.StatusInternalServerError, code: 99999, message: "The error unknown"})
	ErrPermission  = StatusItf(status{httpStatusCode: http.StatusForbidden, code: 10001, message: "Need to check permissions"})
	ErrServerPanic = StatusItf(status{httpStatusCode: http.StatusInternalServerError, code: 10002, message: "Server error"})

	// system errors
	ErrAuthentication = StatusItf(status{httpStatusCode: http.StatusUnauthorized, code: 20001, message: "Authentication failed"})
	ErrCreate         = StatusItf(status{httpStatusCode: http.StatusBadRequest, code: 20002, message: "Create failed"})
	ErrParameterMatch = StatusItf(status{httpStatusCode: http.StatusBadRequest, code: 20003, message: "Parameter binding failed"})
	ErrDelete         = StatusItf(status{httpStatusCode: http.StatusBadRequest, code: 20004, message: "Delete failed"})
	ErrGet            = StatusItf(status{httpStatusCode: http.StatusBadRequest, code: 20005, message: "Get failed"})
	ErrUpdate         = StatusItf(status{httpStatusCode: http.StatusBadRequest, code: 20006, message: "Update failed"})
)

所有错误都应当设立对应的错误码,保证敏感信息不会暴露,未定义的错误类型统一返回ErrUnknown 五位错误码规则: 第一位错误的类型 第二、三位错误出现的模块 第四、五位为该模块内部的错误

Functions

func ErrPanic

func ErrPanic(err error)

func GetCtxLog

func GetCtxLog(ctx context.Context) *zap.SugaredLogger

func GetGinCtxAny

func GetGinCtxAny[T any](c *gin.Context, key string) (ret T)

func GetLogFromGinContext

func GetLogFromGinContext(c *gin.Context) *zap.SugaredLogger

func NewE

func NewE(err error, sts StatusItf) *httpError

func Result

func Result(c *gin.Context, data interface{}, err error)

func Serve

func Serve(c *HttpServerConfig) error

func SetCtxLog

func SetCtxLog(ctx context.Context, log *zap.SugaredLogger) context.Context

func SetLogger

func SetLogger(l zlog.Logger)

Types

type BaseParam

type BaseParam struct {
	ginhelper.BaseParam
	RequestID string `header:"requestID"`
}

func (*BaseParam) Bind

func (param *BaseParam) Bind(c *gin.Context, p ginhelper.Parameter) error

func (*BaseParam) Result

func (param *BaseParam) Result(c *gin.Context, data ginhelper.Data, err error)

type HttpServerConfig

type HttpServerConfig struct {
	Name       string `default:"httpserver"`
	Port       uint   `default:"12321"`
	Https      bool
	Cert       string
	Key        string
	RouterFunc loadRouterFunc
}

HttpServerConfig 启动一个Http服务的配置

type StatusItf

type StatusItf interface {
	error
	Message() string
	HttpStatusCode() int
	Code() int
}

Jump to

Keyboard shortcuts

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