router group developing

This commit is contained in:
John 2018-12-19 14:45:39 +08:00
parent a86f4f8e23
commit 48328ae52c
4 changed files with 136 additions and 36 deletions

View File

@ -21,16 +21,16 @@ import (
// 解析pattern
func (s *Server)parsePattern(pattern string) (domain, method, uri string, err error) {
uri = pattern
uri = strings.TrimSpace(pattern)
domain = gDEFAULT_DOMAIN
method = gDEFAULT_METHOD
if array, err := gregex.MatchString(`([a-zA-Z]+):(.+)`, pattern); len(array) > 1 && err == nil {
method = array[1]
uri = array[2]
if array, err := gregex.MatchString(`(.+):(.+)`, pattern); len(array) > 1 && err == nil {
method = strings.TrimSpace(array[1])
uri = strings.TrimSpace(array[2])
}
if array, err := gregex.MatchString(`(.+)@([\w\.\-]+)`, uri); len(array) > 1 && err == nil {
uri = array[1]
domain = array[2]
if array, err := gregex.MatchString(`(.+)@(.+)`, uri); len(array) > 1 && err == nil {
uri = strings.TrimSpace(array[1])
domain = strings.TrimSpace(array[2])
}
if uri == "" {
err = errors.New("invalid pattern")

View File

@ -0,0 +1,82 @@
// Copyright 2018 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 ghttp
// 分组路由对象
type RouterGroup struct {
server *Server // Web Server
prefix string // URI前缀
}
// 获取分组路由对象
func (s *Server) Group(prefix...string) *RouterGroup {
if len(prefix) > 0 {
return &RouterGroup{
server : s,
prefix : prefix[0],
}
}
return &RouterGroup{}
}
// REST路由注册
func (g *RouterGroup) REST(pattern string, object interface{}) {
}
// 绑定所有的HTTP Method请求方式
func (g *RouterGroup) ALL(pattern string, params...interface{}) {
}
func (g *RouterGroup) GET(pattern string, params...interface{}) {
}
func (g *RouterGroup) PUT(pattern string, params...interface{}) {
}
func (g *RouterGroup) POST(pattern string, params...interface{}) {
}
func (g *RouterGroup) DELETE(pattern string, params...interface{}) {
}
func (g *RouterGroup) PATCH(pattern string, params...interface{}) {
}
func (g *RouterGroup) HEAD(pattern string, params...interface{}) {
}
func (g *RouterGroup) CONNECT(pattern string, params...interface{}) {
}
func (g *RouterGroup) OPTIONS(pattern string, params...interface{}) {
}
func (g *RouterGroup) TRTACE(pattern string, params...interface{}) {
}
// 执行路由绑定
func (g *RouterGroup) bind(method string, pattern string, params...interface{}) {
}
// 判断给定对象是否控制器对象:
// 控制器必须包含以下公开的属性对象Request/Response/Server/Cookie/Session/View.
func (g *RouterGroup) isController(object interface{}) {
}

View File

@ -1,7 +1,33 @@
package main
import "gitee.com/johng/gf/g/util/gtest"
type Registry struct {
Method string
Uri string
Handler interface{}
Object interface{}
}
func BindGroup(group string, routers []Registry) {
}
type User struct { }
type Order struct { }
type Product struct { }
func HookFunc() {
}
func main() {
gtest.Assert(1, 2)
user := new(User)
BindGroup("/api", []Registry {
{"ALL", "/api/*", "BeforeServe", HookFunc},
{"ALL", "/order", "", new(Order)},
{"REST", "/product", "", new(Product)},
{"GET", "/user/register", "Register", user},
{"GET", "/user/reset-pass", "ResetPassword", user},
{"POST", "/user/reset-pass", "ResetPassword", user},
{"POST", "/user/login", "Login", user},
})
}

View File

@ -1,33 +1,25 @@
package main
import "fmt"
type Model struct {
tablesInit string // 初始化Model时的表名称(可以是多个)
tables string // 数据库操作表
fields string // 操作字段
where string // 操作条件
whereArgs []interface{} // 操作条件参数
groupBy string // 分组语句
orderBy string // 排序语句
start int // 分页开始
limit int // 分页条数
data interface{} // 操作记录(支持Map/List/string类型)
batch int // 批量操作条数
filter bool // 是否按照表字段过滤data参数
cacheEnabled bool // 当前SQL操作是否开启查询缓存功能
cacheTime int // 查询缓存时间
cacheName string // 查询缓存名称
}
import (
"fmt"
"gitee.com/johng/gf/g/os/gfile"
"gitee.com/johng/gf/g/os/gfsnotify"
"gitee.com/johng/gf/g/os/glog"
)
func main() {
m1 := &Model{
tables : "1",
path := "/Users/john/Temp"
_, err := gfsnotify.Add(path, func(event *gfsnotify.Event) {
fmt.Println(event)
if event.IsWrite() {
glog.Println("写入文件 : ", event.Path)
fmt.Printf("%s\n", gfile.GetContents(event.Path))
}
})
if err != nil {
glog.Fatal(err)
} else {
select {}
}
m2 := &Model{
tables : "2",
}
*m2 = *m1
fmt.Println(m1)
fmt.Println(m2)
}