pick

package module
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2025 License: BSD-3-Clause Imports: 19 Imported by: 3

README

pick

一个基于反射的自动注入api开发框架,灵感来自于grpc和springmvc pick的底层是灵活的,默认基于gin,同时兼容fiber(fasthttp)。

feature

  • 摆脱路由注册

    xxx.Handle("/",func(){})

  • 摆脱w,r,摆脱xxx.Context这种不直观输入输出的handler

    func(w http.ResponseWriter, r *http.Request)或者func(ctx xxx.Context){ctx.XXX()}的业务代码

  • 类grpc的函数签名,专注于业务

    func(ctx *ginctx.Context,r ReqStruct) (RespStruct,error)

quick start

go get github.com/hopeio/pick

go run $(go list -m -f {{.Dir}} github.com/hopeio/pick)/_example/gin/main.go

usage

main.go

func main() {
  server := gin.New()
  pickgin.Register(server, &UserService{})
  log.Fatal(server.Run(":8080"))
}

service.go

// 首先我们需要定义一个服务
type UserService struct{}
//需要实现Service方法,返回该服务的说明,url前缀,以及需要的中间件
func (*UserService) Service() (string, string, []gin.HandlerFunc) {
return "用户相关", "/api/v1/user", []gin.HandlerFunc{}
}
type Req struct{
  ID int `json:"id"`
}
type User struct {
	ID int `json:"id"`
	Name string `json:"name"`
}
// 然后可以写我们的业务方法
func (*UserService) Get(ctx *ginctx.Context, req *Req) (*User, error) {
//对于一个性能强迫症来说,我宁愿它不优雅一些也不能接受每次都调用
  pick.Api(func() {
    pick.Get(":/id").
    Title("用户详情").
    CreateLog("1.0.0", "jyb", "2019/12/16", "创建").
    ChangeLog("1.0.1", "jyb", "2019/12/16", "修改测试").
    End()
  })
  return &model.User{ID:req.ID,Name: "测试"}, nil
}

这会生成如下的Api

API: Get /api/v1/user/:id 用户详情

curl http://localhost:8080/api/v1/user/1
返回: {"id":1,"name":"测试"}

文档生成

/apidoc pick会为我们生成openapi和markdown文档 当然,这需要你定义的请求配合,例如

type User struct {
	Id uint64 `json:"id"`
	Name string `json:"name" comment:"名字" validate:"gte=3,lte=10"`
	Password string `json:"password" comment:"密码" validate:"gte=8,lte=15"`
	Mail string `json:"mail" comment:"邮箱" validate:"email"`
	Phone string `json:"phone" comment:"手机" validate:"phone"`
}

openapi

Image text

markdown

[TOC]

用户相关


用户注册-v1(/api/v1/user)

POST /api/v1/user (Principal jyb)

接口记录
版本 操作 时间 负责人 日志
1.0.0 创建 2019/12/16 jyb 创建
1.0.1 变更 2019/12/16 jyb 修改测试
参数信息
字段名称 字段类型 字段描述 校验要求
name string 名字 长度必须至少为3个字符
password string 密码 长度必须至少为8个字符
mail string 邮箱 必须是一个有效的手机号!
请求示例
{
	"name": "耰塧囎飿段",
	"password": "虱鷅磷黽楑",
	"mail": "盬艦潦昊譙"
}  
返回信息
字段名称 字段类型 字段描述
id number
name string 名字
password string 密码
mail string 邮箱
mail string 手机
返回示例
{
	"id": 1357,
	"name": "鐷嚅凮珘緻",
	"password": "梊朖迍髽栳"
}  

用户编辑-v1(废弃)(/api/v1/user/:id)

PUT /api/v1/user/:id (Principal jyb)

接口记录

...

是的,示例并不那么好看,并非不能支持简体字和英文字母,我计划单独写一个mock模块

changelog

  1. 移除httprouter,默认gin路由

Documentation

Index

Constants

View Source
const Template = `` /* 332-byte string literal not displayed */

Variables

View Source
var (
	ContextType  = reflect.TypeOf((*context.Context)(nil)).Elem()
	ContextValue = reflect.ValueOf(context.Background())
	ErrorType    = reflect.TypeOf((*error)(nil)).Elem()
)
View Source
var (
	HttpContextType = reflect.TypeOf((*httpctx.Context)(nil))
)

Functions

func Api

func Api(f func())

func CommonHandler added in v1.5.25

func CommonHandler(w http.ResponseWriter, req *http.Request, handle *reflect.Value)

func Connect

func Connect(path string) *apiInfo[none]

func Delete

func Delete(path string) *apiInfo[none]

func Get

func Get(path string) *apiInfo[none]

func GetMethodInfo

func GetMethodInfo[T any](method *reflect.Method, preUrl string, httpContext reflect.Type) (info *apiInfo[T])

recover捕捉panic info

func HandlerPrefix added in v1.7.0

func HandlerPrefix(p string)
func Head(path string) *apiInfo[none]

func Log

func Log(method, path, title string)

func Middleware added in v1.8.8

func Middleware[T any](middleware ...T) *apiInfo[T]

func Options

func Options(path string) *apiInfo[none]

func Patch

func Patch(path string) *apiInfo[none]

func Post

func Post(path string) *apiInfo[none]

func Put

func Put(path string) *apiInfo[none]

func Registered

func Registered()

func ResWriteReflect added in v1.5.6

func ResWriteReflect(w httpi.ICommonResponseWriter, traceId string, result []reflect.Value) error

func Trace

func Trace(path string) *apiInfo[none]

Types

type ApiInfo

type ApiInfo struct {
	Routes      []Route
	Middlewares []none
	Title       string
	Createlog   Changelog
	Changelog   []Changelog
	Deprecated  *Changelog
}

func (*ApiInfo) GetPrincipal added in v1.8.0

func (api *ApiInfo) GetPrincipal() string

获取负责人

type Changelog added in v1.8.8

type Changelog struct {
	Version, Auth, Date, Desc string
}

type ParseToHttpResponse

type ParseToHttpResponse interface {
	Parse() ([]byte, error)
}

type Route added in v1.8.8

type Route struct {
	Path, Method, Remark string
}

type Service

type Service[T any] interface {
	//返回描述,url的前缀,中间件
	Service() (describe, prefix string, middleware []T)
}

type Writer added in v1.8.7

type Writer struct {
	http.ResponseWriter
}

func (Writer) Header added in v1.9.0

func (w Writer) Header() httpi.Header

func (Writer) Status added in v1.8.7

func (w Writer) Status(code int)

func (Writer) Write added in v1.8.7

func (w Writer) Write(p []byte) (int, error)

Directories

Path Synopsis
_example
gin

Jump to

Keyboard shortcuts

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