mirror of
https://gitee.com/johng/gf
synced 2026-06-07 10:22:11 +08:00
完善ghttp服务注册
This commit is contained in:
@ -22,9 +22,6 @@ func main() {
|
||||
* [WebServer](https://www.kancloud.cn/johng/gf/494366)
|
||||
* [MVC模式](https://www.kancloud.cn/johng/gf/494367)
|
||||
* [服务注册](https://www.kancloud.cn/johng/gf/494368)
|
||||
* [控制器注册](https://www.kancloud.cn/johng/gf/494369)
|
||||
* [执行对象注册](https://www.kancloud.cn/johng/gf/494370)
|
||||
* [回调函数注册](https://www.kancloud.cn/johng/gf/494371)
|
||||
* [Cookie](https://www.kancloud.cn/johng/gf/494372)
|
||||
* [Session](https://www.kancloud.cn/johng/gf/494373)
|
||||
* [输入输出](https://www.kancloud.cn/johng/gf/494374)
|
||||
|
||||
@ -16,7 +16,7 @@ type Controller struct {
|
||||
Request *ghttp.Request // 请求数据对象
|
||||
Response *ghttp.Response // 返回数据对象(r.Response)
|
||||
Server *ghttp.Server // Web Server对象(r.Server)
|
||||
Cookie *ghttp.Cookie // COOKIE操作对象
|
||||
Cookie *ghttp.Cookie // COOKIE操作对象(r.Cookie)
|
||||
Session *ghttp.Session // SESSION操作对象
|
||||
View *View // 视图对象
|
||||
}
|
||||
|
||||
@ -264,6 +264,7 @@ func (s *Server) setHandler(domain, method, pattern string, hitem HandlerItem) {
|
||||
} else {
|
||||
s.handlerMap[s.handlerKey(domain, method, pattern)] = hitem
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 查询请求处理方法
|
||||
@ -355,6 +356,22 @@ func (s *Server)BindObject(pattern string, obj interface{}) error {
|
||||
return s.bindHandlerByMap(m)
|
||||
}
|
||||
|
||||
// 绑定对象到URI请求处理中,会自动识别方法名称,并附加到对应的URI地址后面
|
||||
// 第三个参数methods支持多个方法注册,多个方法以英文“,”号分隔
|
||||
func (s *Server)BindObjectMethod(pattern string, obj interface{}, methods string) error {
|
||||
m := make(HandlerMap)
|
||||
for _, v := range strings.Split(methods, ",") {
|
||||
method := strings.TrimSpace(v)
|
||||
fval := reflect.ValueOf(obj).MethodByName(method)
|
||||
if !fval.IsValid() {
|
||||
return errors.New("invalid method name:" + method)
|
||||
}
|
||||
key := s.appendMethodNameToUriWithPattern(pattern, method)
|
||||
m[key] = HandlerItem{nil, "", fval.Interface().(func(*Request))}
|
||||
}
|
||||
return s.bindHandlerByMap(m)
|
||||
}
|
||||
|
||||
// 绑定对象到URI请求处理中,会自动识别方法名称,并附加到对应的URI地址后面
|
||||
// 需要注意对象方法的定义必须按照ghttp.HandlerFunc来定义
|
||||
func (s *Server)BindObjectRest(pattern string, obj interface{}) error {
|
||||
@ -417,16 +434,19 @@ func (s *Server)BindControllerRest(pattern string, c Controller) error {
|
||||
return s.bindHandlerByMap(m)
|
||||
}
|
||||
|
||||
// 绑定控制器方法,pattern支持http method
|
||||
// pattern的格式形如:/user/list, put:/user, delete:/user
|
||||
// 这种方式绑定的控制器每一次请求都会初始化一个新的控制器对象进行处理,对应不同的请求会话
|
||||
// 第三个参数methods支持多个方法注册,多个方法以英文“,”号分隔
|
||||
func (s *Server)BindControllerMethod(pattern string, c Controller, methods string) error {
|
||||
for _, method := range strings.Split(methods, ",") {
|
||||
item := HandlerItem{reflect.ValueOf(c).Elem().Type(), strings.TrimSpace(method), nil}
|
||||
if err := s.bindHandlerItem(pattern, item); err != nil {
|
||||
return err
|
||||
m := make(HandlerMap)
|
||||
cval := reflect.ValueOf(c)
|
||||
for _, v := range strings.Split(methods, ",") {
|
||||
ctype := reflect.ValueOf(c).Elem().Type()
|
||||
method := strings.TrimSpace(v)
|
||||
if !cval.MethodByName(method).IsValid() {
|
||||
return errors.New("invalid method name:" + method)
|
||||
}
|
||||
key := s.appendMethodNameToUriWithPattern(pattern, method)
|
||||
m[key] = HandlerItem{ctype, method, nil}
|
||||
}
|
||||
return nil
|
||||
return s.bindHandlerByMap(m)
|
||||
}
|
||||
|
||||
@ -41,7 +41,7 @@ type CookieItem struct {
|
||||
expire int // 过期时间
|
||||
}
|
||||
|
||||
// 包含所有当前服务器正在服务的Cookie
|
||||
// 包含所有当前服务器正在服务的Cookie(每个请求一个Cookie对象)
|
||||
var cookies = gmap.NewUintInterfaceMap()
|
||||
|
||||
// 创建一个cookie对象,与传入的请求对应
|
||||
|
||||
@ -48,8 +48,7 @@ func (d *Domain) BindHandler(pattern string, handler HandlerFunc) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 绑定对象到URI请求处理中,会自动识别方法名称,并附加到对应的URI地址后面
|
||||
// 需要注意对象方法的定义必须按照ghttp.HandlerFunc来定义
|
||||
// 执行对象方法
|
||||
func (d *Domain) BindObject(pattern string, obj interface{}) error {
|
||||
for domain, _ := range d.m {
|
||||
if err := d.s.BindObject(pattern + "@" + domain, obj); err != nil {
|
||||
@ -59,8 +58,17 @@ func (d *Domain) BindObject(pattern string, obj interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 绑定对象到URI请求处理中,会自动识别方法名称,并附加到对应的URI地址后面
|
||||
// 需要注意对象方法的定义必须按照ghttp.HandlerFunc来定义
|
||||
// 执行对象方法注册
|
||||
func (d *Domain) BindObjectMethod(pattern string, obj interface{}, methods string) error {
|
||||
for domain, _ := range d.m {
|
||||
if err := d.s.BindObjectMethod(pattern + "@" + domain, obj, methods); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RESTful执行对象注册
|
||||
func (d *Domain) BindObjectRest(pattern string, obj interface{}) error {
|
||||
for domain, _ := range d.m {
|
||||
if err := d.s.BindObjectRest(pattern + "@" + domain, obj); err != nil {
|
||||
@ -70,7 +78,7 @@ func (d *Domain) BindObjectRest(pattern string, obj interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 绑定控制器
|
||||
// 控制器注册
|
||||
func (d *Domain) BindController(pattern string, c Controller) error {
|
||||
for domain, _ := range d.m {
|
||||
if err := d.s.BindController(pattern + "@" + domain, c); err != nil {
|
||||
@ -80,7 +88,7 @@ func (d *Domain) BindController(pattern string, c Controller) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 绑定控制器(RESTFul)
|
||||
// RESTful控制器注册
|
||||
func (d *Domain) BindControllerRest(pattern string, c Controller) error {
|
||||
for domain, _ := range d.m {
|
||||
if err := d.s.BindControllerRest(pattern + "@" + domain, c); err != nil {
|
||||
@ -90,13 +98,11 @@ func (d *Domain) BindControllerRest(pattern string, c Controller) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 绑定控制器方法
|
||||
// 控制器方法注册
|
||||
func (d *Domain) BindControllerMethod(pattern string, c Controller, methods string) error {
|
||||
for domain, _ := range d.m {
|
||||
for _, method := range strings.Split(methods, ",") {
|
||||
if err := d.s.BindControllerMethod(pattern + "@" + domain, c, strings.TrimSpace(method)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := d.s.BindControllerMethod(pattern + "@" + domain, c, methods); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
30
geg/frame/mvc/controller/demo/object_method.go
Normal file
30
geg/frame/mvc/controller/demo/object_method.go
Normal file
@ -0,0 +1,30 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"gitee.com/johng/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
type ObjectMethod struct {}
|
||||
|
||||
func init() {
|
||||
ghttp.GetServer().BindObjectMethod("/object-method", &ObjectMethod{}, "Show1, Show2, Show3")
|
||||
ghttp.GetServer().Domain("localhost").BindObjectMethod("/object-method", &ObjectMethod{}, "Show4")
|
||||
}
|
||||
|
||||
func (o *ObjectMethod) Show1(r *ghttp.Request) {
|
||||
r.Response.WriteString("show 1")
|
||||
}
|
||||
|
||||
func (o *ObjectMethod) Show2(r *ghttp.Request) {
|
||||
r.Response.WriteString("show 2")
|
||||
}
|
||||
|
||||
func (o *ObjectMethod) Show3(r *ghttp.Request) {
|
||||
r.Response.WriteString("show 3")
|
||||
}
|
||||
|
||||
func (o *ObjectMethod) Show4(r *ghttp.Request) {
|
||||
r.Response.WriteString("show 4")
|
||||
}
|
||||
|
||||
|
||||
@ -2,13 +2,21 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
|
||||
func main() {
|
||||
i := uint(math.MaxUint64)
|
||||
fmt.Println(int(i&0x7fffffffffffffff))
|
||||
fmt.Println(math.MaxInt64)
|
||||
type T struct {
|
||||
name string
|
||||
}
|
||||
|
||||
func (t *T)Test() {
|
||||
fmt.Println(t.name)
|
||||
}
|
||||
|
||||
func main() {
|
||||
t := &T{"john"}
|
||||
//fmt.Printf("%p\n", t.Test)
|
||||
//fmt.Printf("%p\n", reflect.ValueOf(t).MethodByName("Test").Interface().(func()))
|
||||
reflect.ValueOf(t).MethodByName("Test").Interface().(func())()
|
||||
}
|
||||
Reference in New Issue
Block a user