Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/1-st/gin-annotation
A powerful cli tool to implement gin annotation ⭐
https://github.com/1-st/gin-annotation
gin golang
Last synced: 3 months ago
JSON representation
A powerful cli tool to implement gin annotation ⭐
- Host: GitHub
- URL: https://github.com/1-st/gin-annotation
- Owner: 1-st
- License: mit
- Created: 2021-01-07T17:42:59.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2021-03-22T14:37:15.000Z (almost 4 years ago)
- Last Synced: 2024-10-31T11:51:35.594Z (3 months ago)
- Topics: gin, golang
- Language: Go
- Homepage:
- Size: 33.2 KB
- Stars: 49
- Watchers: 3
- Forks: 10
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gin-annotation
[![License](http://img.shields.io/badge/license-mit-blue.svg?style=flat-square)](https://raw.githubusercontent.com/1-st/gin-annotation/main/LICENSE)
[![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](https://pkg.go.dev/github.com/1-st/gin-annotation)
[![Go](https://github.com/1-st/gin-annotation/workflows/Go/badge.svg?branch=main)](https://github.com/1-st/gin-annotation/actions)
[![Go Report Card](https://goreportcard.com/badge/github.com/1-st/gin-annotation)](https://goreportcard.com/report/github.com/1-st/gin-annotation)A powerful cli tool to implement gin annotation
[Chinese Document](https://github.com/1-st/gin-annotation/blob/main/README_ch.md)
## Features
* Using code generating technology by operating golang [AST](https://en.wikipedia.org/wiki/Abstract_syntax_tree)
* Routing group
* Sorting middlewares through annotations
* Non-intrusive design## Quick Start
1. Installation.
```shell
go get github.com/1-st/gin-annotation
```2. Write your HandlerFunc anywhere.
> source code files see dir: *_example/simple*
```go
// controller/hello.go
package controller/* Hello a simple controller
[
method:GET,
groups:/api,
path:/hello-world,
need:auth
]
*/
func HelloWorld(ctx *gin.Context) {
ctx.JSON(http.StatusOK, map[string]string{
"msg": "hello, world",
})
}
``````go
// middleware/auth.go
package middleware/* Auth a simple middleware
[
id:auth
]
*/
func Auth(ctx *gin.Context) {
ctx.Next()
}/* Log the first middleware in group /api
[
id:log,
group:/api@1
]
*/
func Log(ctx *gin.Context) {
fmt.Println(ctx.ClientIP())
}
```3. Run gin-annotation at the project directory (ex: *_example/simple* ; and you can specify multiple folders)
```sh
$ gin-annotation ./
$ ls
controller main.go route.entry.go(!!!new file)
```> tips: the name of new file is decided by environment variable GIN_ANNOTATION_FILE
, default is route.entry.go4. Look at the generated routing file
```go
package mainimport (
"gin-annotation/_example/simple/controller"
"gin-annotation/_example/simple/middleware"
"github.com/gin-gonic/gin"
)func Route(e *gin.Engine) {
api := e.Group("/api", middleware.Log)
{
v1 := api.Group("/v1")
{
v1.GET("/hello-world", middleware.Auth, controller.HelloWorld)
}
}
}
```5. The last step , call Route() at main.go
```go
package mainimport (
"github.com/gin-gonic/gin"
"path"
)func main() {
e := gin.Default()
Route(e)
_ = e.Run("ip:port")
}
```## Annotations
- handlers
- [groups](#groups-annotation)
- [path](#path-annotation)
- [method](#method-annotation)
- [need](#need-annotation)
- middlewares
- [id](#id-annotation)
- [group](#group-annotation)
- [notice](#notice)### Groups Annotation
Each groups-annotation consists of multiple groups separated by spaces.
### Path Annotation
The last element of path.
### Method Annotation
GET,POST,DELETE,PATCH,OPTIONS or ANY.
### Need Annotation
> need:id1 id2,
Each element of need-annotation is the id of the middleware.
### Id Annotation
The unique id of middleware.
### Group Annotation
```
/* example
[
id:example,
group:/api/v1/@1,
group:/api/v2/@1
]
*/
```Each middleware can have multiple group-annotations,
The number behind @ is the priority of middleware.
### Notice
* Don't write extra "," in the last item of an annotation.
```
/* example
[
id:example,
group:/api/v1/@1 <- here
]
*/
```