httpcontext

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ContextKey = "gomooth_http_context"

	RequestRawBodyDataKey = "request_raw_body"
)

Variables

This section is empty.

Functions

func WithData

func WithData(key string, value interface{}) func(*httpContext)

func WithRawRequestBody

func WithRawRequestBody(body []byte) func(*httpContext)

func WithTrace

func WithTrace(traceID, spanID string) func(*httpContext)

func WithUser

func WithUser(user *User) func(*httpContext)

Types

type IHttpContext

type IHttpContext interface {
	context.Context

	// TraceID 链路追踪标志
	TraceID() string
	// User 用户信息
	User() *User
	// IsRole 判断用户角色
	IsRole(role IRole) bool
	// Set 设置参数
	// 会根据 value 的类型,自动设置对应属性的值,目前支持: User
	Set(key string, value interface{}) IHttpContext
	// StorageTo 将已变更的数据,存储到 gin 上下文中,继续传输
	StorageTo(ctx *gin.Context) bool
}

func MustParse

func MustParse(ctx context.Context) (IHttpContext, error)

MustParse 从 gin 上下文中解析自定义上下文

func NewContext

func NewContext(opts ...func(*httpContext)) IHttpContext

type IRole

type IRole interface {
	String() string
}

type ToRole

type ToRole func(role string) (IRole, error)

ToRole 转换成角色 IRole

type User

type User struct {
	ID      uint
	Account string
	Name    string
	Roles   []IRole

	IP     string            // 用户登录ID
	Extend map[string]string // 扩展信息
}

User 用户基本信息

func (*User) GetAccount

func (u *User) GetAccount() string

func (*User) GetExtendValue

func (u *User) GetExtendValue(field string) string

func (*User) GetID

func (u *User) GetID() uint

func (*User) GetIP

func (u *User) GetIP() string

func (*User) GetName

func (u *User) GetName() string

func (*User) GetRoles

func (u *User) GetRoles() []IRole

func (*User) Is

func (u *User) Is(role IRole) bool

Jump to

Keyboard shortcuts

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