{"id":13494529,"url":"https://github.com/alimy/mir","last_synced_at":"2025-05-07T16:10:39.467Z","repository":{"id":57485744,"uuid":"165086727","full_name":"alimy/mir","owner":"alimy","description":" Mir is a toolkit for register method handler to http engine router(eg: gin,echo,iris,mux,httprouter) use struct tag info.","archived":false,"fork":false,"pushed_at":"2025-04-30T08:27:52.000Z","size":5953,"stargazers_count":82,"open_issues_count":4,"forks_count":14,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-05-07T16:10:04.764Z","etag":null,"topics":["echo","gin","go","go-chi","go-mir","golang","hertz","httprouter","iris","macaron","mux","web"],"latest_commit_sha":null,"homepage":"https://alimy.github.io/mir/","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alimy.png","metadata":{"files":{"readme":"README-en.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-01-10T15:46:16.000Z","updated_at":"2025-01-13T06:56:58.000Z","dependencies_parsed_at":"2024-02-09T13:27:49.435Z","dependency_job_id":"9c07dbbd-0f6d-4ac9-8b9f-007673fe9d2b","html_url":"https://github.com/alimy/mir","commit_stats":{"total_commits":442,"total_committers":2,"mean_commits":221.0,"dds":0.009049773755656076,"last_synced_commit":"0eb651f4522dcbb06c7d690bf234231540dc94bf"},"previous_names":[],"tags_count":164,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alimy%2Fmir","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alimy%2Fmir/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alimy%2Fmir/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alimy%2Fmir/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alimy","download_url":"https://codeload.github.com/alimy/mir/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252912996,"owners_count":21824066,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["echo","gin","go","go-chi","go-mir","golang","hertz","httprouter","iris","macaron","mux","web"],"created_at":"2024-07-31T19:01:25.800Z","updated_at":"2025-05-07T16:10:39.443Z","avatar_url":"https://github.com/alimy.png","language":"Go","readme":"[![Go](https://github.com/alimy/mir/actions/workflows/go.yml/badge.svg)](https://github.com/alimy/mir/actions/workflows/go.yml)\n[![GoDoc](https://godoc.org/github.com/alimy/mir?status.svg)](https://pkg.go.dev/github.com/alimy/mir/v4)\n[![Sourcegraph](https://img.shields.io/badge/view%20on-Sourcegraph-brightgreen.svg?logo=sourcegraph)](https://sourcegraph.com/github.com/alimy/mir)\n\n\u003cdiv align=\"center\"\u003e\n  \u003ca href=\"https://github.com/alimy/mir\"\u003e\n    \u003cimg src=\"https://alimy.me/mir/images/mir-logo.png\" alt=\"Logo\" width=\"240\" height=\"240\"\u003e\n  \u003c/a\u003e\n\n  \u003ch3 align=\"center\"\u003eMir\u003c/h3\u003e\n\n  \u003cp align=\"center\"\u003e\n    A cool help tool to develop RESTful API.\n  \u003c/p\u003e\n\u003c/div\u003e\n\nMir is a toolkit to develop RESTful API backend service like develop service of gRPC. It adapt some HTTP framework sush as [Gin](https://github.com/gin-gonic/gin), [Chi](https://github.com/go-chi/chi), [Hertz](https://github.com/cloudwego/hertz), [Echo](https://github.com/labstack/echo), [Iris](https://github.com/kataras/iris), [Fiber](https://github.com/gofiber/fiber), [Macaron](https://github.com/go-macaron/macaron), [Mux](https://github.com/gorilla/mux), [httprouter](https://github.com/julienschmidt/httprouter)。  \n\n\u003cdiv align=\"center\"\u003e\n  \u003ca href=\"https://github.com/alimy/mir\"\u003e\n    \u003cimg src=\"https://alimy.me/mir/images/mir-arc.png\"\u003e\n  \u003c/a\u003e\n\u003c/div\u003e\n \n ### Tutorials\n* Generate a simple template project\n```bash\n% go install github.com/alimy/mir/mirc/v4@latest\n% mirc new -h\ncreate template project\n\nUsage:\n  mirc new [flags]\n\nFlags:\n  -d, --dst string     genereted destination target directory (default \".\")\n  -h, --help           help for new\n      --mir string     mir replace package name or place\n  -p, --pkg string     project's package name (default \"github.com/alimy/mir-example\")\n  -s, --style string   generated engine style eg: gin,chi,mux,hertz,echo,iris,fiber,fiber-v2,macaron,httprouter (default \"gin\")\n\n% mirc new -d example \n% tree example\nexample\n.\n|-- Makefile\n|-- README.md\n|-- go.mod\n|-- go.sum\n|-- main.go\n|-- mirc\n|   |-- auto\n|   |   `-- api\n|   |       |-- site.go\n|   |       |-- v1\n|   |       |   `-- site.go\n|   |       `-- v2\n|   |           `-- site.go\n|   |-- gen.go\n|   `-- routes\n|       |-- site.go\n|       |-- v1\n|       |   `-- site.go\n|       `-- v2\n|           `-- site.go\n`-- servants\n    |-- core.go\n    |-- servants.go\n    |-- site.go\n    |-- site_v1.go\n    `-- site_v2.go\n\n% cd example\n% make generate\n% make run\n```\n \n* RESTful API define:\n```go\n// file: mirc/routes.go\n\npackage routes\n\nimport (\n\t. \"github.com/alimy/mir/v4\"\n\t. \"github.com/alimy/mir/v4/engine\"\n)\n\nfunc init() {\n\tAddEntry(new(User))\n}\n\ntype LoginReq struct {\n\tName   string `json:\"name\"`\n\tPasswd string `json:\"passwd\"`\n}\n\ntype LoginResp struct {\n\tJwtToken string `json:\"jwt_token\"`\n}\n\n// User user interface info\ntype User struct {\n\tChain  Chain                          `mir:\"-\"`\n\tGroup  Group                          `mir:\"v1\"`\n\tLogin  func(Post, LoginReq) LoginResp `mir:\"/login/\"`\n\tLogout func(Post)                     `mir:\"/logout/\"`\n}\n```\n\n* Stub source code generatee automatic:\n```go\n// file: mirc/auto/api/routes.go\n\n// Code generated by go-mir. DO NOT EDIT.\n// versions:\n// - mir v4.0.0\n\npackage routes\n\nimport (\n\t\"net/http\"\n\n\t\"github.com/alimy/mir/v4\"\n\t\"github.com/gin-gonic/gin\"\n)\n\ntype _binding_ interface {\n\tBind(*gin.Context) mir.Error\n}\n\ntype _render_ interface {\n\tRender(*gin.Context)\n}\n\ntype _default_ interface {\n\tBind(*gin.Context, any) mir.Error\n\tRender(*gin.Context, any, mir.Error)\n}\n\ntype LoginReq struct {\n\tName   string `json:\"name\"`\n\tPasswd string `json:\"passwd\"`\n}\n\ntype LoginResp struct {\n\tJwtToken string `json:\"jwt_token\"`\n}\n\ntype User interface {\n\t_default_\n\n\t// Chain provide handlers chain for gin\n\tChain() gin.HandlersChain\n\n\tLogin(*gin.Context, *LoginReq) (*LoginResp, mir.Error)\n\tLogout(*gin.Context) mir.Error\n\n\tmustEmbedUnimplementedUserServant()\n}\n\n// UnimplementedUserServant can be embedded to have forward compatible implementations.\ntype UnimplementedUserServant struct {\n}\n\n// RegisterUserServant register User servant to gin\nfunc RegisterUserServant(e *gin.Engine, s User) {\n\trouter := e.Group(\"v1\")\n\t// use chain for router\n\tmiddlewares := s.Chain()\n\trouter.Use(middlewares...)\n\n\t// register routes info to router\n\trouter.Handle(\"POST\", \"/login/\", func(c *gin.Context) {\n\t\tselect {\n\t\tcase \u003c-c.Request.Context().Done():\n\t\t\treturn\n\t\tdefault:\n\t\t}\n\t\treq := new(LoginReq)\n\t\tif err := s.Bind(c, req); err != nil {\n\t\t\ts.Render(c, nil, err)\n\t\t\treturn\n\t\t}\n\t\tresp, err := s.Login(req)\n\t\ts.Render(c, resp, err)\n\t})\n\trouter.Handle(\"POST\", \"/logout/\", func(c *gin.Context) {\n\t\tselect {\n\t\tcase \u003c-c.Request.Context().Done():\n\t\t\treturn\n\t\tdefault:\n\t\t}\n\t\t\n\t\tr.Render(c, nil, s.Logout(c))\n\t})\n}\n\nfunc (UnimplementedUserServant) Chain() gin.HandlersChain {\n\treturn nil\n}\n\nfunc (UnimplementedUserServant) Login(c *gin.Context, req *LoginReq) (*LoginResp, mir.Error) {\n\treturn nil, mir.Errorln(http.StatusNotImplemented, http.StatusText(http.StatusNotImplemented))\n}\n\nfunc (UnimplementedUserServant) Logout(c *gin.Context) mir.Error {\n\treturn mir.Errorln(http.StatusNotImplemented, http.StatusText(http.StatusNotImplemented))\n}\n\nfunc (UnimplementedUserServant) mustEmbedUnimplementedUserServant() {}\n```\n\n* API interface implement:   \n```go\n// file: servants/user.go\n\npackage servants\n\nimport (\n\t\"github.com/alimy/mir-example/v4/mirc/auto/api\"\n\t\"github.com/alimy/mir/v4\"\n\t\"github.com/gin-gonic/gin\"\n)\n\ntype baseSrv struct{}\n\nfunc (baseSrv) Bind(c *gin.Context, obj any) mir.Error {\n\tif err := c.ShouldBind(obj); err != nil {\n\t\tmir.NewError(http.StatusBadRequest, err)\n\t}\n\treturn nil\n}\n\nfunc (baseSrv) Render(c *gin.Context, data any, err mir.Error) {\n\tif err == nil {\n\t\tc.JSON(http.StatusOK, data)\n\t} else {\n\t\tc.JSON(err.StatusCode(), err.Error())\n\t}\n}\n\ntype userSrv struct {\n\tbaseSrv\n\n\tapi.UnimplementedUserServant\n}\n\nfunc newUserSrv() api.Site {\n\treturn \u0026userSrv{}\n}\n```\n\n* Service register:  \n```go\n// file: servants/servants.go\n\npackage servants\n\nimport (\n\t\"github.com/alimy/mir-example/v4/mirc/auto/api\"\n\t\"github.com/gin-gonic/gin\"\n)\n\n// RegisterServants register all the servants to gin.Engine\nfunc RegisterServants(e *gin.Engine) {\n\tapi.RegisterUserServant(e, newUserSrv())\n\t\n\t// TODO: some other servant to register\n}\n```\n\n* App start:\n```go\n// file: main.go\n\npackage main\n\nimport (\n\t\"log\"\n\n\t\"github.com/alimy/mir-example/v4/servants\"\n\t\"github.com/gin-gonic/gin\"\n)\n\nfunc main() {\n\te := gin.Default()\n\n\t// register servants to gin\n\tservants.RegisterServants(e)\n\n\t// start servant service\n\tif err := e.Run(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n}\n```\n\n### Projects that used [go-mir](https://github.com/alimy/mir) \n * [examples](examples) - a demo example to describe how to use [Mir](https://github.com/alimy/mir) to develop RESTful API backend service quickly.\n* [paopao-ce](https://github.com/rocboss/paopao-ce/tree/dev) - A artistic \"twitter like\" community built on gin+zinc+vue+ts.      \n","funding_links":[],"categories":["Go"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falimy%2Fmir","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falimy%2Fmir","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falimy%2Fmir/lists"}