改进cookie, session, ghttp关系,便于调用管理

This commit is contained in:
John 2017-12-30 23:49:55 +08:00
parent a2c5b4617b
commit 63694dc7e7
25 changed files with 387 additions and 135 deletions

View File

@ -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 文件指针池

View File

@ -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()

View File

@ -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()

View File

@ -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()

View File

@ -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()

View File

@ -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"

View File

@ -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
}

View File

@ -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()
}

View File

@ -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{}),
}
}

33
g/g.go
View File

@ -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...)
}

View File

@ -21,6 +21,7 @@ type Client struct {
// 请求对象
type ClientRequest struct {
http.Request
id uint64 // 请求id(唯一)
getvals *url.Values // GET参数
}

View File

@ -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 {

View File

@ -20,6 +20,7 @@ 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 (
@ -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)

View File

@ -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有效期
SESSION_ID_NAME = "gfsessionid" // 默认存放Cookie中的SessionId名称
)
// cookie对象
@ -31,13 +35,19 @@ type Cookie struct {
// cookie项
type CookieItem struct {
value string
domain string
path string
domain string // 有效域名
path string // 有效路径
expire int // 过期时间
}
// 初始化cookie对象
// 包含所有当前服务器正在服务的Cookie
var cookies = gmap.NewUintInterfaceMap()
// 创建一个cookie对象与传入的请求对应
func NewCookie(r *ClientRequest, w *ServerResponse) *Cookie {
if r := GetCookie(r.Id()); r != nil {
return r
}
c := &Cookie {
data : make(map[string]CookieItem),
domain : defaultDomain(r),
@ -45,9 +55,23 @@ func NewCookie(r *ClientRequest, w *ServerResponse) *Cookie {
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),
},
)
}
}

View File

@ -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())
}
// 处理静态文件请求

View File

@ -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 (
@ -39,7 +39,7 @@ func Get(sessionid string) *Session {
return r.(*Session)
}
s := &Session {
id : Id(),
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)
}

View File

@ -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")
}
}

View File

@ -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!")
}

View File

@ -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))
}

View File

@ -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

View File

@ -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{})
}
// 用于对象映射

View File

@ -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{})
}
// 定义操作逻辑 - 展示模板

View File

@ -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()
}

View File

@ -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()
}

View File

@ -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)
}
}