- Notifications
You must be signed in to change notification settings - Fork13
Mir is a toolkit for register method handler to http engine router(eg: gin,echo,iris,mux,httprouter) use struct tag info.
License
NotificationsYou must be signed in to change notification settings
alimy/mir
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Mir 是一套提供类似gRPC服务开发体验的快速开发RESTful API后端开发脚手架,适配多种HTTP框架,包括Gin,Chi,Hertz,Echo,Iris,Fiber,Macaron,Mux,httprouter。
- 生成样板项目
% go install github.com/alimy/mir/mirc/v5@latest% mirc new -hcreate template projectUsage: mirc new [flags]Flags: -d, --dst string genereted destination target directory (default".") -h, --helphelpfor new --mir string mir replace package name or place -p, --pkg string project's package name (default "github.com/alimy/mir-example") -s, --style string generated engine style eg: gin,chi,mux,hertz,echo,iris,fiber,fiber-v2,macaron,httprouter (default "gin")% mirc new -d example% tree exampleexample.|-- Makefile|-- README.md|-- go.mod|-- go.sum|-- main.go|-- mirc| |-- auto| | `-- api| | |-- site.go| | |-- v1| | | `-- site.go| | `-- v2| | `-- site.go| |-- gen.go| `-- routes| |-- site.go| |-- v1| | `-- site.go| `-- v2| `-- site.go`-- servants |-- core.go |-- servants.go |-- site.go |-- site_v1.go `-- site_v2.go% cd example% make generate% make run
- RESTful接口定义:
// file: mirc/routes/v1.gopackage v1import (."github.com/alimy/mir/v5")typeLoginReqstruct {Namestring`json:"name"`Passwdstring`json:"passwd"`}typeLoginRespstruct {JwtTokenstring`json:"jwt_token"`}// User user interface infotypeUserstruct {Schema`mir:"v1,chain"`Loginfunc(Post,LoginReq)LoginResp`mir:"login"`Logoutfunc(Post)`mir:"logout"`}
- 代码生成:
// file: mirc/auto/api/routes/v1.go// Code generated by go-mir. DO NOT EDIT.// versions:// - mir v5.2package v1import ("net/http""github.com/alimy/mir/v5""github.com/gin-gonic/gin")type_binding_interface {Bind(*gin.Context)error}type_render_interface {Render(*gin.Context)}type_default_interface {Bind(*gin.Context,any)errorRender(*gin.Context,any,error)}typeLoginReqstruct {Namestring`json:"name"`Passwdstring`json:"passwd"`}typeLoginRespstruct {JwtTokenstring`json:"jwt_token"`}typeUserinterface {_default_// Chain provide handlers chain for ginChain() gin.HandlersChainLogin(*gin.Context,*LoginReq) (*LoginResp,error)Logout(*gin.Context)errormustEmbedUnimplementedUserServant()}// RegisterUserServant register User servant to ginfuncRegisterUserServant(e*gin.Engine,sUser) {router:=e.Group("v1")// use chain for routermiddlewares:=s.Chain()router.Use(middlewares...)// register routes info to routerrouter.Handle("POST","login",func(c*gin.Context) {select {case<-c.Request.Context().Done():returndefault:}req:=new(LoginReq)iferr:=s.Bind(c,req);err!=nil {s.Render(c,nil,err)return}resp,err:=s.Login(req)s.Render(c,resp,err)})router.Handle("POST","logout",func(c*gin.Context) {select {case<-c.Request.Context().Done():returndefault:}s.Render(c,nil,s.Logout(c))})}func (UnimplementedUserServant)Chain() gin.HandlersChain {returnnil}func (UnimplementedUserServant)Login(c*gin.Context,req*LoginReq) (*LoginResp,error) {returnnil,mir.Errorln(http.StatusNotImplemented,http.StatusText(http.StatusNotImplemented))}func (UnimplementedUserServant)Logout(c*gin.Context)error {returnmir.Errorln(http.StatusNotImplemented,http.StatusText(http.StatusNotImplemented))}func (UnimplementedUserServant)mustEmbedUnimplementedUserServant() {}
- 接口实现:
// file: servants/user.gopackage servantsimport (api"github.com/alimy/mir-example/v5/mirc/auto/api/v1""github.com/alimy/mir/v5""github.com/gin-gonic/gin")typebaseSrvstruct{}func (baseSrv)Bind(c*gin.Context,objany)error {iferr:=c.ShouldBind(obj);err!=nil {mir.NewError(http.StatusBadRequest,err)}returnnil}func (baseSrv)Render(c*gin.Context,dataany,errerror) {iferr==nil {c.JSON(http.StatusOK,data)}elseifcode,ok:=mir.HttpStatusCode(err);ok {c.JSON(code,err.Error())}else {c.JSON(http.StatusInternalServer,err.Error())}}typeuserSrvstruct {baseSrvapi.UnimplementedUserServant// TODO: add other fields}funcnewUserSrv() api.User {return&userSrv{}}
- 服务注册:
// file: servants/servants.gopackage servantsimport (api"github.com/alimy/mir-example/v5/mirc/auto/api/v1""github.com/gin-gonic/gin")// RegisterServants register all the servants to gin.EnginefuncRegisterServants(e*gin.Engine) {api.RegisterUserServant(e,newUserSrv())// TODO: some other servant to register}
- 程序启动:
// file: main.gopackage mainimport ("log""github.com/alimy/mir-example/v5/servants""github.com/gin-gonic/gin")funcmain() {e:=gin.Default()// register servants to ginservants.RegisterServants(e)// start servant serviceiferr:=e.Run();err!=nil {log.Fatal(err)}}
使用go-mir的项目
About
Mir is a toolkit for register method handler to http engine router(eg: gin,echo,iris,mux,httprouter) use struct tag info.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.