Help us shape the future of Go! Take the 2026 Go Developer Survey

crudh

package
v0.1.11 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package crudh 提供基于 Hertz 的通用 CRUD HTTP Handler。

将 HTTP Handler 从 data/crud 数据层分离,避免数据层反向依赖 HTTP 中间件层。 Handler 依赖 crud.Service 接口和 middleware 包的响应函数。

用法:

repo := gormrepo.NewGormRepository[Project](db)
svc := crud.NewBaseService[Project, CreateReq, UpdateReq](repo)
h := crudh.NewHandler(svc)
h.RegisterRoutes(group, "/projects")

Package crudh 提供基于 Hertz 的通用 CRUD HTTP Handler。

将 HTTP 层(Handler)与数据层(crud.Repository/crud.Service)分离, 避免 data/crud 包反向依赖 middleware 包。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

type Handler[T any, CreateReq, UpdateReq any] struct {
	// contains filtered or unexported fields
}

Handler 是通用 CRUD HTTP Handler。

func NewHandler

func NewHandler[T any, CreateReq, UpdateReq any](svc crud.Service[T, CreateReq, UpdateReq], opts ...HandlerOption[T, CreateReq, UpdateReq]) *Handler[T, CreateReq, UpdateReq]

NewHandler 创建 CRUD Handler。

func (*Handler[T, C, U]) Create

func (h *Handler[T, C, U]) Create(ctx context.Context, c *app.RequestContext)

Create 处理 POST 创建请求。

func (*Handler[T, C, U]) Delete

func (h *Handler[T, C, U]) Delete(ctx context.Context, c *app.RequestContext)

Delete 处理 DELETE 删除请求。

func (*Handler[T, C, U]) Get

func (h *Handler[T, C, U]) Get(ctx context.Context, c *app.RequestContext)

Get 处理 GET 单个获取请求。

func (*Handler[T, C, U]) List

func (h *Handler[T, C, U]) List(ctx context.Context, c *app.RequestContext)

List 处理 GET 列表查询请求(带分页)。

func (*Handler[T, C, U]) RegisterRoutes

func (h *Handler[T, C, U]) RegisterRoutes(rg *route.RouterGroup, prefix string)

RegisterRoutes 注册 CRUD 路由到 Hertz Group。

func (*Handler[T, C, U]) Update

func (h *Handler[T, C, U]) Update(ctx context.Context, c *app.RequestContext)

Update 处理 PUT 更新请求。

type HandlerOption

type HandlerOption[T any, CreateReq, UpdateReq any] func(*Handler[T, CreateReq, UpdateReq])

HandlerOption 是 NewHandler 的配置选项。

func WithSortableColumns

func WithSortableColumns[T any, CreateReq, UpdateReq any](cols ...string) HandlerOption[T, CreateReq, UpdateReq]

WithSortableColumns 设置 List 接口排序参数的白名单列名。 未设置时仅做标识符格式校验;设置后白名单外的列名返回 400。

Jump to

Keyboard shortcuts

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