pick

package module
v1.8.10 Latest Latest
Warning

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

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

README

pick

一个基于反射的自动注入api开发框架http api服务器,灵感来自于grpc和springmvc,pick的默认基于gin,如不想使用,pick同时兼容fiber(fasthttp),底层可选择。

feature

  • 摆脱路由注册

    摆脱零散的各处的路由注册 xxx.Handle("/",func(){})

  • 摆脱w,r,摆脱xxx.Context

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

  • 专注于业务

quick start

go get github.com/hopeio/pick go run _example/gin/main.go

usage

main.go

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

service.go

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

这会生成如下的Api

 API:	 POST   /api/v1/user   用户注册

api完整示例

func (*UserService) Add(ctx *ginctx.Context, req *model.SignupReq) (*model.User, error) {
	//对于一个性能强迫症来说,我宁愿它不优雅一些也不能接受每次都调用
	pick.Api(func() {
		 pick.Post("").//定义请求的方法及路由
			Title("用户注册").//接口描述
            //接口迭代信息
			CreateLog("1.0.0", "jyb", "2019/12/16", "创建").//创建,唯一
			ChangeLog("1.0.1", "jyb", "2019/12/16", "修改测试").//变更,可有多个
			Deprecated("1.0.0", "jyb", "2019/12/16", "删除").//废弃,唯一
            End()
	})
	return &model.User{Name: "测试"}, nil
}

文档生成

/apidoc pick会为我们生成swagger和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"`
}

swagger

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) Set added in v1.8.7

func (w Writer) Set(k, v string)

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