mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
完成控制器反射功能测试
This commit is contained in:
@ -3,6 +3,6 @@ package ghttp
|
||||
|
||||
// 控制器接口
|
||||
type Controller interface {
|
||||
Init()
|
||||
Init(*ClientRequest, *ServerResponse)
|
||||
Shut()
|
||||
}
|
||||
|
||||
@ -13,8 +13,10 @@ type ControllerBase struct {
|
||||
}
|
||||
|
||||
// 控制器初始化
|
||||
func (c *ControllerBase) Init() {
|
||||
c.Cookie = NewCookie(c.Request, c.Response)
|
||||
func (c *ControllerBase) Init(r *ClientRequest, w *ServerResponse) {
|
||||
c.Request = r
|
||||
c.Response = w
|
||||
c.Cookie = NewCookie(c.Request, c.Response)
|
||||
if r := c.Cookie.Get("gfsessionid"); r != "" {
|
||||
c.Session = gsession.Get(r)
|
||||
} else {
|
||||
@ -25,6 +27,7 @@ func (c *ControllerBase) Init() {
|
||||
// 控制器结束请求
|
||||
func (c *ControllerBase) Shut() {
|
||||
c.Cookie.Output()
|
||||
c.Response.Output()
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -213,7 +213,7 @@ func (s *Server) handlerKey(domain, method, pattern string) string {
|
||||
func (s *Server) setHandler(domain, method, pattern string, handler HandlerFunc) {
|
||||
s.hmu.Lock()
|
||||
defer s.hmu.Unlock()
|
||||
if method == "all" {
|
||||
if method == gDEFAULT_METHOD {
|
||||
s.handlerMap[s.handlerKey(domain, "GET", pattern)] = handler
|
||||
s.handlerMap[s.handlerKey(domain, "PUT", pattern)] = handler
|
||||
s.handlerMap[s.handlerKey(domain, "POST", pattern)] = handler
|
||||
@ -280,7 +280,7 @@ func (s *Server)bindHandlerByMap(m HandlerMap) error {
|
||||
// 绑定方法,pattern支持http method
|
||||
// pattern的格式形如:/user/list, put:/user, delete:/user
|
||||
func (s *Server)BindMethod(pattern string, c Controller, method string) error {
|
||||
return s.bindHandler(pattern, HandlerFunc{reflect.ValueOf(c).Type(), method})
|
||||
return s.bindHandler(pattern, HandlerFunc{reflect.ValueOf(c).Elem().Type(), method})
|
||||
}
|
||||
|
||||
// 绑定控制器,控制器需要实现gmvc.Controller接口
|
||||
@ -301,8 +301,7 @@ func (s *Server)BindController(uri string, c Controller) error {
|
||||
}
|
||||
key += strings.ToLower(string(name[i]))
|
||||
}
|
||||
m[key] = HandlerFunc{t, name}
|
||||
m[key] = HandlerFunc{v.Elem().Type(), name}
|
||||
}
|
||||
//fmt.Println(m)
|
||||
return s.bindHandlerByMap(m)
|
||||
}
|
||||
|
||||
@ -3,6 +3,11 @@ package ghttp
|
||||
import (
|
||||
"sync"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
gDEFAULT_MAX_AGE = 86400
|
||||
)
|
||||
|
||||
// cookie对象
|
||||
@ -41,8 +46,13 @@ func (c *Cookie) init() {
|
||||
}
|
||||
}
|
||||
|
||||
// 设置cookie
|
||||
func (c *Cookie) Set(key, value, domain, path string, maxage int) {
|
||||
// 设置cookie,使用默认参数
|
||||
func (c *Cookie) Set(key, value string) {
|
||||
c.SetCookie(key, value, strings.Split(c.request.Host, ":")[0], "/", gDEFAULT_MAX_AGE)
|
||||
}
|
||||
|
||||
// 设置cookie,带详细cookie参数
|
||||
func (c *Cookie) SetCookie(key, value, domain, path string, maxage int) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
c.data[key] = CookieItem {
|
||||
|
||||
@ -37,11 +37,8 @@ func (s *Server)handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// 初始化控制器
|
||||
func (s *Server)initController(h *HandlerFunc, r *ClientRequest, w *ServerResponse) {
|
||||
c := reflect.Indirect(reflect.New(h.ctype)).Elem()
|
||||
fmt.Println(c.String())
|
||||
c.FieldByName("Request").Set(reflect.ValueOf(r))
|
||||
c.FieldByName("Response").Set(reflect.ValueOf(w))
|
||||
c.MethodByName("Init").Call(nil)
|
||||
c := reflect.New(h.ctype)
|
||||
c.MethodByName("Init").Call([]reflect.Value{reflect.ValueOf(r), reflect.ValueOf(w)})
|
||||
c.MethodByName(h.fname).Call(nil)
|
||||
c.MethodByName("Shut").Call(nil)
|
||||
}
|
||||
|
||||
@ -3,12 +3,15 @@ package ghttp
|
||||
import (
|
||||
"net/http"
|
||||
"gitee.com/johng/gf/g/encoding/gjson"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// 服务端请求返回对象
|
||||
type ServerResponse struct {
|
||||
http.ResponseWriter
|
||||
bufmu sync.RWMutex // 缓冲区互斥锁
|
||||
server *Server // 所属Server对象
|
||||
buffer []byte // 每个请求的返回数据缓冲区
|
||||
}
|
||||
|
||||
// 返回的固定JSON数据结构
|
||||
@ -20,20 +23,24 @@ type ResponseJson struct {
|
||||
|
||||
// 返回信息(byte)
|
||||
func (r *ServerResponse) Write(content []byte) {
|
||||
r.ResponseWriter.Write(content)
|
||||
r.bufmu.Lock()
|
||||
defer r.bufmu.Unlock()
|
||||
r.buffer = append(r.buffer, content...)
|
||||
}
|
||||
|
||||
// 返回信息(string)
|
||||
func (r *ServerResponse) WriteString(content string) {
|
||||
r.Write([]byte(content))
|
||||
r.bufmu.Lock()
|
||||
defer r.bufmu.Unlock()
|
||||
r.buffer = append(r.buffer, content...)
|
||||
}
|
||||
|
||||
// 返回固定格式的json
|
||||
func (r *ServerResponse) WriteJson(result int, message string, data interface{}) {
|
||||
if r.Header().Get("Content-Type") == "" {
|
||||
r.Header().Set("Content-Type", "application/json")
|
||||
}
|
||||
r.Write([]byte(gjson.Encode(ResponseJson{ result, message, data })))
|
||||
r.Header().Set("Content-Type", "application/json")
|
||||
r.bufmu.Lock()
|
||||
defer r.bufmu.Unlock()
|
||||
r.buffer = append(r.buffer, gjson.Encode(ResponseJson{ result, message, data })...)
|
||||
}
|
||||
|
||||
// 返回内容编码
|
||||
@ -41,3 +48,9 @@ func (r *ServerResponse) WriteHeaderEncoding(encoding string) {
|
||||
r.Header().Set("Content-Type", "text/plain; charset=" + encoding)
|
||||
}
|
||||
|
||||
// 输出缓冲区数据到客户端
|
||||
func (r *ServerResponse) Output() {
|
||||
r.bufmu.RLock()
|
||||
defer r.bufmu.RUnlock()
|
||||
r.ResponseWriter.Write(r.buffer)
|
||||
}
|
||||
|
||||
@ -21,8 +21,9 @@ func init() {
|
||||
}
|
||||
|
||||
// 定义操作逻辑
|
||||
func (cu *ControllerUser) Info() {
|
||||
cu.Response.Write([]byte("user information page"))
|
||||
func (c *ControllerUser) Info() {
|
||||
c.Cookie.Set("name", "john2")
|
||||
c.Response.Write([]byte("user information page"))
|
||||
//t, err := template.New("test").Funcs(template.FuncMap{"add": Add}).Parse(gfile.GetContents("/home/john/Workspace/Go/GOPATH/src/gitee.com/johng/gf/geg/frame/mvc/view/user/info.tpl"))
|
||||
//if err != nil {
|
||||
// fmt.Println(err)
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package main
|
||||
import (
|
||||
"reflect"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
@ -7,22 +8,13 @@ type B struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func (b B) Get() {
|
||||
fmt.Printf("b addr:%p\n", &b)
|
||||
}
|
||||
func main() {
|
||||
b := B{}
|
||||
b.Get()
|
||||
b.Get()
|
||||
func(b *B) Test() {
|
||||
|
||||
return
|
||||
//view := gmvc.NewView("/home/john/Workspace/Go/GOPATH/src/gitee.com/johng/gf/geg/frame/mvc/view/user/")
|
||||
//tpl, _ := view.Template("info")
|
||||
//tpl.BindFunc("add", add)
|
||||
//fmt.Println(tpl.Parse(nil))
|
||||
////t, err := template.New("text").Funcs(template.FuncMap{"add":add}).Parse(`{{add 1 2}}`)
|
||||
////if err != nil {
|
||||
//// panic(err)
|
||||
////}
|
||||
////t.Execute(os.Stdout, u)
|
||||
}
|
||||
|
||||
func main() {
|
||||
b := &B{}
|
||||
t := reflect.ValueOf(b).Elem().Type()
|
||||
//n := reflect.New(t)
|
||||
fmt.Println(t.NumMethod())
|
||||
}
|
||||
Reference in New Issue
Block a user