Skip to content

v4.1.0-alpha.1

Compare
Choose a tag to compare
@alimy alimy released this 13 Dec 04:11
· 45 commits to main since this release
f54b37e
  • add Context primitive support for gin engine code generate

Usage:

// go-mir route interface define file
// Site site interface info
type Site struct {
	Chain            `mir:"-"`
	Index            func(Get, Chain)                               `mir:"/index/"`
	Assets           func(Get, Context)                             `mir:"/assets/:name"`
}

使用gin的代码生成器生成的代码如下

type _binding_ interface {
	Bind(*gin.Context) mir.Error
}

type _render_ interface {
	Render(*gin.Context)
}

type _default_ interface {
	Bind(*gin.Context, any) mir.Error
	Render(*gin.Context, any, mir.Error)
}

type Site interface {
	_default_

	// Chain provide handlers chain for gin
	Chain() gin.HandlersChain

	Assets(*gin.Context)
	Index() mir.Error

	mustEmbedUnimplementedSiteServant()
}

// RegisterSiteServant register Site servant to gin
func RegisterSiteServant(e *gin.Engine, s Site, m ...SiteChain) {
	var cc SiteChain
	if len(m) > 0 {
		cc = m[0]
	} else {
		cc = &UnimplementedSiteChain{}
	}
	router := e
	// use chain for router
	middlewares := s.Chain()
	router.Use(middlewares...)

	router.Handle("GET", "/assets/:name", s.Assets)	
	router.Handle("GET", "/index/", append(cc.ChainIndex(), func(c *gin.Context) {
		select {
		case <-c.Request.Context().Done():
			return
		default:
		}

		s.Render(c, nil, s.Index())
	})...)
}