diff --git a/README.MD b/README.MD index 9f983144f..0560c9467 100644 --- a/README.MD +++ b/README.MD @@ -35,8 +35,8 @@ import "gitee.com/johng/gf/g/xxx/xxx" │   │   └── gurl URL │   │   │   ├── frame 【开发框架】 - │   │   ├── gconfig 通用配置管理 - │   │   ├── ginstance 单例对象管理 + │   │   ├── gcfg 通用配置管理 + │   │   ├── gins 单例对象管理 │   │   └── gmvc MVC模式封装基类 │   │   │   ├── net 【网络通信】 @@ -52,7 +52,7 @@ import "gitee.com/johng/gf/g/xxx/xxx" │   │   │   ├── os 【系统管理】 │   │   ├── gcache 缓存管理 - │   │   ├── gconsole 命令行控制 + │   │   ├── gcmd 命令行控制 │   │   ├── genv 环境变量 │   │   ├── gfile 文件管理 │   │   ├── gfilepool 文件指针池 diff --git a/g/container/gmap/int_interface_map.go b/g/container/gmap/int_interface_map.go index 484927ec0..5e6ae20b9 100644 --- a/g/container/gmap/int_interface_map.go +++ b/g/container/gmap/int_interface_map.go @@ -56,6 +56,41 @@ func (this *IntInterfaceMap) Get(key int) (interface{}) { return val } +func (this *IntInterfaceMap) GetInt(key int) int { + if r := this.Get(key); r != nil { + return r.(int) + } + return 0 +} + +func (this *IntInterfaceMap) GetUint (key int) uint { + if r := this.Get(key); r != nil { + return r.(uint) + } + return 0 +} + +func (this *IntInterfaceMap) GetFloat32 (key int) float32 { + if r := this.Get(key); r != nil { + return r.(float32) + } + return 0 +} + +func (this *IntInterfaceMap) GetFloat64 (key int) float64 { + if r := this.Get(key); r != nil { + return r.(float64) + } + return 0 +} + +func (this *IntInterfaceMap) GetString (key int) string { + if r := this.Get(key); r != nil { + return r.(string) + } + return "" +} + // 删除键值对 func (this *IntInterfaceMap) Remove(key int) { this.Lock() diff --git a/g/container/gmap/interface_interface_map.go b/g/container/gmap/interface_interface_map.go index ccd6eef48..c28904fdc 100644 --- a/g/container/gmap/interface_interface_map.go +++ b/g/container/gmap/interface_interface_map.go @@ -56,6 +56,41 @@ func (this *InterfaceInterfaceMap) Get(key interface{}) (interface{}) { return val } +func (this *InterfaceInterfaceMap) GetInt(key interface{}) int { + if r := this.Get(key); r != nil { + return r.(int) + } + return 0 +} + +func (this *InterfaceInterfaceMap) GetUint (key interface{}) uint { + if r := this.Get(key); r != nil { + return r.(uint) + } + return 0 +} + +func (this *InterfaceInterfaceMap) GetFloat32 (key interface{}) float32 { + if r := this.Get(key); r != nil { + return r.(float32) + } + return 0 +} + +func (this *InterfaceInterfaceMap) GetFloat64 (key interface{}) float64 { + if r := this.Get(key); r != nil { + return r.(float64) + } + return 0 +} + +func (this *InterfaceInterfaceMap) GetString (key interface{}) string { + if r := this.Get(key); r != nil { + return r.(string) + } + return "" +} + // 删除键值对 func (this *InterfaceInterfaceMap) Remove(key interface{}) { this.Lock() diff --git a/g/container/gmap/string_interface_map.go b/g/container/gmap/string_interface_map.go index 5004ed81c..8e5d76d41 100644 --- a/g/container/gmap/string_interface_map.go +++ b/g/container/gmap/string_interface_map.go @@ -56,6 +56,41 @@ func (this *StringInterfaceMap) Get(key string) interface{} { return val } +func (this *StringInterfaceMap) GetInt(key string) int { + if r := this.Get(key); r != nil { + return r.(int) + } + return 0 +} + +func (this *StringInterfaceMap) GetUint (key string) uint { + if r := this.Get(key); r != nil { + return r.(uint) + } + return 0 +} + +func (this *StringInterfaceMap) GetFloat32 (key string) float32 { + if r := this.Get(key); r != nil { + return r.(float32) + } + return 0 +} + +func (this *StringInterfaceMap) GetFloat64 (key string) float64 { + if r := this.Get(key); r != nil { + return r.(float64) + } + return 0 +} + +func (this *StringInterfaceMap) GetString (key string) string { + if r := this.Get(key); r != nil { + return r.(string) + } + return "" +} + // 删除键值对 func (this *StringInterfaceMap) Remove(key string) { this.Lock() diff --git a/g/container/gmap/uint_interface_map.go b/g/container/gmap/uint_interface_map.go index 645ce5e03..d9319f20f 100644 --- a/g/container/gmap/uint_interface_map.go +++ b/g/container/gmap/uint_interface_map.go @@ -56,6 +56,41 @@ func (this *UintInterfaceMap) Get(key uint) (interface{}) { return val } +func (this *UintInterfaceMap) GetInt(key uint) int { + if r := this.Get(key); r != nil { + return r.(int) + } + return 0 +} + +func (this *UintInterfaceMap) GetUint (key uint) uint { + if r := this.Get(key); r != nil { + return r.(uint) + } + return 0 +} + +func (this *UintInterfaceMap) GetFloat32 (key uint) float32 { + if r := this.Get(key); r != nil { + return r.(float32) + } + return 0 +} + +func (this *UintInterfaceMap) GetFloat64 (key uint) float64 { + if r := this.Get(key); r != nil { + return r.(float64) + } + return 0 +} + +func (this *UintInterfaceMap) GetString (key uint) string { + if r := this.Get(key); r != nil { + return r.(string) + } + return "" +} + // 删除键值对 func (this *UintInterfaceMap) Remove(key uint) { this.Lock() diff --git a/g/frame/gconfig/gconfig.go b/g/frame/gcfg/gcfg.go similarity index 97% rename from g/frame/gconfig/gconfig.go rename to g/frame/gcfg/gcfg.go index 5aa928905..b1f1325f8 100644 --- a/g/frame/gconfig/gconfig.go +++ b/g/frame/gcfg/gcfg.go @@ -4,8 +4,8 @@ // If a copy of the MIT was not distributed with this file, // You can obtain one at https://gitee.com/johng/gf. -// 全局配置管理对象 -package gconfig +// 全局配置管理对象,配置文件为json文件 +package gcfg import ( "gitee.com/johng/gf/g/os/gfile" diff --git a/g/frame/ginstance/ginstance.go b/g/frame/gins/gins.go similarity index 93% rename from g/frame/ginstance/ginstance.go rename to g/frame/gins/gins.go index 3ad1f03c7..fa1581f7a 100644 --- a/g/frame/ginstance/ginstance.go +++ b/g/frame/gins/gins.go @@ -6,17 +6,17 @@ // 单例对象管理工具 // 框架内置了一些核心对象,并且可以通过Set和Get方法实现IoC以及对内置核心对象的自定义替换 -package ginstance +package gins import ( "strconv" + "gitee.com/johng/gf/g/os/gcmd" "gitee.com/johng/gf/g/os/glog" "gitee.com/johng/gf/g/os/genv" "gitee.com/johng/gf/g/os/gview" "gitee.com/johng/gf/g/os/gfile" - "gitee.com/johng/gf/g/os/gconsole" + "gitee.com/johng/gf/g/frame/gcfg" "gitee.com/johng/gf/g/database/gdb" - "gitee.com/johng/gf/g/frame/gconfig" "gitee.com/johng/gf/g/container/gmap" ) @@ -45,7 +45,7 @@ func View() *gview.View { if result != nil { return result.(*gview.View) } else { - path := gconsole.Option.Get("viewpath") + path := gcmd.Option.Get("viewpath") if path == "" { path = genv.Get("viewpath") if path == "" { @@ -61,19 +61,19 @@ func View() *gview.View { // 核心对象:Config // 配置文件目录查找依次为:启动参数cfgpath、当前程序运行目录 -func Config() *gconfig.Config { +func Config() *gcfg.Config { result := Get(FRAME_CORE_COMPONENT_NAME_CONFIG) if result != nil { - return result.(*gconfig.Config) + return result.(*gcfg.Config) } else { - path := gconsole.Option.Get("cfgpath") + path := gcmd.Option.Get("cfgpath") if path == "" { path = genv.Get("cfgpath") if path == "" { path = gfile.SelfDir() } } - config := gconfig.New(path) + config := gcfg.New(path) Set(FRAME_CORE_COMPONENT_NAME_CONFIG, config) return config } diff --git a/g/frame/gmvc/controller.go b/g/frame/gmvc/controller.go index e0a26be25..c9aeb84bb 100644 --- a/g/frame/gmvc/controller.go +++ b/g/frame/gmvc/controller.go @@ -12,10 +12,6 @@ import ( "gitee.com/johng/gf/g/net/gsession" ) -const ( - gDEFAULT_SESSION_ID_NAME = "gfsessionid" -) - // 控制器基类 type Controller struct { Server *ghttp.Server // Web Server对象 @@ -33,19 +29,12 @@ func (c *Controller) Init(s *ghttp.Server, r *ghttp.ClientRequest, w *ghttp.Serv c.Response = w c.Cookie = ghttp.NewCookie(c.Request, c.Response) c.View = NewView(c) - if r := c.Cookie.Get(gDEFAULT_SESSION_ID_NAME); r != "" { - c.Session = gsession.Get(r) - } else { - c.Session = gsession.Get(gsession.Id()) - } + c.Session = gsession.Get(c.Cookie.SessionId()) } // 控制器结束请求接口方法 func (c *Controller) Shut() { - if c.Cookie.Get(gDEFAULT_SESSION_ID_NAME) == "" { - c.Cookie.Set(gDEFAULT_SESSION_ID_NAME, c.Session.Id()) - } - c.Cookie.Output() + } diff --git a/g/frame/gmvc/view.go b/g/frame/gmvc/view.go index fea5aba77..bdb449f74 100644 --- a/g/frame/gmvc/view.go +++ b/g/frame/gmvc/view.go @@ -10,7 +10,7 @@ import ( "sync" "html/template" "gitee.com/johng/gf/g/os/gview" - "gitee.com/johng/gf/g/frame/ginstance" + "gitee.com/johng/gf/g/frame/gins" ) // 视图对象(一个请求一个视图对象,用完即销毁) @@ -25,7 +25,7 @@ type View struct { func NewView(c *Controller) *View { return &View{ ctl : c, - view : ginstance.View(), + view : gins.View(), data : make(map[string]interface{}), } } diff --git a/g/g.go b/g/g.go deleted file mode 100644 index d0f1a18b3..000000000 --- a/g/g.go +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved. -// -// This Source Code Form is subject to the terms of the MIT License. -// If a copy of the MIT was not distributed with this file, -// You can obtain one at https://gitee.com/johng/gf. -// -package g - -import ( - "gitee.com/johng/gf/g/net/gtcp" - "gitee.com/johng/gf/g/net/gudp" - "gitee.com/johng/gf/g/net/ghttp" -) - -// 单例HTTP Server -// 框架支持多服务器对象,通过传入不同的name进行区分 -// HTTPServer启动时支持命令行参数: xxx --cfgpath=xxx --viewpath=xxx -func HTTPServer(names...string) *ghttp.Server { - return ghttp.GetServer(names...) -} - -// 单例TCP Server -// 框架支持多服务器对象,通过传入不同的name进行区分 -func TCPServer(names...string) *gtcp.Server { - return gtcp.GetServer(names...) -} - - -// 单例HTTP Server -// 框架支持多服务器对象,通过传入不同的name进行区分 -func UDPServer(names...string) *gudp.Server { - return gudp.GetServer(names...) -} diff --git a/g/net/ghttp/http_client.go b/g/net/ghttp/http_client.go index 4e54c8563..3051995a2 100644 --- a/g/net/ghttp/http_client.go +++ b/g/net/ghttp/http_client.go @@ -21,7 +21,8 @@ type Client struct { // 请求对象 type ClientRequest struct { http.Request - getvals *url.Values // GET参数 + id uint64 // 请求id(唯一) + getvals *url.Values // GET参数 } // 客户端请求结果对象 diff --git a/g/net/ghttp/http_client_request.go b/g/net/ghttp/http_client_request.go index 1d534b331..1c68630ef 100644 --- a/g/net/ghttp/http_client_request.go +++ b/g/net/ghttp/http_client_request.go @@ -11,6 +11,11 @@ import ( "strconv" ) +// 获取当前请求的id +func (r *ClientRequest) Id() uint64 { + return r.id +} + // 获得指定名称的get参数列表 func (r *ClientRequest) GetQuery(k string) []string { if r.getvals == nil { diff --git a/g/net/ghttp/http_server.go b/g/net/ghttp/http_server.go index 1f9c8456a..f85add25c 100644 --- a/g/net/ghttp/http_server.go +++ b/g/net/ghttp/http_server.go @@ -20,13 +20,14 @@ import ( "gitee.com/johng/gf/g/util/gutil" "gitee.com/johng/gf/g/container/gmap" "gitee.com/johng/gf/g/net/grouter" + "sync/atomic" ) const ( - gDEFAULT_SERVER = "default" - gDEFAULT_DOMAIN = "default" - gDEFAULT_METHOD = "all" - gHTTP_METHODS = "GET,PUT,POST,DELETE,PATCH,HEAD,CONNECT,OPTIONS,TRACE" + gDEFAULT_SERVER = "default" + gDEFAULT_DOMAIN = "default" + gDEFAULT_METHOD = "all" + gHTTP_METHODS = "GET,PUT,POST,DELETE,PATCH,HEAD,CONNECT,OPTIONS,TRACE" ) // http server结构体 @@ -36,6 +37,7 @@ type Server struct { server http.Server // 底层http server对象 config ServerConfig // 配置对象 status int8 // 当前服务器状态(0:未启动,1:运行中) + served uint64 // 已服务的请求数(递增) handlerMap HandlerMap // 所有注册的回调函数 methodsMap map[string]bool // 所有支持的HTTP Method Router *grouter.Router // 路由管理对象 @@ -245,6 +247,11 @@ func (s *Server)SetServerRoot(root string) error { return nil } +// 服务请求数原子递增 +func (s *Server) increServed() uint64 { + return atomic.AddUint64(&s.served, 1) +} + // 生成回调方法查询的Key func (s *Server) handlerKey(domain, method, pattern string) string { return strings.ToUpper(method) + ":" + pattern + "@" + strings.ToLower(domain) diff --git a/g/net/ghttp/http_server_cookie.go b/g/net/ghttp/http_server_cookie.go index 14a72c582..92f1265ce 100644 --- a/g/net/ghttp/http_server_cookie.go +++ b/g/net/ghttp/http_server_cookie.go @@ -3,20 +3,24 @@ // This Source Code Form is subject to the terms of the MIT License. // If a copy of the MIT was not distributed with this file, // You can obtain one at https://gitee.com/johng/gf. - +// +// HTTP Cookie管理对象, +// 由于Cookie是和HTTP请求挂钩的,因此被包含到 ghttp 包中进行管理 package ghttp import ( "sync" + "time" "strings" "net/http" - "time" "gitee.com/johng/gf/g/os/gtime" + "gitee.com/johng/gf/g/container/gmap" ) const ( - gDEFAULT_PATH = "/" // 默认path - gDEFAULT_MAX_AGE = 86400 // 默认cookie有效期 + gDEFAULT_PATH = "/" // 默认path + gDEFAULT_MAX_AGE = 86400 // 默认cookie有效期 + SESSION_ID_NAME = "gfsessionid" // 默认存放Cookie中的SessionId名称 ) // cookie对象 @@ -31,23 +35,43 @@ type Cookie struct { // cookie项 type CookieItem struct { value string - domain string - path string - expire int //过期时间 + domain string // 有效域名 + path string // 有效路径 + expire int // 过期时间 } -// 初始化cookie对象 +// 包含所有当前服务器正在服务的Cookie +var cookies = gmap.NewUintInterfaceMap() + +// 创建一个cookie对象,与传入的请求对应 func NewCookie(r *ClientRequest, w *ServerResponse) *Cookie { - c := &Cookie{ + if r := GetCookie(r.Id()); r != nil { + return r + } + c := &Cookie { data : make(map[string]CookieItem), domain : defaultDomain(r), request : r, response : w, } c.init() + cookies.Set(uint(r.Id()), c) return c } +// 获取一个已经存在的Cookie对象 +func GetCookie(requestid uint64) *Cookie { + if r := cookies.Get(uint(requestid)); r != nil { + return r.(*Cookie) + } + return nil +} + +// 请求完毕后删除已经存在的Cookie对象 +func RemoveCookie(requestid uint64) { + cookies.Remove(uint(requestid)) +} + // 获取默认的domain参数 func defaultDomain(r *ClientRequest) string { return strings.Split(r.Host, ":")[0] @@ -64,6 +88,16 @@ func (c *Cookie) init() { } } +// 获取SessionId +func (c *Cookie) SessionId() string { + return c.Get(SESSION_ID_NAME) +} + +// 设置SessionId +func (c *Cookie) SetSessionId(sessionid string) { + c.Set(SESSION_ID_NAME, sessionid) +} + // 设置cookie,使用默认参数 func (c *Cookie) Set(key, value string) { c.SetCookie(key, value, c.domain, gDEFAULT_PATH, gDEFAULT_MAX_AGE) @@ -92,19 +126,36 @@ func (c *Cookie) Get(key string) string { return "" } +// 标记该cookie在对应的域名和路径失效 // 删除cookie的重点是需要通知浏览器客户端cookie已过期 -func (c *Cookie) Remove(key string) { - c.SetCookie(key, "", c.domain, gDEFAULT_PATH, -86400) +func (c *Cookie) Remove(key, domain, path string) { + c.SetCookie(key, "", domain, path, -86400) } // 输出到客户端 func (c *Cookie) Output() { c.mu.RLock() defer c.mu.RUnlock() + // 自动更新SessionId的过期时间 + sitem := c.data[SESSION_ID_NAME] + minex := int(gtime.Second()) + gDEFAULT_MAX_AGE + if sitem.expire < minex { + sitem.expire = minex + c.data[SESSION_ID_NAME] = sitem + } for k, v := range c.data { if v.expire == 0 { continue } - http.SetCookie(c.response.ResponseWriter, &http.Cookie{Name: k, Value: v.value, Domain: v.domain, Path: v.path, Expires: time.Unix(int64(v.expire), 0)}) + http.SetCookie( + c.response.ResponseWriter, + &http.Cookie { + Name : k, + Value : v.value, + Domain : v.domain, + Path : v.path, + Expires : time.Unix(int64(v.expire), 0), + }, + ) } } diff --git a/g/net/ghttp/http_server_handler.go b/g/net/ghttp/http_server_handler.go index 5165bca56..fe02a6dc7 100644 --- a/g/net/ghttp/http_server_handler.go +++ b/g/net/ghttp/http_server_handler.go @@ -10,13 +10,14 @@ import ( "os" "fmt" "sort" + "reflect" "strings" "net/url" "net/http" "path/filepath" "gitee.com/johng/gf/g/os/gfile" + "gitee.com/johng/gf/g/net/gsession" "gitee.com/johng/gf/g/encoding/ghtml" - "reflect" ) // 默认HTTP Server处理入口,http包底层默认使用了gorutine异步处理请求,所以这里不再异步执行 @@ -38,6 +39,7 @@ func (s *Server)handleRequest(w http.ResponseWriter, r *http.Request) { // 构造请求/返回参数对象 request := &ClientRequest{} response := &ServerResponse{} + request.id = s.increServed() request.Request = *r response.ResponseWriter = w if h := s.getHandler(gDEFAULT_DOMAIN, r.Method, r.URL.Path); h != nil { @@ -53,12 +55,22 @@ func (s *Server)handleRequest(w http.ResponseWriter, r *http.Request) { // 初始化控制器 func (s *Server)callHandler(h *HandlerItem, r *ClientRequest, w *ServerResponse) { + // 会话处理,每个请求必定有一个sessionid + cookie := NewCookie(r, w) + sessionid := cookie.SessionId() + if sessionid == "" { + sessionid = gsession.Id() + cookie.SetSessionId(sessionid) + } + // 请求处理 if h.faddr == nil { + // 新建一个控制器对象处理请求 c := reflect.New(h.ctype) c.MethodByName("Init").Call([]reflect.Value{reflect.ValueOf(s), reflect.ValueOf(r), reflect.ValueOf(w)}) c.MethodByName(h.fname).Call(nil) c.MethodByName("Shut").Call(nil) } else { + // 直接调用注册的方法处理请求 h.faddr(s, r, w) } // 路由规则打包 @@ -66,8 +78,15 @@ func (s *Server)callHandler(h *HandlerItem, r *ClientRequest, w *ServerResponse) w.ClearBuffer() w.Write(buffer) } + + // 输出Cookie + cookie.Output() + // 输出缓冲区 w.OutputBuffer() + + // 删除当前会话的Cookie + RemoveCookie(r.Id()) } // 处理静态文件请求 diff --git a/g/net/gsession/gsession.go b/g/net/gsession/gsession.go index bf5e362ea..11281c522 100644 --- a/g/net/gsession/gsession.go +++ b/g/net/gsession/gsession.go @@ -7,13 +7,13 @@ package gsession import ( + "sync" "strconv" "strings" "gitee.com/johng/gf/g/os/gtime" + "gitee.com/johng/gf/g/os/gcache" "gitee.com/johng/gf/g/util/grand" "gitee.com/johng/gf/g/container/gmap" - "gitee.com/johng/gf/g/os/gcache" - "sync" ) const ( @@ -38,8 +38,8 @@ func Get(sessionid string) *Session { if r := gcache.Get(cacheKey(sessionid)); r != nil { return r.(*Session) } - s := &Session{ - id : Id(), + s := &Session { + id : sessionid, data : gmap.NewStringInterfaceMap(), expire : DEFAULT_EXPIRE_TIME, } @@ -84,6 +84,38 @@ func (s *Session) Get (k string) interface{} { return s.data.Get(k) } +func (s *Session) GetInt (k string) int { + go s.updateExpire() + if r := s.data.Get(k); r != nil { + return r.(int) + } + return 0 +} + +func (s *Session) GetUint (k string) uint { + go s.updateExpire() + if r := s.data.Get(k); r != nil { + return r.(uint) + } + return 0 +} + +func (s *Session) GetFloat32 (k string) float32 { + go s.updateExpire() + if r := s.data.Get(k); r != nil { + return r.(float32) + } + return 0 +} + +func (s *Session) GetFloat64 (k string) float64 { + go s.updateExpire() + if r := s.data.Get(k); r != nil { + return r.(float64) + } + return 0 +} + // 获取session(字符串) func (s *Session) GetString (k string) string { go s.updateExpire() @@ -101,5 +133,6 @@ func (s *Session) Remove (k string) { // 更新过期时间 func (s *Session) updateExpire() { - gcache.Set(cacheKey(s.id), s, int64(s.expire*1000)) + //gcache.Set(cacheKey(s.id), s, int64(s.expire*1000)) + gcache.Set(cacheKey(s.id), s, 0) } diff --git a/g/os/gconsole/gconsole.go b/g/os/gcmd/gcmd.go similarity index 74% rename from g/os/gconsole/gconsole.go rename to g/os/gcmd/gcmd.go index 90fdbbb24..62d943514 100644 --- a/g/os/gconsole/gconsole.go +++ b/g/os/gcmd/gcmd.go @@ -3,8 +3,8 @@ // This Source Code Form is subject to the terms of the MIT License. // If a copy of the MIT was not distributed with this file, // You can obtain one at https://gitee.com/johng/gf. - -package gconsole +// +package gcmd import ( "os" @@ -15,18 +15,18 @@ import ( ) // 命令行参数列表 -type gConsoleValue struct { +type gCmdValue struct { values []string } // 命令行选项列表 -type gConsoleOption struct { +type gCmdOption struct { options map[string]string } // 终端管理对象(全局) -var Value gConsoleValue // console终端参数-命令参数列表 -var Option gConsoleOption // console终端参数-选项参数列表 +var Value = &gCmdValue{} // 终端参数-命令参数列表 +var Option = &gCmdOption{} // 终端参数-选项参数列表 var cmdFuncMap = make(map[string]func()) // 终端命令及函数地址对应表 // 检查并初始化console参数,在包加载的时候触发 @@ -45,17 +45,17 @@ func init() { } // 返回所有的命令行参数values -func (c gConsoleValue) GetAll() []string { +func (c *gCmdValue) GetAll() []string { return c.values } // 返回所有的命令行参数options -func (c gConsoleOption) GetAll() map[string]string { +func (c *gCmdOption) GetAll() map[string]string { return c.options } // 获得一条指定索引位置的value参数 -func (c gConsoleValue) Get(index uint8) string { +func (c *gCmdValue) Get(index uint8) string { if index < uint8(len(c.values)) { return c.values[index] } @@ -63,9 +63,8 @@ func (c gConsoleValue) Get(index uint8) string { } // 类型转换 -func (c gConsoleValue) GetInt(key uint8) int { - v := c.Get(key) - if v != "" { +func (c *gCmdValue) GetInt(key uint8) int { + if v := c.Get(key); v != "" { i, _ := strconv.Atoi(v) return i } @@ -73,7 +72,7 @@ func (c gConsoleValue) GetInt(key uint8) int { } // 类型转换bool -func (c gConsoleValue) GetBool(key uint8) bool { +func (c *gCmdValue) GetBool(key uint8) bool { v := c.Get(key) v = strings.ToLower(v) if v != "" && v != "0" && v != "false" { @@ -82,19 +81,17 @@ func (c gConsoleValue) GetBool(key uint8) bool { return false } -// 获得一条指定索引位置的option参数 -func (c gConsoleOption) Get(key string) string { - option, ok := c.options[key] - if ok { +// 获得一条指定索引位置的option参数; +func (c *gCmdOption) Get(key string) string { + if option, ok := c.options[key]; ok { return option } return "" } // 类型转换int -func (c gConsoleOption) GetInt(key string) int { - v := c.Get(key) - if v != "" { +func (c *gCmdOption) GetInt(key string) int { + if v := c.Get(key); v != "" { i, _ := strconv.Atoi(v) return i } @@ -102,7 +99,7 @@ func (c gConsoleOption) GetInt(key string) int { } // 类型转换bool -func (c gConsoleOption) GetBool(key string) bool { +func (c *gCmdOption) GetBool(key string) bool { v := c.Get(key) v = strings.ToLower(v) if v != "" && v != "0" && v != "false" { @@ -114,8 +111,7 @@ func (c gConsoleOption) GetBool(key string) bool { // 绑定命令行参数及对应的命令函数,注意参数是函数的内存地址 // 如果操作失败返回错误信息 func BindHandle (cmd string, f func()) error { - _, ok := cmdFuncMap[cmd] - if ok { + if _, ok := cmdFuncMap[cmd]; ok { return errors.New("duplicated handle for command:" + cmd) } else { cmdFuncMap[cmd] = f @@ -125,8 +121,7 @@ func BindHandle (cmd string, f func()) error { // 执行命令对应的函数 func RunHandle (cmd string) error { - handle, ok := cmdFuncMap[cmd] - if ok { + if handle, ok := cmdFuncMap[cmd]; ok { handle() return nil } else { @@ -136,8 +131,7 @@ func RunHandle (cmd string) error { // 自动识别命令参数并执行命令参数对应的函数 func AutoRun () error { - cmd := Value.Get(1); - if cmd != "" { + if cmd := Value.Get(1); cmd != "" { if handle, ok := cmdFuncMap[cmd]; ok { handle() return nil @@ -147,5 +141,4 @@ func AutoRun () error { } else { return errors.New("no command found") } - } diff --git a/geg/frame/mvc/controller/demo/domain.go b/geg/frame/mvc/controller/demo/domain.go new file mode 100644 index 000000000..1aa9e180c --- /dev/null +++ b/geg/frame/mvc/controller/demo/domain.go @@ -0,0 +1,21 @@ +package demo + +import ( + "gitee.com/johng/gf/g/net/ghttp" +) + +type ControllerDomain struct {} + +// 初始化控制器对象,并绑定操作到Web Server +func init() { + // 只有localhost域名下才能访问该对象, + // 对应URL为:http://localhost:8199/test/show + // 通过该地址将无法访问到内容:http://127.0.0.1:8199/test/show + ghttp.GetServer().Domain("localhost").BindObject("/domain", &ControllerDomain{}) +} + +// 用于对象映射 +func (d *ControllerDomain) Show(s *ghttp.Server, r *ghttp.ClientRequest, w *ghttp.ServerResponse) { + w.WriteString("It's show time bibi!") +} + diff --git a/geg/frame/mvc/controller/demo/hello.go b/geg/frame/mvc/controller/demo/hello.go index f8a7fe919..c2c1f923c 100644 --- a/geg/frame/mvc/controller/demo/hello.go +++ b/geg/frame/mvc/controller/demo/hello.go @@ -1,18 +1,31 @@ package demo import ( - "gitee.com/johng/gf/g" "gitee.com/johng/gf/g/net/ghttp" + "gitee.com/johng/gf/g/net/gsession" + "strconv" ) // 初始化控制器对象,并绑定操作到Web Server func init() { // 将URI映射到指定的方法中执行 - g.HTTPServer().BindHandler("/hello", Hello) + ghttp.GetServer().BindHandler("/hello", Hello) } // 用于函数映射 func Hello(s *ghttp.Server, r *ghttp.ClientRequest, w *ghttp.ServerResponse) { - w.WriteString("Hello World!") + cookie := ghttp.GetCookie(r.Id()) + session := gsession.Get(cookie.SessionId()) + + id := 0 + for i := 0; i < 1; i++ { + if r := session.Get("id"); r != nil { + id = r.(int) + } + id++ + session.Set("id", id) + } + + w.WriteString("Hello World!" + strconv.Itoa(id)) } \ No newline at end of file diff --git a/geg/frame/mvc/controller/demo/rest.go b/geg/frame/mvc/controller/demo/rest.go index 700e9d2ad..d668a0f8e 100644 --- a/geg/frame/mvc/controller/demo/rest.go +++ b/geg/frame/mvc/controller/demo/rest.go @@ -2,7 +2,8 @@ package demo import ( "gitee.com/johng/gf/g/frame/gmvc" - "gitee.com/johng/gf/g" + + "gitee.com/johng/gf/g/net/ghttp" ) // 测试控制器 @@ -12,8 +13,8 @@ type ControllerRest struct { // 初始化控制器对象,并绑定操作到Web Server func init() { - // 控制器公开方法中与HTTP Method方法同名的方法将会绑定映射 - g.HTTPServer().BindControllerRest("/user", &ControllerRest{}) + // 控制器公开方法中与HTTP Method方法同名的方法将会自动绑定映射 + ghttp.GetServer().BindControllerRest("/user", &ControllerRest{}) } // RESTFul - GET diff --git a/geg/frame/mvc/controller/demo/test.go b/geg/frame/mvc/controller/demo/test.go index 2ddc88bfa..037baca85 100644 --- a/geg/frame/mvc/controller/demo/test.go +++ b/geg/frame/mvc/controller/demo/test.go @@ -1,7 +1,7 @@ package demo import ( - "gitee.com/johng/gf/g" + "gitee.com/johng/gf/g/net/ghttp" ) @@ -10,12 +10,12 @@ type T struct {} // 初始化控制器对象,并绑定操作到Web Server func init() { - // 只能通过RESTFul方式访问接口,这里为测试方便,使用的是Get - //g.HTTPServer().BindObject("/test", &T{}) + // 绑定对象,对象中的公开方法将会自动绑定到指定的URI末尾 + // ghttp.GetServer().BindObject("/test", &T{}) // 只有localhost域名下才能访问该对象, // 对应URL为:http://localhost:8199/test/show // 通过该地址将无法访问到内容:http://127.0.0.1:8199/test/show - g.HTTPServer().Domain("localhost").BindObject("/test", &T{}) + ghttp.GetServer().Domain("localhost").BindObject("/test", &T{}) } // 用于对象映射 diff --git a/geg/frame/mvc/controller/demo/user.go b/geg/frame/mvc/controller/demo/user.go index 73d0423a2..d92a025b0 100644 --- a/geg/frame/mvc/controller/demo/user.go +++ b/geg/frame/mvc/controller/demo/user.go @@ -1,8 +1,9 @@ package demo import ( - "gitee.com/johng/gf/g" + "gitee.com/johng/gf/g/frame/gmvc" + "gitee.com/johng/gf/g/net/ghttp" ) // 定义业务相关的控制器对象, @@ -15,7 +16,7 @@ type ControllerUser struct { func init() { // 绑定控制器到指定URI,所有控制器的公开方法将会映射到指定URI末尾 // 例如该方法执行后,查看效果可访问:http://127.0.0.1:8199/user/info - g.HTTPServer().BindController("/user", &ControllerUser{}) + ghttp.GetServer().BindController("/user", &ControllerUser{}) } // 定义操作逻辑 - 展示模板 diff --git a/geg/frame/mvc/main.go b/geg/frame/mvc/main.go index 1f789e03b..e4161b36b 100644 --- a/geg/frame/mvc/main.go +++ b/geg/frame/mvc/main.go @@ -1,11 +1,11 @@ package main import ( - "gitee.com/johng/gf/g" + "gitee.com/johng/gf/g/net/ghttp" _ "gitee.com/johng/gf/geg/frame/mvc/controller/demo" ) func main() { - g.HTTPServer().SetPort(8199) - g.HTTPServer().Run() + ghttp.GetServer().SetPort(8199) + ghttp.GetServer().Run() } diff --git a/geg/os/console.go b/geg/os/console.go index afcf5132f..82b4fd429 100644 --- a/geg/os/console.go +++ b/geg/os/console.go @@ -2,7 +2,7 @@ package main import ( "fmt" - "gitee.com/johng/gf/g/os/gconsole" + "gitee.com/johng/gf/g/os/gcmd" ) func doEcho() { @@ -10,12 +10,12 @@ func doEcho() { } func main() { - fmt.Println(gconsole.Value.GetAll()) + fmt.Println(gcmd.Value.GetAll()) - fmt.Println(gconsole.Value.GetIndex(1)) + fmt.Println(gcmd.Value.GetIndex(1)) - gconsole.BindHandle("echo", doEcho) - gconsole.RunHandle("echo") + gcmd.BindHandle("echo", doEcho) + gcmd.RunHandle("echo") - gconsole.AutoRun() + gcmd.AutoRun() } diff --git a/geg/other/test.go b/geg/other/test.go index 018390b5f..156d37f7b 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -1,11 +1,22 @@ package main import ( + "gitee.com/johng/gf/g/net/gsession" "fmt" - "gitee.com/johng/gf/g/util/gvalid" ) func main() { - fmt.Println(gvalid.Check("10.0.0.0", "ip", nil)) + id := 0 + for i := 0; i < 10; i++ { + s := gsession.Get("1") + if r := s.Get("id"); r != nil { + id = r.(int) + } + id++ + s.Set("id", id) + + fmt.Println(id) + } + } \ No newline at end of file