api

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Adapt

func Adapt[Req any, Resp any](adapter *Adapter, mode BindMode, handler HandlerFunc[Req, Resp]) gin.HandlerFunc

func BadRequestError

func BadRequestError(message string, cause error) error

func ConflictError

func ConflictError(message string, cause error) error

func ErrorWithCode

func ErrorWithCode(status int, code, message string, details map[string]any) error

ErrorWithCode 用于需要结构化错误响应的端点(按 code 做前端 i18n)。

func ForbiddenError

func ForbiddenError(message string) error

func InternalError

func InternalError(message string, cause error) error

func NormalizeAllowedChannelIDs

func NormalizeAllowedChannelIDs(v any) ([]uint, error)

NormalizeAllowedChannelIDs converts an arbitrary JSON value (typically from a map[string]any update payload) into []uint, rejecting non-array or non-integer / negative values. A nil input returns nil, nil.

func NormalizeAllowedGroupIDs added in v0.0.8

func NormalizeAllowedGroupIDs(v any) ([]uint, error)

NormalizeAllowedGroupIDs converts an arbitrary JSON value (typically from a map[string]any update payload) into []uint, rejecting non-array or non-integer / negative values. A nil input returns (nil, nil).

func NormalizePagination

func NormalizePagination(page, pageSize int) (int, int)

func NotFoundError

func NotFoundError(message string) error

func ResolveGroupID added in v0.0.8

func ResolveGroupID(q dao.AdminQuery, userID uint) (uint, error)

ResolveGroupID 返回 userID 所属用户组 ID。查不到用户时返回 (0, err)。 放在 api 包供 token / token_template 等子包共用;只依赖 dao,不依赖 middleware。

func StatusEqualsEnabled added in v0.0.6

func StatusEqualsEnabled(v any) bool

StatusEqualsEnabled reports whether v decodes to consts.StatusEnabled. Call only after ValidateStatusValue has confirmed v is a legal status.

func UnauthorizedError

func UnauthorizedError(message string) error

func ValidateAllowedChannelIDs

func ValidateAllowedChannelIDs(ids []uint) error

ValidateAllowedChannelIDs validates a slice of channel IDs for token / token_template / user_group. Returns error if length > 100 or any ID is zero.

func ValidateAllowedGroupIDs added in v0.0.8

func ValidateAllowedGroupIDs(ids []uint) error

ValidateAllowedGroupIDs validates a slice of user-group IDs for token_template. Returns error if length > 100 or any ID is zero.

func ValidateStatusValue

func ValidateStatusValue(v any) error

ValidateStatusValue checks that the provided value is a valid status (consts.StatusEnabled or consts.StatusDisabled). The value may come from a JSON-decoded map[string]any (float64) or from a typed int field.

Types

type APIError

type APIError struct {
	Status  int
	Code    string // 结构化错误 code(前端 i18n 用)
	Message string
	Details map[string]any // 结构化错误细节
	Cause   error
}

func (*APIError) Error

func (e *APIError) Error() string

type Adapter

type Adapter struct {
	ContextFactory ContextFactory
	Binder         RequestBinder
	Writer         ResponseWriter
	ErrorMapper    ErrorMapper
}

func NewAdapter

func NewAdapter(settings *config.MasterRuntimeConfig, logger *zap.Logger, application app.Application) *Adapter

type BindMode

type BindMode int
const (
	BindNone BindMode = iota
	BindJSON
	BindOptionalJSON
	BindQuery
	BindURI
	BindURIAndJSON
	BindURIAndOptionalJSON
	BindURIAndQuery
	BindURIAndBodyMap
)

type BodyMapSetter

type BodyMapSetter interface {
	SetBodyMap(map[string]any)
}

type BodyProvider

type BodyProvider interface {
	Body() any
}

type ContextFactory

type ContextFactory interface {
	Build(*gin.Context) *app.Context
}

type Created

type Created[T any] struct {
	Value T
}

func (Created[T]) Body

func (c Created[T]) Body() any

func (Created[T]) StatusCode

func (Created[T]) StatusCode() int

type DefaultContextFactory

type DefaultContextFactory struct {
	Settings *config.MasterRuntimeConfig
	Logger   *zap.Logger
	App      app.Application
}

func (DefaultContextFactory) Build

type DefaultErrorMapper

type DefaultErrorMapper struct{}

func (DefaultErrorMapper) Map

func (DefaultErrorMapper) Map(err error) (int, any)

type DefaultRequestBinder

type DefaultRequestBinder struct{}

func (DefaultRequestBinder) Bind

func (DefaultRequestBinder) Bind(c *gin.Context, mode BindMode, req any) error

type EmptyRequest

type EmptyRequest struct{}

type ErrorMapper

type ErrorMapper interface {
	Map(error) (int, any)
}

type HandlerFunc

type HandlerFunc[Req any, Resp any] func(*app.Context, Req) (Resp, error)

type IDPathRequest

type IDPathRequest struct {
	ID string `uri:"id" binding:"required"`
}

type JSONWriter

type JSONWriter struct{}

func (JSONWriter) WriteJSON

func (JSONWriter) WriteJSON(c *gin.Context, status int, body any)

type PaginatedResponse

type PaginatedResponse[T any] struct {
	Data     []T   `json:"data"`
	Total    int64 `json:"total"`
	Page     int   `json:"page"`
	PageSize int   `json:"page_size"`
}

type PaginationQuery

type PaginationQuery struct {
	Page     int `form:"page"`
	PageSize int `form:"page_size"`
}

type RequestBinder

type RequestBinder interface {
	Bind(*gin.Context, BindMode, any) error
}

type ResponseWriter

type ResponseWriter interface {
	WriteJSON(*gin.Context, int, any)
}

type StatusCoder

type StatusCoder interface {
	StatusCode() int
}

type StatusResponse

type StatusResponse struct {
	Status string `json:"status"`
}

Jump to

Keyboard shortcuts

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