Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Context primitive indicator use Engine's raw handler as method #42

Closed
10 tasks done
alimy opened this issue Dec 13, 2023 · 1 comment
Closed
10 tasks done

add Context primitive indicator use Engine's raw handler as method #42

alimy opened this issue Dec 13, 2023 · 1 comment
Assignees
Labels
Milestone

Comments

@alimy
Copy link
Owner

alimy commented Dec 13, 2023

功能特性

  • 添加 Context 原语用于指示代码生成器 生成http引擎框架原生的handler作为接口方法;eg: gin as engine
// 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())
	})...)
}

具体示例请参考项目的examples。

适配进度

  • gin
  • chi
  • echo
  • fiber
  • hertz
  • httprouter
  • iris
  • macaron
  • mux
  • mirc
@alimy alimy self-assigned this Dec 13, 2023
@alimy alimy added the feature label Dec 13, 2023
@alimy alimy pinned this issue Dec 13, 2023
@alimy
Copy link
Owner Author

alimy commented Dec 14, 2023

优化了Context的语义:

  • 如果只使用了Context,没有req参数和resp参数,代码生成器就生成http框架的原生handler;
  • 如果添加了Context,并且有req参数或者resp参数或者两者都有,代码生成器就生成带有http框架上下文的go-mir handler,eg: gin :
    handler(*gin.Context, *Request) (*Response, mir.Error)

@alimy alimy added this to the v4.1 milestone Dec 26, 2023
@alimy alimy closed this as completed Dec 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant