Giter Site home page Giter Site logo

ginrouteautoregister's Introduction

ginRouteAutoRegister

making gin route auto registration as simple as irismvc ##使用方式

import (
	"github.com/260721735/ginRouteAutoRegister"
)
func Route(engine *gin.Engine) {
	//原生路由注册方式
	engine.GET("/hello", controller.GetTest)
	//自动注册方式
	ginRouteAutoRegister.GinAutoRoute(engine, "/api", NewHelloController())
}

##Demo ###将HelloController.GetTest注册到http://localhost:8888/api/test路径上

import (
	"github.com/260721735/ginRouteAutoRegister"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"os"
	"os/signal"
	"syscall"
	"time"
)

func main() {
	router := gin.New()
	router.Use(gin.Recovery())
	Route(router)
	srv := &http.Server{
		Addr:    ":8888",
		Handler: router,
	}
	go func() {
		// service connections
		if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
			log.Fatalf("listen: %s\n", err)
		}
	}()
	quit := make(chan os.Signal)
	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
	<-quit
	//自定义退出时prestop逻辑,这里简单用等待5s
	time.Sleep(1 * time.Second)
	log.Println("Server exiting")
}
func Route(engine *gin.Engine) {
	ginRouteAutoRegister.GinAutoRoute(engine, "/api", NewHelloController())
}

type HelloController struct {
}

func NewHelloController() *HelloController {
	return &HelloController{}
}
func (controller *HelloController) GetTest(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "GetTest",
		"data": nil,
	})
}

controller

- controller层
  参照hello_controller.go
  -新建子location,controller层新增方法 GetLoation或PostLocation等
  -将controller注册到route/route.go中Route方法里

router

-目前支持AnyOptions


ginrouteautoregister's People

Contributors

260721735 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.