GoFrame 路由注册-函数注册

函数注册

函数注册方式是最简单且最灵活的的路由注册方式,注册的服务可以是一个实例化对象的方法地址,也可以是一个包方法地址。服务需要的数据可以通过模块内部变量形式或者对象内部变量形式进行管理,开发者可根据实际情况进行灵活控制。相关方法:

func (s *Server) BindHandler(pattern string, handler interface{})

我们可以直接通过​BindHandler​方法完成回调函数的注册,在框架的开发手册中很多地方都使用了回调函数注册的方式来做演示,因为这种注册方式比较简单。

使用示例

Hello World

我们来看一个简单的示例:

package main

import (
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/net/ghttp"
)

func main() {
    s := g.Server()
    s.BindHandler("/", func(r *ghttp.Request) {
        r.Response.Write("哈喽世界!")
    })
    s.SetPort(8199)
    s.Run()
}

执行后,我们访问 http://127.0.0.1:8199 可以看到我们熟悉的内容。

包方法注册

注册的路由函数可以是一个包方法,例如:

package main

import (
	"github.com/gogf/gf/v2/container/gtype"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
)

var (
	total = gtype.NewInt()
)

func Total(r *ghttp.Request) {
	r.Response.Write("total:", total.Add(1))
}

func main() {
	s := g.Server()
	s.BindHandler("/total", Total)
	s.SetPort(8199)
	s.Run()
}

在该示例中,我们通过包方法的形式来注册路由。该方法返回总共访问的次数,由于涉及到并发安全,因此​total​变量使用了​gtype.Int​并发安全类型。执行后,当我们不停访问 ​http://127.0.0.1:8199/total​ 时,可以看到返回的数值不停递增。

对象方法注册

注册的路由函数可以是一个对象的方法,例如:

package main

import (
	"github.com/gogf/gf/v2/container/gtype"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
)

type Controller struct {
	total *gtype.Int
}

func (c *Controller) Total(r *ghttp.Request) {
	r.Response.Write("total:", c.total.Add(1))
}

func main() {
	s := g.Server()
	c := &Controller{
		total: gtype.NewInt(),
	}
	s.BindHandler("/total", c.Total)
	s.SetPort(8199)
	s.Run()
}

该示例与示例1的实现的效果一致,但我们使用了对象来封装业务逻辑所需的变量,使用回调函数注册来灵活注册对应的对象方法。

作者:andy,如若转载,请注明出处:https://www.web176.com/goframe/20880.html

(0)
打赏 支付宝 支付宝 微信 微信
andy的头像andy
上一篇 2023年5月18日
下一篇 2023年5月18日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注