A powerful cli tool to implement gin annotation ⭐

gin-annotation

License GoDoc Go Go Report Card

A powerful cli tool to implement gin annotation

logo

Chinese Document

Features

  • Using code generating technology by operating golang AST
  • Routing group
  • Sorting middlewares through annotations
  • Non-intrusive design

Quick Start

  1. Installation.
go get github.com/1-st/gin-annotation
  1. Write your HandlerFunc anywhere.

source code files see dir: _example/simple

// 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",
	})
}
// 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())
}
  1. Run gin-annotation at the project directory (ex: _example/simple ; and you can specify multiple folders)
$ 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.go

  1. Look at the generated routing file
package main

import (
	"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)
		}
	}
}
  1. The last step , call Route() at main.go
package main

import (
	"github.com/gin-gonic/gin"
	"path"
)

func main() {
	e := gin.Default()
	Route(e)
	_ = e.Run("ip:port")
}

Annotations

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
]
*/
Owner
Tang WenXuan
to be a fullstack engineer
Tang WenXuan
Similar Resources

GTDF-CLI - The official CLI tool to operate with Getting Things Done Framework

GTDF-CLI - The official CLI tool to operate with Getting Things Done Framework

This is the official CLI tool to operate with Getting Things Done Framework. How

Feb 14, 2022

Flag is a simple but powerful command line option parsing library for Go support infinite level subcommand

Flag Flag is a simple but powerful commandline flag parsing library for Go. Documentation Documentation can be found at Godoc Supported features bool

Sep 26, 2022

A fast and powerful alternative to grep

sift A fast and powerful open source alternative to grep. Features sift has a slightly different focus than most other grep alternatives. Code search,

Jan 3, 2023

Building powerful interactive prompts in Go, inspired by python-prompt-toolkit.

Building powerful interactive prompts in Go, inspired by python-prompt-toolkit.

go-prompt A library for building powerful interactive prompts inspired by python-prompt-toolkit, making it easier to build cross-platform command line

Jan 3, 2023

A powerful little TUI framework 🏗

A powerful little TUI framework 🏗

Bubble Tea The fun, functional and stateful way to build terminal apps. A Go framework based on The Elm Architecture. Bubble Tea is well-suited for si

Dec 27, 2022

🚀 Platform providing a powerful and fast public script parsing API dedicated to the Skript community.

SkriptMC-Parser is currently a prototype in the early stages of development of a system that allows the Skript community to test their scripts via a public API for potential errors or warnings. This is a quick and easy way to check your scripts without having to set up a Spigot server on your environment.

Mar 3, 2022

This tool is a CLI-interactive tool for TA who use eeclass platform

NTHU eeclass TA helper. This tool is a CLI-interactive tool for TA who use eeclass platform. It helps TA to download all the submitted homework, and use CSV to record the score and comment, and upload CSV score directly to the eeclass platform with just 2 Enter key!

Dec 11, 2021

Elegant CLI wrapper for kubeseal CLI

Overview This is a wrapper CLI ofkubeseal CLI, specifically the raw mode. If you just need to encrypt your secret on RAW mode, this CLI will be the ea

Jan 8, 2022

CLI to run a docker image with R. CLI built using cobra library in go.

CLI  to run a docker image with R. CLI built using cobra library in go.

BlueBeak Installation Guide Task 1: Building the CLI The directory structure looks like Fastest process: 1)cd into bbtools 2)cd into bbtools/bin 3)I h

Dec 20, 2021
A template that help you to quick implement some CLI using Go

db-data-generator This is template that help you to quick implement some CLI using Go. This repository is contains following. minimal CLI implementati

Nov 22, 2021
There is a certain amount of work to be done before you can implement the features of your Go powered CLI app

go-project-template-cli There is a certain amount of work to be done before you can implement the features of your Go powered CLI app. A few of those

Jan 23, 2022
Powerful CLI written in GO to generate projects in various technologies
Powerful CLI written in GO to generate projects in various technologies

Barca CLI is a project generator written in GO and its purpose is to build and configure HTTP servers, web proxy, SPA/PWA, Blog and custom landing page. It's easy, fast and productive.

Aug 26, 2022
A powerful modern CLI and SHELL
A powerful modern CLI and SHELL

Grumble - A powerful modern CLI and SHELL There are a handful of powerful go CLI libraries available (spf13/cobra, urfave/cli). However sometimes an i

Dec 30, 2021
Mcli - A mininal and very powerful cli library for Go

mcli mcli is a minimal but powerful cli library for Go. m stands for minimal and

Nov 18, 2022
Nebula Diagnosis CLI Tool is an information diagnosis cli tool for the nebula service and the node to which the service belongs.

Nebula Diagnosis CLI Tool is an information diagnosis cli tool for the nebula service and the node to which the service belongs.

Jan 12, 2022
`tmax` is a powerful tool to help you get terminal cmd directly.
`tmax`  is a powerful tool to help you get terminal cmd directly.

The positioning of tmax is a command line tool with a little artificial intelligence. If you frequently deal with the terminal daily, tmax will greatly improve your work efficiency.

Oct 15, 2022
A lightweight but powerful OCR tool.
A lightweight but powerful OCR tool.

这个项目是什么? LOCR(Lightweight OCR)是一款轻量级的文字识别工具, 结合第三方截图工具, 可以快速的对图片文字进行识别。 为什么有这个项目 在日常学习的工作中, 难免会遇到一些文字的复制粘贴任务。但由于一些限制,我们无法复制想要的文字,只能一个字一个字的敲出来。而随着近几年OC

Nov 1, 2022
Symfony-cli - The Symfony CLI tool For Golang

Symfony CLI Install To install Symfony CLI, please download the appropriate vers

Dec 28, 2022
bcrypt-cli is the CLI tool for hashing passwords with bcrypt.

bcrypt-cli bcrypt-cli is the CLI tool for hashing passwords with bcrypt. Install go install github.com/ryicoh/bcrypt-cli Usage It can be used like bas

Jan 9, 2023