mirror of
https://gitee.com/johng/gf
synced 2026-06-09 02:57:43 +08:00
Compare commits
27 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cb7c3a9fa4 | |||
| b0f859cc91 | |||
| 6d4da529ee | |||
| 6c7e536eeb | |||
| 36c2648be8 | |||
| a876b6133d | |||
| 03ff358da8 | |||
| 037f74c549 | |||
| a8caf4ad21 | |||
| 51d9fe5253 | |||
| 3218c89f17 | |||
| 02e467fb57 | |||
| ec994f3080 | |||
| 1181ab499c | |||
| 5424935fd9 | |||
| b9fbfb91bd | |||
| 62f66b4fec | |||
| eef9da9a41 | |||
| 7d32be3b6c | |||
| c2ad9f5fb9 | |||
| 4847fecdaa | |||
| f75383e0c5 | |||
| f683dccb6f | |||
| 6fb106b618 | |||
| 97956ad903 | |||
| 5d72a5b5ae | |||
| 1d7ded562c |
@ -1,16 +0,0 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
array := []uint{1, 1, 2, 3, 3, 4, 4, 5, 5, 6, 6}
|
||||
for i := 0; i < len(array)-1; i++ {
|
||||
for j := i + 1; j < len(array); j++ {
|
||||
if array[i] == array[j] {
|
||||
array = append(array[:j], array[j+1:]...)
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Println(array)
|
||||
|
||||
}
|
||||
46
.example/container/garray/basic_array.go
Normal file
46
.example/container/garray/basic_array.go
Normal file
@ -0,0 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/container/garray"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Create a int array, which is concurrent-unsafe in default.
|
||||
a := garray.NewIntArray()
|
||||
|
||||
// Appending items.
|
||||
for i := 0; i < 10; i++ {
|
||||
a.Append(i)
|
||||
}
|
||||
|
||||
// Get the length of the array.
|
||||
fmt.Println(a.Len())
|
||||
|
||||
// Get the slice of the array.
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// Get the item of specified index.
|
||||
fmt.Println(a.Get(6))
|
||||
|
||||
// Insert after/before specified index.
|
||||
a.InsertAfter(9, 11)
|
||||
a.InsertBefore(10, 10)
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
a.Set(0, 100)
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// Searching the item and returning the index.
|
||||
fmt.Println(a.Search(5))
|
||||
|
||||
// Remove item of specified index.
|
||||
a.Remove(0)
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// Clearing the array.
|
||||
fmt.Println(a.Slice())
|
||||
a.Clear()
|
||||
fmt.Println(a.Slice())
|
||||
}
|
||||
@ -1,59 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/container/garray"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 创建普通的int类型数组,并关闭默认的并发安全特性
|
||||
a := garray.NewIntArray(true)
|
||||
|
||||
// 添加数据项
|
||||
for i := 0; i < 10; i++ {
|
||||
a.Append(i)
|
||||
}
|
||||
|
||||
// 获取当前数组长度
|
||||
fmt.Println(a.Len())
|
||||
|
||||
// 获取当前数据项列表
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// 获取指定索引项
|
||||
fmt.Println(a.Get(6))
|
||||
|
||||
// 在指定索引前插入数据项
|
||||
a.InsertAfter(9, 11)
|
||||
// 在指定索引后插入数据项
|
||||
a.InsertBefore(10, 10)
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// 修改指定索引的数据项
|
||||
a.Set(0, 100)
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// 搜索数据项,返回搜索到的索引位置
|
||||
fmt.Println(a.Search(5))
|
||||
|
||||
// 删除指定索引的数据项
|
||||
a.Remove(0)
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// 并发安全,写锁操作
|
||||
a.LockFunc(func(array []int) {
|
||||
// 将末尾项改为100
|
||||
array[len(array)-1] = 100
|
||||
})
|
||||
|
||||
// 并发安全,读锁操作
|
||||
a.RLockFunc(func(array []int) {
|
||||
fmt.Println(array[len(array)-1])
|
||||
})
|
||||
|
||||
// 清空数组
|
||||
fmt.Println(a.Slice())
|
||||
a.Clear()
|
||||
fmt.Println(a.Slice())
|
||||
}
|
||||
@ -1,17 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/container/garray"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
a := garray.NewIntArray()
|
||||
a.Append(1, 2, 3)
|
||||
|
||||
v := a.Slice()
|
||||
v[0] = 4
|
||||
|
||||
g.Dump(a.Slice())
|
||||
g.Dump(v)
|
||||
}
|
||||
@ -1,20 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/container/garray"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
array := garray.NewSortedStringArray()
|
||||
array.Add("1")
|
||||
array.Add("2")
|
||||
array.Add("3")
|
||||
array.Add("4")
|
||||
array.Add("5")
|
||||
array.Add("6")
|
||||
array.Add("7")
|
||||
array.Add("8")
|
||||
array := garray.NewSortedStrArray()
|
||||
array.Add("9")
|
||||
g.Dump(array.Slice())
|
||||
array.Add("8")
|
||||
array.Add("7")
|
||||
array.Add("6")
|
||||
array.Add("5")
|
||||
array.Add("4")
|
||||
array.Add("3")
|
||||
array.Add("2")
|
||||
array.Add("1")
|
||||
fmt.Println(array.Slice())
|
||||
// output:
|
||||
// [1 2 3 4 5 6 7 8 9]
|
||||
}
|
||||
@ -1,27 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/container/garray"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
array := garray.NewSortedStringArray()
|
||||
array.Add("/api/ctl/show")
|
||||
array.Add("/api/ctl/post")
|
||||
array.Add("/api/obj/rest")
|
||||
array.Add("/api/handler")
|
||||
array.Add("/api/obj/delete")
|
||||
array.Add("/api/obj/show")
|
||||
array.Add("/api/obj/my-show")
|
||||
array.Add("/api/*")
|
||||
array.Add("/api/ctl/rest")
|
||||
array.Add("/api/ctl/my-show")
|
||||
g.Dump(array.Slice())
|
||||
|
||||
fmt.Println(strings.Compare("/api/ctl/post", "/api/*"))
|
||||
fmt.Println(strings.Compare("/api/*", "/api/ctl/my-show"))
|
||||
}
|
||||
@ -8,13 +8,6 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
//m := gmap.New()
|
||||
//m.Set("k", "v")
|
||||
//m.Set("1", "2")
|
||||
//b, err := json.Marshal(m)
|
||||
//fmt.Println(err)
|
||||
//fmt.Println(string(b))
|
||||
|
||||
m := gmap.NewIntIntMap()
|
||||
m.Set(1, 2)
|
||||
m.Set(3, 4)
|
||||
|
||||
26
.example/container/gmap/gmap_map_clone_safe.go
Normal file
26
.example/container/gmap/gmap_map_clone_safe.go
Normal file
@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/container/gmap"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
m1 := gmap.New()
|
||||
m1.Set("1", "1")
|
||||
|
||||
m2 := m1.Map()
|
||||
m2["2"] = "2"
|
||||
|
||||
g.Dump(m1.Clone())
|
||||
g.Dump(m2)
|
||||
//output:
|
||||
//{
|
||||
// "1": "1"
|
||||
//}
|
||||
//
|
||||
//{
|
||||
// "1": "1",
|
||||
// "2": "2"
|
||||
//}
|
||||
}
|
||||
@ -1,17 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/container/gmap"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
m := gmap.New()
|
||||
m.Set("1", "1")
|
||||
|
||||
m1 := m.Map()
|
||||
m1["2"] = "2"
|
||||
|
||||
g.Dump(m.Clone())
|
||||
g.Dump(m1)
|
||||
}
|
||||
@ -10,9 +10,9 @@ import (
|
||||
|
||||
func main() {
|
||||
array := g.Slice{2, 3, 1, 5, 4, 6, 8, 7, 9}
|
||||
hashMap := gmap.New(true)
|
||||
linkMap := gmap.NewLinkMap(true)
|
||||
treeMap := gmap.NewTreeMap(gutil.ComparatorInt, true)
|
||||
hashMap := gmap.New()
|
||||
linkMap := gmap.NewListMap()
|
||||
treeMap := gmap.NewTreeMap(gutil.ComparatorInt)
|
||||
for _, v := range array {
|
||||
hashMap.Set(v, v)
|
||||
}
|
||||
@ -1,39 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 验证 map 的delete方法是否并发安全
|
||||
func main() {
|
||||
// 创建一个初始化的map
|
||||
m := make(map[int]int)
|
||||
for i := 0; i < 10000; i++ {
|
||||
m[i] = i
|
||||
}
|
||||
|
||||
fmt.Println("map size:", len(m))
|
||||
|
||||
wg := sync.WaitGroup{}
|
||||
ev := make(chan struct{}, 0)
|
||||
|
||||
// 创建10个并发的goroutine,使用ev控制并发开始事件,更容易模拟data race
|
||||
for i := 0; i < 10; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
<-ev
|
||||
fmt.Println("start")
|
||||
for i := 0; i < 10000; i++ {
|
||||
delete(m, i)
|
||||
}
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
|
||||
time.Sleep(time.Second)
|
||||
|
||||
close(ev)
|
||||
wg.Wait()
|
||||
}
|
||||
@ -30,8 +30,4 @@ func main() {
|
||||
s := new(Score)
|
||||
v.Struct(s)
|
||||
fmt.Println(s)
|
||||
|
||||
// 只读接口
|
||||
r := v.ReadOnly()
|
||||
fmt.Println(r.String())
|
||||
}
|
||||
|
||||
@ -519,7 +519,6 @@ func getQueriedSqls() {
|
||||
fmt.Println("Sql :", v.Sql)
|
||||
fmt.Println("Args :", v.Args)
|
||||
fmt.Println("Error:", v.Error)
|
||||
fmt.Println("Func :", v.Func)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -8,9 +8,9 @@ import (
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
db.SetMaxIdleConns(10)
|
||||
db.SetMaxOpenConns(10)
|
||||
db.SetConnMaxLifetime(10)
|
||||
db.SetMaxIdleConnCount(10)
|
||||
db.SetMaxOpenConnCount(10)
|
||||
db.SetMaxConnLifetime(time.Minute)
|
||||
|
||||
// 开启调试模式,以便于记录所有执行的SQL
|
||||
db.SetDebug(true)
|
||||
|
||||
34
.example/database/gdb/mysql/gdb_update_field.go
Normal file
34
.example/database/gdb/mysql/gdb_update_field.go
Normal file
@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
|
||||
"github.com/gogf/gf/encoding/gjson"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
table := "medicine_clinics_upload_yinchuan"
|
||||
list, err := db.Table(table).All()
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
panic(err)
|
||||
}
|
||||
content := ""
|
||||
for _, item := range list {
|
||||
if j, err := gjson.DecodeToJson(item["upload_data"].String()); err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
s, _ := j.ToJsonIndentString()
|
||||
content += item["id"].String() + "\t" + item["medicine_clinic_id"].String() + "\t"
|
||||
content += s
|
||||
content += "\n\n"
|
||||
//if _, err := db.Table(table).Data("data_decode", s).Where("id", item["id"].Int()).Update(); err != nil {
|
||||
// panic(err)
|
||||
//}
|
||||
}
|
||||
}
|
||||
gfile.PutContents("/Users/john/Temp/medicine_clinics_upload_yinchuan.txt", content)
|
||||
}
|
||||
@ -8,7 +8,6 @@ import (
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
// 开启调试模式,以便于记录所有执行的SQL
|
||||
db.SetDebug(true)
|
||||
|
||||
r, e := db.Table("test").Where("id IN (?)", []interface{}{1, 2}).All()
|
||||
|
||||
@ -520,7 +520,6 @@ func getQueriedSqls() {
|
||||
fmt.Println("Sql :", v.Sql)
|
||||
fmt.Println("Args :", v.Args)
|
||||
fmt.Println("Error:", v.Error)
|
||||
fmt.Println("Func :", v.Func)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,47 +1,47 @@
|
||||
package main
|
||||
|
||||
//import (
|
||||
// _ "github.com/mattn/go-sqlite3"
|
||||
// "github.com/gogf/gf/database/gdb"
|
||||
// "github.com/gogf/gf/frame/g"
|
||||
// "fmt"
|
||||
//)
|
||||
//
|
||||
//func main() {
|
||||
// gdb.SetConfig(gdb.Config{
|
||||
// "default": gdb.ConfigGroup{
|
||||
// gdb.ConfigNode{
|
||||
// Name: "/tmp/my.db",
|
||||
// Type: "sqlite",
|
||||
// },
|
||||
// },
|
||||
// })
|
||||
// db := g.Database()
|
||||
// if db == nil {
|
||||
// panic("db create failed")
|
||||
// }
|
||||
// defer db.Close()
|
||||
//
|
||||
// // 创建表
|
||||
// sql := `CREATE TABLE user (
|
||||
// uid INT PRIMARY KEY NOT NULL,
|
||||
// name VARCHAR(30) NOT NULL
|
||||
// );`
|
||||
// if _, err := db.Exec(sql); err != nil {
|
||||
// fmt.Println(err)
|
||||
// }
|
||||
//
|
||||
// // 写入数据
|
||||
// result, err := db.Table("user").Data(g.Map{"uid" : 1, "name" : "john"}).Save()
|
||||
// if err == nil {
|
||||
// fmt.Println(result.RowsAffected())
|
||||
// } else {
|
||||
// fmt.Println(err)
|
||||
// }
|
||||
//
|
||||
// // 删除表
|
||||
// sql = `DROP TABLE user;`
|
||||
// if _, err := db.Exec(sql); err != nil {
|
||||
// fmt.Println(err)
|
||||
// }
|
||||
//}
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
gdb.SetConfig(gdb.Config{
|
||||
"default": gdb.ConfigGroup{
|
||||
gdb.ConfigNode{
|
||||
Name: "/tmp/my.db",
|
||||
Type: "sqlite",
|
||||
},
|
||||
},
|
||||
})
|
||||
db := g.DB()
|
||||
if db == nil {
|
||||
panic("db create failed")
|
||||
}
|
||||
|
||||
// 创建表
|
||||
sql := `CREATE TABLE user (
|
||||
uid INT PRIMARY KEY NOT NULL,
|
||||
name VARCHAR(30) NOT NULL
|
||||
);`
|
||||
if _, err := db.Exec(sql); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
// 写入数据
|
||||
result, err := db.Table("user").Data(g.Map{"uid": 1, "name": "john"}).Save()
|
||||
if err == nil {
|
||||
fmt.Println(result.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
// 删除表
|
||||
sql = `DROP TABLE user;`
|
||||
if _, err := db.Exec(sql); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,60 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
|
||||
"github.com/gogf/gf/os/gcmd"
|
||||
|
||||
"github.com/gogf/gf/container/garray"
|
||||
"github.com/gogf/gf/database/gredis"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
batchNumber := 1000
|
||||
redis1Config, err := gredis.ConfigFromStr("im-redis-slave:6379,9")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
redis2Config, err := gredis.ConfigFromStr("r-bp1f0a5d4efd8744.redis.rds.aliyuncs.com:6379,9")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
gredis.SetConfig(redis1Config)
|
||||
|
||||
v, err := g.Redis().DoVar("keys", "*")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
array := garray.NewStrArrayFrom(v.Strings())
|
||||
for {
|
||||
slice := array.PopLefts(batchNumber)
|
||||
if len(slice) > 0 {
|
||||
// `migrate %s %d "" 0 2000 copy replace auth %s keys %s`,
|
||||
params := g.Slice{
|
||||
redis2Config.Host,
|
||||
redis2Config.Port,
|
||||
"",
|
||||
redis2Config.Db,
|
||||
2000,
|
||||
"copy",
|
||||
"replace",
|
||||
"keys",
|
||||
}
|
||||
params = append(params, gconv.Interfaces(slice)...)
|
||||
fmt.Println(params)
|
||||
if gcmd.GetOpt("dryrun") == "0" {
|
||||
if v, err := g.Redis().DoVar("migrate", params...); err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
fmt.Println(v.String())
|
||||
}
|
||||
}
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
fmt.Println("done")
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
package main
|
||||
package gbase64
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@ -9,26 +9,19 @@ import (
|
||||
|
||||
func main() {
|
||||
// 使用gbinary.Encoded对基本数据类型进行二进制打包
|
||||
if buffer, err := gbinary.Encode(18, 300, 1.01); err != nil {
|
||||
glog.Error(err)
|
||||
} else {
|
||||
fmt.Println(buffer)
|
||||
}
|
||||
fmt.Println(gbinary.Encode(18, 300, 1.01))
|
||||
|
||||
// 使用gbinary.Decode对整形二进制解包,注意第二个及其后参数为字长确定的整形变量的指针地址,字长确定的类型,
|
||||
// 例如:int8/16/32/64、uint8/16/32/64、float32/64
|
||||
// 这里的1.01默认为float64类型(64位系统下)
|
||||
if buffer, err := gbinary.Encode(18, 300, 1.01); err != nil {
|
||||
buffer := gbinary.Encode(18, 300, 1.01)
|
||||
var i1 int8
|
||||
var i2 int16
|
||||
var f3 float64
|
||||
if err := gbinary.Decode(buffer, &i1, &i2, &f3); err != nil {
|
||||
glog.Error(err)
|
||||
} else {
|
||||
var i1 int8
|
||||
var i2 int16
|
||||
var f3 float64
|
||||
if err := gbinary.Decode(buffer, &i1, &i2, &f3); err != nil {
|
||||
glog.Error(err)
|
||||
} else {
|
||||
fmt.Println(i1, i2, f3)
|
||||
}
|
||||
fmt.Println(i1, i2, f3)
|
||||
}
|
||||
|
||||
// 编码/解析 int,自动识别变量长度
|
||||
|
||||
@ -15,6 +15,6 @@ func main() {
|
||||
}
|
||||
|
||||
redisCfg := new(RedisConfig)
|
||||
fmt.Println(g.Config().GetToStruct("redis", redisCfg))
|
||||
fmt.Println(g.Config().GetStruct("redis", redisCfg))
|
||||
fmt.Println(redisCfg)
|
||||
}
|
||||
|
||||
@ -15,4 +15,6 @@ func main() {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(str)
|
||||
// output:
|
||||
// 花间一壶酒,独酌无相亲。
|
||||
}
|
||||
|
||||
44
.example/encoding/gparser/config.yaml
Normal file
44
.example/encoding/gparser/config.yaml
Normal file
@ -0,0 +1,44 @@
|
||||
|
||||
broker:
|
||||
ip: "127.0.0.1"
|
||||
tcpport: "4222"
|
||||
httpport: "8222"
|
||||
user: "alfxnats"
|
||||
pwd: "alfxnats"
|
||||
clusterid: "alfxnats"
|
||||
database:
|
||||
ip: "110.1.1.227"
|
||||
port: "27017"
|
||||
user: "alfxdev"
|
||||
pwd: "alfxdev"
|
||||
dbname: "alfxdev"
|
||||
scheduler:
|
||||
a2rparallel: 4
|
||||
sleeptime: 300
|
||||
worker:
|
||||
name: "worker1"
|
||||
domains:
|
||||
officenet: 3
|
||||
producnet: 3
|
||||
free: 3
|
||||
temppath: "/home/alfx/proc"
|
||||
common:
|
||||
filepath: "/home/alfx/file"
|
||||
logpath: "home/alfx/log"
|
||||
logdebug: true
|
||||
logtrace: true
|
||||
report:
|
||||
localip: "127.0.0.1"
|
||||
localport: ""
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -19,7 +19,7 @@ func getWithPattern1() {
|
||||
}
|
||||
}`
|
||||
|
||||
if p, e := gparser.LoadContent([]byte(data), "json"); e != nil {
|
||||
if p, e := gparser.LoadContent([]byte(data)); e != nil {
|
||||
glog.Error(e)
|
||||
} else {
|
||||
fmt.Println("John Score:", p.GetFloat32("users.list.1.score"))
|
||||
@ -36,7 +36,7 @@ func getWithPattern2() {
|
||||
<body>Don't forget me this weekend!</body>
|
||||
</note>`
|
||||
|
||||
if p, e := gparser.LoadContent([]byte(data), "xml"); e != nil {
|
||||
if p, e := gparser.LoadContent([]byte(data)); e != nil {
|
||||
glog.Error(e)
|
||||
} else {
|
||||
fmt.Println("Heading:", p.GetString("note.heading"))
|
||||
@ -52,7 +52,7 @@ func multiDots1() {
|
||||
},
|
||||
"users.count" : 101
|
||||
}`
|
||||
if p, e := gparser.LoadContent([]byte(data), "json"); e != nil {
|
||||
if p, e := gparser.LoadContent([]byte(data)); e != nil {
|
||||
glog.Error(e)
|
||||
} else {
|
||||
fmt.Println("Users Count:", p.Get("users.count"))
|
||||
@ -70,7 +70,7 @@ func multiDots2() {
|
||||
"count.type1" : 100
|
||||
}
|
||||
}`
|
||||
if p, e := gparser.LoadContent([]byte(data), "json"); e != nil {
|
||||
if p, e := gparser.LoadContent([]byte(data)); e != nil {
|
||||
glog.Error(e)
|
||||
} else {
|
||||
fmt.Println("Users Count:", p.Get("users.count.type1"))
|
||||
@ -88,7 +88,7 @@ func set1() {
|
||||
<list><title>gf article2</title><content>gf content2</content></list>
|
||||
<list><title>gf article3</title><content>gf content3</content></list>
|
||||
</article>`
|
||||
if p, e := gparser.LoadContent([]byte(data), "xml"); e != nil {
|
||||
if p, e := gparser.LoadContent([]byte(data)); e != nil {
|
||||
glog.Error(e)
|
||||
} else {
|
||||
p.Set("article.list.0", nil)
|
||||
@ -105,7 +105,7 @@ func set2() {
|
||||
"count" : 100
|
||||
}
|
||||
}`
|
||||
if p, e := gparser.LoadContent([]byte(data), "json"); e != nil {
|
||||
if p, e := gparser.LoadContent([]byte(data)); e != nil {
|
||||
glog.Error(e)
|
||||
} else {
|
||||
p.Set("users.count", 1)
|
||||
@ -116,7 +116,7 @@ func set2() {
|
||||
}
|
||||
|
||||
func makeXml1() {
|
||||
p := gparser.New()
|
||||
p := gparser.New(nil)
|
||||
p.Set("name", "john")
|
||||
p.Set("age", 18)
|
||||
p.Set("scores", map[string]int{
|
||||
@ -133,7 +133,7 @@ func makeJson1() {
|
||||
Id int `json:"id"`
|
||||
Price float32 `json:"price"`
|
||||
}
|
||||
p := gparser.New()
|
||||
p := gparser.New(nil)
|
||||
p.Set("orders.list.0", Order{1, 100})
|
||||
p.Set("orders.list.1", Order{2, 666})
|
||||
p.Set("orders.list.2", Order{3, 999.99})
|
||||
|
||||
77
.example/encoding/gparser/gparser_yaml_issue336.go
Normal file
77
.example/encoding/gparser/gparser_yaml_issue336.go
Normal file
@ -0,0 +1,77 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/encoding/gparser"
|
||||
)
|
||||
|
||||
type Conf struct {
|
||||
Broker Broker
|
||||
Database Database
|
||||
Scheduler Scheduler
|
||||
Worker Worker
|
||||
Common Common
|
||||
Report Report
|
||||
}
|
||||
|
||||
type Broker struct {
|
||||
Ip string
|
||||
Tcport string
|
||||
Httpport string
|
||||
User string
|
||||
Pwd string
|
||||
Clusterid string
|
||||
}
|
||||
|
||||
type Database struct {
|
||||
Ip string
|
||||
Port string
|
||||
User string
|
||||
Pwd string
|
||||
Dbname string
|
||||
}
|
||||
|
||||
type Scheduler struct {
|
||||
SleepTime int
|
||||
Pidfilepath string
|
||||
A2rparallel int
|
||||
}
|
||||
|
||||
type Worker struct {
|
||||
Name string
|
||||
Domains map[string]interface{}
|
||||
Temppath string
|
||||
Pidfilepath string
|
||||
}
|
||||
|
||||
type Common struct {
|
||||
Filepath string
|
||||
Logpath string
|
||||
Logdebug bool
|
||||
Logtrace bool
|
||||
}
|
||||
|
||||
type Report struct {
|
||||
Localip string
|
||||
Localport string //暂不启用
|
||||
}
|
||||
|
||||
func main() {
|
||||
_, err := gparser.Load("config.yaml")
|
||||
if err != nil {
|
||||
fmt.Println("oops,read config.yaml err:", err)
|
||||
}
|
||||
|
||||
//fmt.Println("yaml.v3读取yaml文件")
|
||||
//f, err := os.Open("config.yaml")
|
||||
//if err != nil {
|
||||
// panic(err)
|
||||
//}
|
||||
//var conf Conf
|
||||
//err = yaml.NewDecoder(f).Decode(&conf)
|
||||
//if err != nil {
|
||||
// panic(err)
|
||||
//}
|
||||
//fmt.Println(conf)
|
||||
}
|
||||
@ -1,4 +0,0 @@
|
||||
|
||||
# MySQL数据库配置
|
||||
[database]
|
||||
link = "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
|
||||
@ -1,13 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/.example/frame/mvc/model/test"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
g.DB().SetDebug(true)
|
||||
user, err := test.ModelUser().One()
|
||||
g.Dump(err)
|
||||
g.Dump(user)
|
||||
}
|
||||
@ -7,7 +7,7 @@ import (
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_FULLNAME)
|
||||
s.SetNameToUriType(ghttp.URI_TYPE_FULLNAME)
|
||||
s.EnableAdmin()
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.Write("hello world")
|
||||
|
||||
25
.example/net/ghttp/server/cors/cors1.go
Normal file
25
.example/net/ghttp/server/cors/cors1.go
Normal file
@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func MiddlewareCORS(r *ghttp.Request) {
|
||||
r.Response.CORSDefault()
|
||||
r.Middleware.Next()
|
||||
}
|
||||
|
||||
func Order(r *ghttp.Request) {
|
||||
r.Response.Write("GET")
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/api.v1", func(g *ghttp.RouterGroup) {
|
||||
g.Middleware(MiddlewareCORS)
|
||||
g.GET("/order", Order)
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
27
.example/net/ghttp/server/cors/cors2.go
Normal file
27
.example/net/ghttp/server/cors/cors2.go
Normal file
@ -0,0 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func MiddlewareCORS(r *ghttp.Request) {
|
||||
corsOptions := r.Response.DefaultCORSOptions()
|
||||
corsOptions.AllowDomain = []string{"goframe.org", "baidu.com"}
|
||||
r.Response.CORS(corsOptions)
|
||||
r.Middleware.Next()
|
||||
}
|
||||
|
||||
func Order(r *ghttp.Request) {
|
||||
r.Response.Write("GET")
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/api.v1", func(g *ghttp.RouterGroup) {
|
||||
g.Middleware(MiddlewareCORS)
|
||||
g.GET("/order", Order)
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
33
.example/net/ghttp/server/cors/cors3.go
Normal file
33
.example/net/ghttp/server/cors/cors3.go
Normal file
@ -0,0 +1,33 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func MiddlewareCORS(r *ghttp.Request) {
|
||||
corsOptions := r.Response.DefaultCORSOptions()
|
||||
corsOptions.AllowDomain = []string{"goframe.org"}
|
||||
if !r.Response.CORSAllowedOrigin(corsOptions) {
|
||||
r.Response.WriteStatus(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
r.Response.CORS(corsOptions)
|
||||
r.Middleware.Next()
|
||||
}
|
||||
|
||||
func Order(r *ghttp.Request) {
|
||||
r.Response.Write("GET")
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/api.v1", func(g *ghttp.RouterGroup) {
|
||||
g.Middleware(MiddlewareCORS)
|
||||
g.GET("/order", Order)
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
16
.example/net/ghttp/server/form/form.go
Normal file
16
.example/net/ghttp/server/form/form.go
Normal file
@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
g.Dump(r.GetPostMap())
|
||||
r.Response.WriteTpl("form.html")
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
30
.example/net/ghttp/server/form/form.html
Normal file
30
.example/net/ghttp/server/form/form.html
Normal file
@ -0,0 +1,30 @@
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<title>form test</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>form1</h1>
|
||||
<form action="?" method="post" enctype="multipart/form-data">
|
||||
<p><input type="text" name="input1" value="1"/></p>
|
||||
<p><input type="text" name="input2" value="2"/></p>
|
||||
<p><input type="text" name="array1" value="3"/></p>
|
||||
<p><input type="text" name="array1" value="4"/></p>
|
||||
<p><input type="text" name="array2[]" value="5"/></p>
|
||||
<p><input type="text" name="array2[]" value="6"/></p>
|
||||
<p><input type="text" name="map[a]" value="7"/></p>
|
||||
<p><input type="text" name="map[b]" value="8"/></p>
|
||||
<p><input type="password" name="password1" value="9"/></p>
|
||||
<input type="submit" value="Submit" />
|
||||
</form>
|
||||
|
||||
<h1>form2</h1>
|
||||
<form action="?" method="post" enctype="application/x-www-form-urlencoded">
|
||||
<p><input type="text" name="input[a]" value="1"/></p>
|
||||
<p><input type="text" name="input[b]" value="2"/></p>
|
||||
<p><input type="text" name="array" value="3"/></p>
|
||||
<p><input type="text" name="array" value="4"/></p>
|
||||
<p><input type="password" name="password2" value="5"/></p>
|
||||
<input type="submit" value="Submit" />
|
||||
</form>
|
||||
</body>
|
||||
41
.example/net/ghttp/server/middleware/auth_exception.go
Normal file
41
.example/net/ghttp/server/middleware/auth_exception.go
Normal file
@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func MiddlewareAuth(r *ghttp.Request) {
|
||||
token := r.Get("token")
|
||||
if token == "123456" {
|
||||
r.Middleware.Next()
|
||||
} else {
|
||||
r.Response.WriteStatus(http.StatusForbidden)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/admin", func(g *ghttp.RouterGroup) {
|
||||
g.MiddlewarePattern("/*action", func(r *ghttp.Request) {
|
||||
if action := r.GetRouterString("action"); action != "" {
|
||||
switch action {
|
||||
case "login":
|
||||
r.Middleware.Next()
|
||||
return
|
||||
}
|
||||
}
|
||||
MiddlewareAuth(r)
|
||||
})
|
||||
g.ALL("/login", func(r *ghttp.Request) {
|
||||
r.Response.Write("login")
|
||||
})
|
||||
g.ALL("/dashboard", func(r *ghttp.Request) {
|
||||
r.Response.Write("dashboard")
|
||||
})
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
42
.example/net/ghttp/server/middleware/error_handling.go
Normal file
42
.example/net/ghttp/server/middleware/error_handling.go
Normal file
@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func MiddlewareAuth(r *ghttp.Request) {
|
||||
token := r.Get("token")
|
||||
if token == "123456" {
|
||||
r.Middleware.Next()
|
||||
} else {
|
||||
r.Response.WriteStatus(http.StatusForbidden)
|
||||
}
|
||||
}
|
||||
|
||||
func MiddlewareCORS(r *ghttp.Request) {
|
||||
r.Response.CORSDefault()
|
||||
r.Middleware.Next()
|
||||
}
|
||||
|
||||
func MiddlewareError(r *ghttp.Request) {
|
||||
r.Middleware.Next()
|
||||
if r.Response.Status >= http.StatusInternalServerError {
|
||||
r.Response.ClearBuffer()
|
||||
r.Response.Write("Internal error occurred, please try again later.")
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/api.v2", func(g *ghttp.RouterGroup) {
|
||||
g.Middleware(MiddlewareAuth, MiddlewareCORS, MiddlewareError)
|
||||
g.ALL("/user/list", func(r *ghttp.Request) {
|
||||
panic("db error: sql is xxxxxxx")
|
||||
})
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
@ -17,10 +17,10 @@ func main() {
|
||||
s3 := g.Server(3)
|
||||
s4 := g.Server(4)
|
||||
|
||||
s1.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_DEFAULT)
|
||||
s2.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_FULLNAME)
|
||||
s3.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_ALLLOWER)
|
||||
s4.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_CAMEL)
|
||||
s1.SetNameToUriType(ghttp.URI_TYPE_DEFAULT)
|
||||
s2.SetNameToUriType(ghttp.URI_TYPE_FULLNAME)
|
||||
s3.SetNameToUriType(ghttp.URI_TYPE_ALLLOWER)
|
||||
s4.SetNameToUriType(ghttp.URI_TYPE_CAMEL)
|
||||
|
||||
s1.BindObject("/{.struct}/{.method}", new(User))
|
||||
s2.BindObject("/{.struct}/{.method}", new(User))
|
||||
|
||||
@ -9,7 +9,7 @@ import (
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.SetServerRoot("public")
|
||||
s.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_ALLLOWER)
|
||||
s.SetNameToUriType(ghttp.URI_TYPE_ALLLOWER)
|
||||
s.SetErrorLogEnabled(true)
|
||||
s.SetAccessLogEnabled(true)
|
||||
s.SetPort(2333)
|
||||
|
||||
@ -1,16 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
l := flock.NewFlock("/tmp/go-lock.lock")
|
||||
l.Lock()
|
||||
fmt.Printf("lock 1")
|
||||
l.Lock()
|
||||
fmt.Printf("lock 1")
|
||||
|
||||
time.Sleep(time.Hour)
|
||||
}
|
||||
@ -1,18 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/os/gflock"
|
||||
"github.com/gogf/gf/os/glog"
|
||||
"github.com/gogf/gf/os/gproc"
|
||||
)
|
||||
|
||||
func main() {
|
||||
l := gflock.New("demo.lock")
|
||||
l.Lock()
|
||||
glog.Printf("locked by pid: %d", gproc.Pid())
|
||||
time.Sleep(10 * time.Second)
|
||||
l.UnLock()
|
||||
glog.Printf("unlocked by pid: %d", gproc.Pid())
|
||||
}
|
||||
@ -2,10 +2,47 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
"github.com/gogf/gf/text/gregex"
|
||||
|
||||
"github.com/gogf/gf/debug/gdebug"
|
||||
)
|
||||
|
||||
func main() {
|
||||
file := "xxx/github.com/hg-hh/ww/gf/.example/"
|
||||
fmt.Println(gregex.IsMatchString(`/github.com/[^/]+/gf/\.example/`, file))
|
||||
cdnUrl := "http://localhost"
|
||||
content := `
|
||||
<link rel="stylesheet" href="/plugin/amazeui-2.7.2/css/amazeui.min.css">
|
||||
<link rel="stylesheet" href="/plugin/markdown-css/github-markdown.min.js">
|
||||
<link rel="stylesheet" href="/plugin/prism/prism.css">
|
||||
<link rel="stylesheet" href="/resource/css/document/style.css">
|
||||
<link rel="icon" href="/resource/image/favicon.ico" type="image/x-icon">
|
||||
`
|
||||
s, err := gregex.ReplaceStringFuncMatch(`(href|src)=['"](.+?)['"]`, content, func(match []string) string {
|
||||
link := match[2]
|
||||
if len(link) == 0 {
|
||||
return match[0]
|
||||
}
|
||||
if link[0:1] != "/" && link[0:1] != "#" {
|
||||
if len(link) > 10 && link[0:10] == "javascript" {
|
||||
return match[0]
|
||||
}
|
||||
if len(link) > 7 && link[0:7] == "mailto:" {
|
||||
return match[0]
|
||||
}
|
||||
if len(link) > 4 && link[0:4] == "http" {
|
||||
return match[0]
|
||||
}
|
||||
link = "/" + link
|
||||
}
|
||||
if link[0:1] == "/" {
|
||||
switch gfile.ExtName(link) {
|
||||
case "png", "jpg", "jpeg", "gif", "js", "css", "otf", "eot", "ttf", "woff", "woff2":
|
||||
return fmt.Sprintf(`%s="%s%s?%s"`, match[1], cdnUrl, link, gdebug.BinVersion())
|
||||
}
|
||||
}
|
||||
return match[0]
|
||||
})
|
||||
fmt.Println(err)
|
||||
fmt.Println(s)
|
||||
}
|
||||
|
||||
12
.github/FUNDING.yml
vendored
Normal file
12
.github/FUNDING.yml
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
# These are supported funding model platforms
|
||||
|
||||
github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
|
||||
patreon: # Replace with a single Patreon username
|
||||
open_collective: # Replace with a single Open Collective username
|
||||
ko_fi: # Replace with a single Ko-fi username
|
||||
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
|
||||
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
|
||||
liberapay: # Replace with a single Liberapay username
|
||||
issuehunt: # Replace with a single IssueHunt username
|
||||
otechie: # Replace with a single Otechie username
|
||||
custom: https://github.com/gogf/gf#donators
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@ -14,3 +14,4 @@ bin/
|
||||
cbuild
|
||||
**/.DS_Store
|
||||
.vscode/
|
||||
go.sum
|
||||
|
||||
@ -13,6 +13,7 @@
|
||||
|[zfan_codes](https://gitee.com/zfan_codes)|gitee|¥10.00
|
||||
|[arden](https://github.com/arden)|alipay|¥10.00
|
||||
|[macnie](https://www.macnie.com)|wechat|¥100.00
|
||||
|lah|wechat|¥100.00
|
||||
|x*z|wechat|¥20.00
|
||||
|潘兄|wechat|¥100.00
|
||||
|Fly的狐狸|wechat|¥100.00
|
||||
|
||||
191
README.MD
191
README.MD
@ -38,7 +38,6 @@ golang version >= 1.10
|
||||
|
||||
# Quick Start
|
||||
|
||||
## Hello World
|
||||
```go
|
||||
package main
|
||||
|
||||
@ -55,196 +54,6 @@ func main() {
|
||||
s.Run()
|
||||
}
|
||||
```
|
||||
## Router & Middleware
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/api.v2", func(g *ghttp.RouterGroup) {
|
||||
g.Middleware(func(r *ghttp.Request) {
|
||||
r.Response.Write("start")
|
||||
r.Middleware.Next()
|
||||
r.Response.Write("end")
|
||||
})
|
||||
g.Group("/order", func(g *ghttp.RouterGroup) {
|
||||
g.GET("/list", func(r *ghttp.Request) {
|
||||
r.Response.Write("list")
|
||||
})
|
||||
})
|
||||
g.Group("/user", func(g *ghttp.RouterGroup) {
|
||||
g.GET("/info", func(r *ghttp.Request) {
|
||||
r.Response.Write("info")
|
||||
})
|
||||
g.POST("/edit", func(r *ghttp.Request) {
|
||||
r.Response.Write("edit")
|
||||
})
|
||||
})
|
||||
g.Group("/hook", func(g *ghttp.RouterGroup) {
|
||||
g.Hook("/*", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
||||
r.Response.Write("hook any")
|
||||
})
|
||||
g.Hook("/:name", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
||||
r.Response.Write("hook name")
|
||||
})
|
||||
})
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
```
|
||||
|
||||
## Multi ports & domains
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func Hello1(r *ghttp.Request) {
|
||||
r.Response.Write("127.0.0.1: Hello1!")
|
||||
}
|
||||
|
||||
func Hello2(r *ghttp.Request) {
|
||||
r.Response.Write("localhost: Hello2!")
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Domain("127.0.0.1").BindHandler("/", Hello1)
|
||||
s.Domain("localhost").BindHandler("/", Hello2)
|
||||
s.SetPort(8100, 8200, 8300)
|
||||
s.Run()
|
||||
}
|
||||
```
|
||||
## Template Engine
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/template", func(r *ghttp.Request) {
|
||||
r.Response.WriteTpl("index.tpl", g.Map{
|
||||
"id": 123,
|
||||
"name": "john",
|
||||
})
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
```
|
||||
## File Uploading
|
||||
```go
|
||||
func Upload(r *ghttp.Request) {
|
||||
if f, h, e := r.FormFile("upload-file"); e == nil {
|
||||
defer f.Close()
|
||||
name := gfile.Basename(h.Filename)
|
||||
buffer := make([]byte, h.Size)
|
||||
f.Read(buffer)
|
||||
gfile.PutBytes("/tmp/" + name, buffer)
|
||||
r.Response.Write(name + " uploaded successly")
|
||||
} else {
|
||||
r.Response.Write(e.Error())
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## ORM Operations
|
||||
|
||||
### 1. Retrieving instance
|
||||
```go
|
||||
db := g.DB()
|
||||
db := g.DB("user-center")
|
||||
```
|
||||
### 2. Chaining Operations
|
||||
|
||||
`Where + string`
|
||||
```go
|
||||
// SELECT * FROM user WHERE uid>1 LIMIT 0,10
|
||||
r, err := db.Table("user").Where("uid > ?", 1).Limit(0, 10).Select()
|
||||
|
||||
// SELECT uid,name FROM user WHERE uid>1 LIMIT 0,10
|
||||
r, err := db.Table("user").Fileds("uid,name").Where("uid > ?", 1).Limit(0, 10).Select()
|
||||
|
||||
// SELECT * FROM user WHERE uid=1
|
||||
r, err := db.Table("user").Where("u.uid=1",).One()
|
||||
r, err := db.Table("user").Where("u.uid", 1).One()
|
||||
r, err := db.Table("user").Where("u.uid=?", 1).One()
|
||||
// SELECT * FROM user WHERE (uid=1) AND (name='john')
|
||||
r, err := db.Table("user").Where("uid", 1).Where("name", "john").One()
|
||||
r, err := db.Table("user").Where("uid=?", 1).And("name=?", "john").One()
|
||||
// SELECT * FROM user WHERE (uid=1) OR (name='john')
|
||||
r, err := db.Table("user").Where("uid=?", 1).Or("name=?", "john").One()
|
||||
```
|
||||
`Where + map`
|
||||
```go
|
||||
// SELECT * FROM user WHERE uid=1 AND name='john'
|
||||
r, err := db.Table("user").Where(g.Map{"uid" : 1, "name" : "john"}).One()
|
||||
// SELECT * FROM user WHERE uid=1 AND age>18
|
||||
r, err := db.Table("user").Where(g.Map{"uid" : 1, "age>" : 18}).One()
|
||||
```
|
||||
`Where + struct/*struct`
|
||||
```go
|
||||
type User struct {
|
||||
Id int `json:"uid"`
|
||||
UserName string `gconv:"name"`
|
||||
}
|
||||
// SELECT * FROM user WHERE uid =1 AND name='john'
|
||||
r, err := db.Table("user").Where(User{ Id : 1, UserName : "john"}).One()
|
||||
// SELECT * FROM user WHERE uid =1
|
||||
r, err := db.Table("user").Where(&User{ Id : 1}).One()
|
||||
```
|
||||
### 3. Update & Delete
|
||||
```go
|
||||
// UPDATE user SET name='john guo' WHERE name='john'
|
||||
r, err := db.Table("user").Data(gdb.Map{"name" : "john guo"}).Where("name=?", "john").Update()
|
||||
r, err := db.Table("user").Data("name='john guo'").Where("name=?", "john").Update()
|
||||
// UPDATE user SET status=1 ORDER BY login_time asc LIMIT 10
|
||||
r, err := db.Table("user").Data("status", 1).OrderBy("login_time asc").Limit(10).Update
|
||||
|
||||
// DELETE FROM user WHERE uid=10
|
||||
r, err := db.Table("user").Where("uid=?", 10).Delete()
|
||||
// DELETE FROM user ORDER BY login_time asc LIMIT 10
|
||||
r, err := db.Table("user").OrderBy("login_time asc").Limit(10).Delete()
|
||||
```
|
||||
### 4. Insert & Replace & Save
|
||||
```go
|
||||
r, err := db.Table("user").Data(g.Map{"name": "john"}).Insert()
|
||||
r, err := db.Table("user").Data(g.Map{"uid": 10000, "name": "john"}).Replace()
|
||||
r, err := db.Table("user").Data(g.Map{"uid": 10001, "name": "john"}).Save()
|
||||
```
|
||||
### 5. Transaction
|
||||
```go
|
||||
if tx, err := db.Begin(); err == nil {
|
||||
r, err := tx.Save("user", g.Map{
|
||||
"uid" : 1,
|
||||
"name" : "john",
|
||||
})
|
||||
tx.Commit()
|
||||
}
|
||||
```
|
||||
### 6. Error Handling
|
||||
```go
|
||||
func GetOrderInfo(id int) (order *Order, err error) {
|
||||
err = g.DB().Table("order").Where("id", id).Struct(&order)
|
||||
if err != nil && err == sql.ErrNoRows {
|
||||
err = nil
|
||||
}
|
||||
return
|
||||
}
|
||||
```
|
||||
|
||||
[More Features...](https://goframe.org/start/index)
|
||||
|
||||
|
||||
34
RELEASE.2.MD
34
RELEASE.2.MD
@ -1,30 +1,18 @@
|
||||
# `v1.9.0`
|
||||
# `v1.9.3`
|
||||
|
||||
该版本实际为`v2.0`的大版本发布,为避免`go module`机制严格要求`v2`版本以上需要修改`import`并加上`v2`后缀,因此使用了`v1.9`版本进行发布。
|
||||
|
||||
## 新特性
|
||||
|
||||
1. 增加`gf`命令行开发辅助工具:https://goframe.org/toolchain/cli
|
||||
- 支持`GF`框架下载更新;
|
||||
- 支持初始化新建项目命令;
|
||||
- 支持跨平台交叉编译命令;
|
||||
- 命令行工具支持自动更新命令;
|
||||
- 支持二进制文件打包,生成二进制文件或者Go程序文件;
|
||||
- 支持指定数据库生成数据表模型,支持本地配置文件读取数据库配置;
|
||||
1. 新增`gi18n`国际化管理模块:https://goframe.org/i18n/gi18n/index
|
||||
1. 新增`gf`命令行开发辅助工具:https://goframe.org/toolchain/cli
|
||||
1. 新增`gres`资源管理器模块:https://goframe.org/os/gres/index
|
||||
- 资源管理器支持虚拟的文件/目录操作方法;
|
||||
- 默认整合支持到了WebServer、配置管理、模板引擎中;
|
||||
- 可将任意的文件打包为`Go`内容,支持开发者自定义加解密;
|
||||
- 任意文件如网站静态文件、配置文件等可编译到二进制文件中,也可编译到发布的可执行文件中;
|
||||
- 开发者可只需编译发布一个可执行文件,除了方便了软件分发,也为保护软件知识产权内容提供了可能;
|
||||
1. 新增`gini`模块:https://goframe.org/encoding/gini/index
|
||||
- 支持`ini`文件的读取/生成;
|
||||
- 同时配置管理模块也增加了对`ini`文件的支持;
|
||||
- 配置管理模块目前自动识别支持`ini/xml/json/toml/yaml`五种数据格式;
|
||||
1. `Session`功能重构,新增`gsession`模块,`WebServer`默认使用文件存储`Session`:https://goframe.org/net/ghttp/session
|
||||
1. 重构`Session`功能,新增`gsession`模块,`WebServer`默认使用文件存储`Session`:https://goframe.org/net/ghttp/session
|
||||
1. `WebServer`新增中间件特性,并保留原有的HOOK设计,两者都可实现请求拦截、预处理等等特性:https://goframe.org/net/ghttp/router/middleware
|
||||
1. 新增`gi18n`国际化管理模块:https://goframe.org/i18n/gi18n/index
|
||||
1. 新增`gini`模块:https://goframe.org/encoding/gini/index
|
||||
1. `WebServer`新增更便捷的层级路由注册方式:https://goframe.org/net/ghttp/group/level
|
||||
1. `gcmd`命令行参数解析模块重构,增加`Parser`解析对象;
|
||||
1. 新增`gdebug`模块,用于堆栈获取/打印;
|
||||
1. `gcmd`命令行参数解析模块重构,增加`Parser`解析对象:https://goframe.org/os/gcmd/index
|
||||
1. 新增`gdebug`模块,用于堆栈信息获取/打印:https://goframe.org/debug/gdebug/index
|
||||
|
||||
|
||||
## 重大调整
|
||||
@ -40,6 +28,8 @@
|
||||
## 功能改进
|
||||
|
||||
1. `ghttp`
|
||||
- 改进`Request`参数解析方式:https://goframe.org/net/ghttp/request
|
||||
- 改进跨域请求功能,新增`Origin`设置及校验功能:https://goframe.org/net/ghttp/cors
|
||||
- `Cookie`及`Session`的`TTL`配置数据类型修改为`time.Duration`;
|
||||
- 新增允许同时通过`Header/Cookie`传递`SessionId`;
|
||||
- 新增`ConfigFromMap/SetConfigWithMap`方法,支持通过`map`参数设置WebServer;
|
||||
@ -53,7 +43,7 @@
|
||||
- 增加`SetLogger`方法用于开发者自定义数据库的日志打印;
|
||||
- 增加`Master/Slave`方法,开发者可自主选择数据库操作执行的主从节点;
|
||||
- 增加对`mssql/pgsql/oracle`的单元测试;
|
||||
- `debug`模式支持完整带参数整合的SQL语句调试打印;
|
||||
- 在`debug`模式支持完整带参数整合的SQL语句调试打印;
|
||||
- 增加了更多的功能方法;
|
||||
1. `glog`
|
||||
- 新增`Default`方法用于获取默认的`Logger`对象;
|
||||
|
||||
@ -68,6 +68,17 @@ func (m *AnyAnyMap) Map() map[interface{}]interface{} {
|
||||
return data
|
||||
}
|
||||
|
||||
// MapStrAny returns a copy of the data of the map as map[string]interface{}.
|
||||
func (m *AnyAnyMap) MapStrAny() map[string]interface{} {
|
||||
m.mu.RLock()
|
||||
data := make(map[string]interface{}, len(m.data))
|
||||
for k, v := range m.data {
|
||||
data[gconv.String(k)] = v
|
||||
}
|
||||
m.mu.RUnlock()
|
||||
return data
|
||||
}
|
||||
|
||||
// Set sets key-value to the hash map.
|
||||
func (m *AnyAnyMap) Set(key interface{}, val interface{}) {
|
||||
m.mu.Lock()
|
||||
|
||||
@ -104,6 +104,20 @@ func (m *ListMap) Map() map[interface{}]interface{} {
|
||||
return data
|
||||
}
|
||||
|
||||
// MapStrAny returns a copy of the data of the map as map[string]interface{}.
|
||||
func (m *ListMap) MapStrAny() map[string]interface{} {
|
||||
m.mu.RLock()
|
||||
node := (*gListMapNode)(nil)
|
||||
data := make(map[string]interface{}, len(m.data))
|
||||
m.list.IteratorAsc(func(e *glist.Element) bool {
|
||||
node = e.Value.(*gListMapNode)
|
||||
data[gconv.String(node.key)] = node.value
|
||||
return true
|
||||
})
|
||||
m.mu.RUnlock()
|
||||
return data
|
||||
}
|
||||
|
||||
// Set sets key-value to the map.
|
||||
func (m *ListMap) Set(key interface{}, value interface{}) {
|
||||
m.mu.Lock()
|
||||
|
||||
@ -10,6 +10,8 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
|
||||
"github.com/gogf/gf/container/gvar"
|
||||
"github.com/gogf/gf/internal/rwmutex"
|
||||
)
|
||||
@ -408,6 +410,16 @@ func (tree *AVLTree) Map() map[interface{}]interface{} {
|
||||
return m
|
||||
}
|
||||
|
||||
// MapStrAny returns all key-value items as map[string]interface{}.
|
||||
func (tree *AVLTree) MapStrAny() map[string]interface{} {
|
||||
m := make(map[string]interface{}, tree.Size())
|
||||
tree.IteratorAsc(func(key, value interface{}) bool {
|
||||
m[gconv.String(key)] = value
|
||||
return true
|
||||
})
|
||||
return m
|
||||
}
|
||||
|
||||
// Flip exchanges key-value of the tree to value-key.
|
||||
// Note that you should guarantee the value is the same type as key,
|
||||
// or else the comparator would panic.
|
||||
|
||||
@ -12,6 +12,8 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
|
||||
"github.com/gogf/gf/container/gvar"
|
||||
"github.com/gogf/gf/internal/rwmutex"
|
||||
)
|
||||
@ -302,6 +304,16 @@ func (tree *BTree) Map() map[interface{}]interface{} {
|
||||
return m
|
||||
}
|
||||
|
||||
// MapStrAny returns all key-value items as map[string]interface{}.
|
||||
func (tree *BTree) MapStrAny() map[string]interface{} {
|
||||
m := make(map[string]interface{}, tree.Size())
|
||||
tree.IteratorAsc(func(key, value interface{}) bool {
|
||||
m[gconv.String(key)] = value
|
||||
return true
|
||||
})
|
||||
return m
|
||||
}
|
||||
|
||||
// Clear removes all nodes from the tree.
|
||||
func (tree *BTree) Clear() {
|
||||
tree.mu.Lock()
|
||||
|
||||
@ -10,6 +10,8 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
|
||||
"github.com/gogf/gf/container/gvar"
|
||||
"github.com/gogf/gf/internal/rwmutex"
|
||||
)
|
||||
@ -345,6 +347,16 @@ func (tree *RedBlackTree) Map() map[interface{}]interface{} {
|
||||
return m
|
||||
}
|
||||
|
||||
// MapStrAny returns all key-value items as map[string]interface{}.
|
||||
func (tree *RedBlackTree) MapStrAny() map[string]interface{} {
|
||||
m := make(map[string]interface{}, tree.Size())
|
||||
tree.IteratorAsc(func(key, value interface{}) bool {
|
||||
m[gconv.String(key)] = value
|
||||
return true
|
||||
})
|
||||
return m
|
||||
}
|
||||
|
||||
// Left returns the left-most (min) node or nil if tree is empty.
|
||||
func (tree *RedBlackTree) Left() *RedBlackTreeNode {
|
||||
tree.mu.RLock()
|
||||
|
||||
@ -220,11 +220,63 @@ func (v *Var) Map(tags ...string) map[string]interface{} {
|
||||
return gconv.Map(v.Val(), tags...)
|
||||
}
|
||||
|
||||
// MapStrStr converts <v> to map[string]string.
|
||||
func (v *Var) MapStrStr(tags ...string) map[string]string {
|
||||
m := v.Map(tags...)
|
||||
if len(m) > 0 {
|
||||
vMap := make(map[string]string)
|
||||
for k, v := range m {
|
||||
vMap[k] = gconv.String(v)
|
||||
}
|
||||
return vMap
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// MapStrVar converts <v> to map[string]*Var.
|
||||
func (v *Var) MapStrVar(tags ...string) map[string]*Var {
|
||||
m := v.Map(tags...)
|
||||
if len(m) > 0 {
|
||||
vMap := make(map[string]*Var)
|
||||
for k, v := range m {
|
||||
vMap[k] = New(v)
|
||||
}
|
||||
return vMap
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// MapDeep converts <v> to map[string]interface{} recursively.
|
||||
func (v *Var) MapDeep(tags ...string) map[string]interface{} {
|
||||
return gconv.MapDeep(v.Val(), tags...)
|
||||
}
|
||||
|
||||
// MapDeep converts <v> to map[string]string recursively.
|
||||
func (v *Var) MapStrStrDeep(tags ...string) map[string]string {
|
||||
m := v.MapDeep(tags...)
|
||||
if len(m) > 0 {
|
||||
vMap := make(map[string]string)
|
||||
for k, v := range m {
|
||||
vMap[k] = gconv.String(v)
|
||||
}
|
||||
return vMap
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// MapStrVarDeep converts <v> to map[string]*Var recursively.
|
||||
func (v *Var) MapStrVarDeep(tags ...string) map[string]*Var {
|
||||
m := v.MapDeep(tags...)
|
||||
if len(m) > 0 {
|
||||
vMap := make(map[string]*Var)
|
||||
for k, v := range m {
|
||||
vMap[k] = New(v)
|
||||
}
|
||||
return vMap
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Struct maps value of <v> to <pointer>.
|
||||
// The parameter <pointer> should be a pointer to a struct instance.
|
||||
// The parameter <mapping> is used to specify the key-to-attribute mapping rules.
|
||||
@ -249,26 +301,26 @@ func (v *Var) StructsDeep(pointer interface{}, mapping ...map[string]string) (er
|
||||
return gconv.StructsDeep(v.Val(), pointer, mapping...)
|
||||
}
|
||||
|
||||
// MapStruct converts map type variable <params> to another map type variable <pointer>.
|
||||
// MapToMap converts map type variable <params> to another map type variable <pointer>.
|
||||
// The elements of <pointer> should be type of struct/*struct.
|
||||
func (v *Var) MapStruct(pointer interface{}, mapping ...map[string]string) (err error) {
|
||||
return gconv.MapStruct(v.Val(), pointer, mapping...)
|
||||
func (v *Var) MapToMap(pointer interface{}, mapping ...map[string]string) (err error) {
|
||||
return gconv.MapToMap(v.Val(), pointer, mapping...)
|
||||
}
|
||||
|
||||
// MapStructDeep recursively converts map type variable <params> to another map type variable <pointer>.
|
||||
// MapToMapDeep recursively converts map type variable <params> to another map type variable <pointer>.
|
||||
// The elements of <pointer> should be type of struct/*struct.
|
||||
func (v *Var) MapStructDeep(pointer interface{}, mapping ...map[string]string) (err error) {
|
||||
return gconv.MapStructDeep(v.Val(), pointer, mapping...)
|
||||
func (v *Var) MapToMapDeep(pointer interface{}, mapping ...map[string]string) (err error) {
|
||||
return gconv.MapToMapDeep(v.Val(), pointer, mapping...)
|
||||
}
|
||||
|
||||
// MapStructs converts map type variable <params> to another map type variable <pointer>.
|
||||
// MapToMaps converts map type variable <params> to another map type variable <pointer>.
|
||||
// The elements of <pointer> should be type of []struct/[]*struct.
|
||||
func (v *Var) MapStructs(pointer interface{}, mapping ...map[string]string) (err error) {
|
||||
return gconv.MapStructs(v.Val(), pointer, mapping...)
|
||||
func (v *Var) MapToMaps(pointer interface{}, mapping ...map[string]string) (err error) {
|
||||
return gconv.MapToMaps(v.Val(), pointer, mapping...)
|
||||
}
|
||||
|
||||
// MapStructsDeep recursively converts map type variable <params> to another map type variable <pointer>.
|
||||
// MapToMapsDeep recursively converts map type variable <params> to another map type variable <pointer>.
|
||||
// The elements of <pointer> should be type of []struct/[]*struct.
|
||||
func (v *Var) MapStructsDeep(pointer interface{}, mapping ...map[string]string) (err error) {
|
||||
return gconv.MapStructsDeep(v.Val(), pointer, mapping...)
|
||||
func (v *Var) MapToMapsDeep(pointer interface{}, mapping ...map[string]string) (err error) {
|
||||
return gconv.MapToMapsDeep(v.Val(), pointer, mapping...)
|
||||
}
|
||||
|
||||
@ -90,7 +90,7 @@ type DB interface {
|
||||
SetLogger(logger *glog.Logger)
|
||||
SetMaxIdleConnCount(n int)
|
||||
SetMaxOpenConnCount(n int)
|
||||
SetMaxConnLifetime(n int)
|
||||
SetMaxConnLifetime(d time.Duration)
|
||||
Tables() (tables []string, err error)
|
||||
TableFields(table string) (map[string]*TableField, error)
|
||||
|
||||
@ -126,7 +126,7 @@ type dbBase struct {
|
||||
logger *glog.Logger // 日志管理对象
|
||||
maxIdleConnCount int // 连接池最大限制的连接数
|
||||
maxOpenConnCount int // 连接池最大打开的连接数
|
||||
maxConnLifetime int // (单位秒)连接对象可重复使用的时间长度
|
||||
maxConnLifetime time.Duration // 连接对象可重复使用的时间长度
|
||||
}
|
||||
|
||||
// 执行的SQL对象
|
||||
|
||||
@ -27,7 +27,7 @@ import (
|
||||
|
||||
const (
|
||||
gDEFAULT_DEBUG_SQL_LENGTH = 1000
|
||||
gPATH_FILTER_KEY = "/gf/database/gdb/gdb"
|
||||
gPATH_FILTER_KEY = "/database/gdb/gdb"
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
@ -9,6 +9,7 @@ package gdb
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/os/glog"
|
||||
|
||||
@ -27,20 +28,20 @@ type ConfigGroup []ConfigNode
|
||||
|
||||
// 数据库单项配置
|
||||
type ConfigNode struct {
|
||||
Host string // 地址
|
||||
Port string // 端口
|
||||
User string // 账号
|
||||
Pass string // 密码
|
||||
Name string // 数据库名称
|
||||
Type string // 数据库类型:mysql, sqlite, mssql, pgsql, oracle
|
||||
Role string // (可选,默认为master)数据库的角色,用于主从操作分离,至少需要有一个master,参数值:master, slave
|
||||
Debug bool // (可选)开启调试模式
|
||||
Weight int // (可选)用于负载均衡的权重计算,当集群中只有一个节点时,权重没有任何意义
|
||||
Charset string // (可选,默认为 utf8)编码,默认为 utf8
|
||||
LinkInfo string // (可选)自定义链接信息,当该字段被设置值时,以上链接字段(Host,Port,User,Pass,Name)将失效(该字段是一个扩展功能)
|
||||
MaxIdleConnCount int // (可选)连接池最大限制的连接数
|
||||
MaxOpenConnCount int // (可选)连接池最大打开的连接数
|
||||
MaxConnLifetime int // (可选,单位秒)连接对象可重复使用的时间长度
|
||||
Host string // 地址
|
||||
Port string // 端口
|
||||
User string // 账号
|
||||
Pass string // 密码
|
||||
Name string // 数据库名称
|
||||
Type string // 数据库类型:mysql, sqlite, mssql, pgsql, oracle
|
||||
Role string // (可选,默认为master)数据库的角色,用于主从操作分离,至少需要有一个master,参数值:master, slave
|
||||
Debug bool // (可选)开启调试模式
|
||||
Weight int // (可选)用于负载均衡的权重计算,当集群中只有一个节点时,权重没有任何意义
|
||||
Charset string // (可选,默认为 utf8)编码,默认为 utf8
|
||||
LinkInfo string // (可选)自定义链接信息,当该字段被设置值时,以上链接字段(Host,Port,User,Pass,Name)将失效(该字段是一个扩展功能)
|
||||
MaxIdleConnCount int // (可选)连接池最大限制的连接数
|
||||
MaxOpenConnCount int // (可选)连接池最大打开的连接数
|
||||
MaxConnLifetime time.Duration // (可选)连接对象可重复使用的时间长度
|
||||
}
|
||||
|
||||
// 数据库配置包内对象
|
||||
@ -130,8 +131,8 @@ func (bs *dbBase) SetMaxOpenConnCount(n int) {
|
||||
|
||||
// 设置数据库连接可重复利用的时间,超过该时间则被关闭废弃
|
||||
// 如果 d <= 0 表示该链接会一直重复利用
|
||||
func (bs *dbBase) SetMaxConnLifetime(n int) {
|
||||
bs.maxConnLifetime = n
|
||||
func (bs *dbBase) SetMaxConnLifetime(d time.Duration) {
|
||||
bs.maxConnLifetime = d
|
||||
}
|
||||
|
||||
// 节点配置转换为字符串
|
||||
|
||||
@ -12,17 +12,24 @@ import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/encoding/ghash"
|
||||
|
||||
"github.com/gogf/gf/crypto/gmd5"
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
)
|
||||
|
||||
const (
|
||||
gMAX_DEPTH = 1000
|
||||
gFILTER_KEY = "/gf/debug/gdebug/gdebug.go"
|
||||
gFILTER_KEY = "/debug/gdebug/gdebug.go"
|
||||
)
|
||||
|
||||
var (
|
||||
// goRootForFilter is used for stack filtering purpose.
|
||||
goRootForFilter = runtime.GOROOT()
|
||||
goRootForFilter = runtime.GOROOT() // goRootForFilter is used for stack filtering purpose.
|
||||
binaryVersion = "" // The version of current running binary(uint64 hex).
|
||||
binaryVersionMd5 = "" // The version of current running binary(MD5).
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -31,6 +38,27 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
// BinVersion returns the version of current running binary.
|
||||
// It uses ghash.BKDRHash+BASE36 algorithm to calculate the unique version of the binary.
|
||||
func BinVersion() string {
|
||||
if binaryVersion == "" {
|
||||
binaryVersion = strconv.FormatInt(
|
||||
int64(ghash.BKDRHash(gfile.GetBytes(gfile.SelfPath()))),
|
||||
36,
|
||||
)
|
||||
}
|
||||
return binaryVersion
|
||||
}
|
||||
|
||||
// BinVersionMd5 returns the version of current running binary.
|
||||
// It uses MD5 algorithm to calculate the unique version of the binary.
|
||||
func BinVersionMd5() string {
|
||||
if binaryVersionMd5 == "" {
|
||||
binaryVersionMd5, _ = gmd5.EncryptFile(gfile.SelfPath())
|
||||
}
|
||||
return binaryVersionMd5
|
||||
}
|
||||
|
||||
// PrintStack prints to standard error the stack trace returned by runtime.Stack.
|
||||
func PrintStack(skip ...int) {
|
||||
fmt.Print(Stack(skip...))
|
||||
|
||||
@ -25,8 +25,13 @@ func Decode(dst []byte) ([]byte, error) {
|
||||
return src[:n], err
|
||||
}
|
||||
|
||||
// EncodeString encodes bytes with BASE64 algorithm.
|
||||
func EncodeString(src []byte) string {
|
||||
// EncodeString encodes string with BASE64 algorithm.
|
||||
func EncodeString(src string) string {
|
||||
return EncodeToString([]byte(src))
|
||||
}
|
||||
|
||||
// EncodeToString encodes bytes to string with BASE64 algorithm.
|
||||
func EncodeToString(src []byte) string {
|
||||
return string(Encode(src))
|
||||
}
|
||||
|
||||
@ -34,3 +39,9 @@ func EncodeString(src []byte) string {
|
||||
func DecodeString(str string) ([]byte, error) {
|
||||
return Decode([]byte(str))
|
||||
}
|
||||
|
||||
// DecodeString decodes string with BASE64 algorithm.
|
||||
func DecodeToString(str string) (string, error) {
|
||||
b, err := DecodeString(str)
|
||||
return string(b), err
|
||||
}
|
||||
|
||||
@ -12,11 +12,11 @@ import (
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
)
|
||||
|
||||
type testpair struct {
|
||||
type testPair struct {
|
||||
decoded, encoded string
|
||||
}
|
||||
|
||||
var pairs = []testpair{
|
||||
var pairs = []testPair{
|
||||
// RFC 3548 examples
|
||||
{"\x14\xfb\x9c\x03\xd9\x7e", "FPucA9l+"},
|
||||
{"\x14\xfb\x9c\x03\xd9", "FPucA9k="},
|
||||
@ -45,15 +45,20 @@ var pairs = []testpair{
|
||||
func TestBase64(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
for k := range pairs {
|
||||
// []byte
|
||||
// Encode
|
||||
gtest.Assert(gbase64.Encode([]byte(pairs[k].decoded)), []byte(pairs[k].encoded))
|
||||
e1, _ := gbase64.Decode([]byte(pairs[k].encoded))
|
||||
gtest.Assert(e1, []byte(pairs[k].decoded))
|
||||
gtest.Assert(gbase64.EncodeToString([]byte(pairs[k].decoded)), pairs[k].encoded)
|
||||
gtest.Assert(gbase64.EncodeString(pairs[k].decoded), pairs[k].encoded)
|
||||
|
||||
// string
|
||||
gtest.Assert(gbase64.EncodeString([]byte(pairs[k].decoded)), pairs[k].encoded)
|
||||
e2, _ := gbase64.DecodeString(pairs[k].encoded)
|
||||
gtest.Assert(e2, []byte(pairs[k].decoded))
|
||||
// Decode
|
||||
r1, _ := gbase64.Decode([]byte(pairs[k].encoded))
|
||||
gtest.Assert(r1, []byte(pairs[k].decoded))
|
||||
|
||||
r2, _ := gbase64.DecodeString(pairs[k].encoded)
|
||||
gtest.Assert(r2, []byte(pairs[k].decoded))
|
||||
|
||||
r3, _ := gbase64.DecodeToString(pairs[k].encoded)
|
||||
gtest.Assert(r3, pairs[k].decoded)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@ -296,20 +296,20 @@ func (j *Json) GetStructsDeep(pattern string, pointer interface{}, mapping ...ma
|
||||
return gconv.StructsDeep(j.Get(pattern), pointer, mapping...)
|
||||
}
|
||||
|
||||
func (j *Json) GetMapStruct(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
return gconv.MapStruct(j.Get(pattern), pointer, mapping...)
|
||||
func (j *Json) GetMapToMap(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
return gconv.MapToMap(j.Get(pattern), pointer, mapping...)
|
||||
}
|
||||
|
||||
func (j *Json) GetMapStructDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
return gconv.MapStructDeep(j.Get(pattern), pointer, mapping...)
|
||||
func (j *Json) GetMapToMapDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
return gconv.MapToMapDeep(j.Get(pattern), pointer, mapping...)
|
||||
}
|
||||
|
||||
func (j *Json) GetMapStructs(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
return gconv.MapStructs(j.Get(pattern), pointer, mapping...)
|
||||
func (j *Json) GetMapToMaps(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
return gconv.MapToMaps(j.Get(pattern), pointer, mapping...)
|
||||
}
|
||||
|
||||
func (j *Json) GetMapStructsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
return gconv.MapStructsDeep(j.Get(pattern), pointer, mapping...)
|
||||
func (j *Json) GetMapToMapsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
return gconv.MapToMapsDeep(j.Get(pattern), pointer, mapping...)
|
||||
}
|
||||
|
||||
// ToMap converts current Json object to map[string]interface{}.
|
||||
@ -354,28 +354,28 @@ func (j *Json) ToStructsDeep(pointer interface{}, mapping ...map[string]string)
|
||||
return gconv.StructsDeep(*(j.p), pointer, mapping...)
|
||||
}
|
||||
|
||||
func (j *Json) ToMapStruct(pointer interface{}, mapping ...map[string]string) error {
|
||||
func (j *Json) ToMapToMap(pointer interface{}, mapping ...map[string]string) error {
|
||||
j.mu.RLock()
|
||||
defer j.mu.RUnlock()
|
||||
return gconv.MapStruct(*(j.p), pointer, mapping...)
|
||||
return gconv.MapToMap(*(j.p), pointer, mapping...)
|
||||
}
|
||||
|
||||
func (j *Json) ToMapStructDeep(pointer interface{}, mapping ...map[string]string) error {
|
||||
func (j *Json) ToMapToMapDeep(pointer interface{}, mapping ...map[string]string) error {
|
||||
j.mu.RLock()
|
||||
defer j.mu.RUnlock()
|
||||
return gconv.MapStructDeep(*(j.p), pointer, mapping...)
|
||||
return gconv.MapToMapDeep(*(j.p), pointer, mapping...)
|
||||
}
|
||||
|
||||
func (j *Json) ToMapStructs(pointer interface{}, mapping ...map[string]string) error {
|
||||
func (j *Json) ToMapToMaps(pointer interface{}, mapping ...map[string]string) error {
|
||||
j.mu.RLock()
|
||||
defer j.mu.RUnlock()
|
||||
return gconv.MapStructs(*(j.p), pointer, mapping...)
|
||||
return gconv.MapToMaps(*(j.p), pointer, mapping...)
|
||||
}
|
||||
|
||||
func (j *Json) ToMapStructsDeep(pointer interface{}, mapping ...map[string]string) error {
|
||||
func (j *Json) ToMapToMapsDeep(pointer interface{}, mapping ...map[string]string) error {
|
||||
j.mu.RLock()
|
||||
defer j.mu.RUnlock()
|
||||
return gconv.MapStructsDeep(*(j.p), pointer, mapping...)
|
||||
return gconv.MapToMapsDeep(*(j.p), pointer, mapping...)
|
||||
}
|
||||
|
||||
// Dump prints current Json object with more manually readable.
|
||||
|
||||
@ -12,6 +12,23 @@ import (
|
||||
"github.com/gogf/gf/encoding/gjson"
|
||||
)
|
||||
|
||||
var (
|
||||
jsonStr1 = `[1,2,3]`
|
||||
jsonStr2 = `{"CallbackCommand":"Group.CallbackAfterSendMsg","From_Account":"61934946","GroupId":"@TGS#2FLGX67FD","MsgBody":[{"MsgContent":{"Text":"是的"},"MsgType":"TIMTextElem"}],"MsgSeq":23,"MsgTime":1567032819,"Operator_Account":"61934946","Random":2804799576,"Type":"Public"}`
|
||||
)
|
||||
|
||||
func Benchmark_Validate1(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
gjson.Valid(jsonStr1)
|
||||
}
|
||||
}
|
||||
|
||||
func Benchmark_Validate2(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
gjson.Valid(jsonStr2)
|
||||
}
|
||||
}
|
||||
|
||||
func Benchmark_Set1(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
p := gjson.New(map[string]string{
|
||||
|
||||
@ -10,25 +10,24 @@ package gyaml
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/gf-third/yaml"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
|
||||
yaml3 "gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func Encode(v interface{}) ([]byte, error) {
|
||||
return yaml3.Marshal(v)
|
||||
return yaml.Marshal(v)
|
||||
}
|
||||
|
||||
func Decode(v []byte) (interface{}, error) {
|
||||
var result map[string]interface{}
|
||||
if err := yaml3.Unmarshal(v, &result); err != nil {
|
||||
if err := yaml.Unmarshal(v, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return gconv.Map(result), nil
|
||||
return gconv.MapDeep(result), nil
|
||||
}
|
||||
|
||||
func DecodeTo(v []byte, result interface{}) error {
|
||||
return yaml3.Unmarshal(v, result)
|
||||
return yaml.Unmarshal(v, result)
|
||||
}
|
||||
|
||||
func ToJson(v []byte) ([]byte, error) {
|
||||
|
||||
@ -22,7 +22,7 @@ type Error struct {
|
||||
}
|
||||
|
||||
const (
|
||||
gFILTER_KEY = "/gf/errors/gerror/gerror"
|
||||
gFILTER_KEY = "/errors/gerror/gerror"
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
@ -6,9 +6,11 @@
|
||||
|
||||
package g
|
||||
|
||||
import "github.com/gogf/gf/container/gvar"
|
||||
import (
|
||||
"github.com/gogf/gf/container/gvar"
|
||||
)
|
||||
|
||||
// Universal variable type, like generics.
|
||||
// Var is a universal variable type, like generics.
|
||||
type Var = gvar.Var
|
||||
|
||||
// Frequently-used map type alias.
|
||||
|
||||
@ -215,10 +215,10 @@ func parseDBConfigNode(value interface{}) *gdb.ConfigNode {
|
||||
node.MaxOpenConnCount = gconv.Int(value)
|
||||
}
|
||||
if value, ok := nodeMap["max-lifetime"]; ok {
|
||||
node.MaxConnLifetime = gconv.Int(value)
|
||||
node.MaxConnLifetime = gconv.Duration(value)
|
||||
}
|
||||
if value, ok := nodeMap["maxLifetime"]; ok {
|
||||
node.MaxConnLifetime = gconv.Int(value)
|
||||
node.MaxConnLifetime = gconv.Duration(value)
|
||||
}
|
||||
// Parse link syntax.
|
||||
if node.LinkInfo != "" && node.Type == "" {
|
||||
|
||||
10
go.mod
10
go.mod
@ -6,15 +6,13 @@ require (
|
||||
github.com/fatih/structs v1.1.0
|
||||
github.com/fsnotify/fsnotify v1.4.7
|
||||
github.com/gf-third/mysql v1.4.2
|
||||
github.com/gofrs/flock v0.7.1 // indirect
|
||||
github.com/gf-third/yaml v1.0.1
|
||||
github.com/gomodule/redigo v2.0.0+incompatible
|
||||
github.com/gorilla/websocket v1.4.1
|
||||
github.com/grokify/html-strip-tags-go v0.0.0-20190424092004-025bd760b278
|
||||
github.com/grokify/html-strip-tags-go v0.0.0-20190921062105-daaa06bf1aaf
|
||||
github.com/mattn/go-runewidth v0.0.4 // indirect
|
||||
github.com/olekukonko/tablewriter v0.0.1
|
||||
github.com/theckman/go-flock v0.7.1
|
||||
golang.org/x/sys v0.0.0-20190910064555-bbd175535a8b // indirect
|
||||
golang.org/x/sys v0.0.0-20190924092210-98129a5cf4a0 // indirect
|
||||
golang.org/x/text v0.3.2
|
||||
google.golang.org/appengine v1.6.2 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.0-20190905181640-827449938966
|
||||
google.golang.org/appengine v1.6.3 // indirect
|
||||
)
|
||||
|
||||
49
go.sum
49
go.sum
@ -1,49 +0,0 @@
|
||||
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/clbanning/mxj v1.8.4 h1:HuhwZtbyvyOw+3Z1AowPkU87JkJUSv751ELWaiTpj8I=
|
||||
github.com/clbanning/mxj v1.8.4/go.mod h1:BVjHeAH+rl9rs6f+QIpeRl0tfu10SXn1pUSa5PVGJng=
|
||||
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
|
||||
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
|
||||
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
|
||||
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
||||
github.com/gf-third/mysql v1.4.2 h1:f1M5CNFUG3WkE07UOomtu4o0n/KJKeuUUf5Nc9ZFXs4=
|
||||
github.com/gf-third/mysql v1.4.2/go.mod h1:+dd90V663ppI2fV5uQ6+rHk0u8KCyU6FkG8Um8Cx3ms=
|
||||
github.com/gofrs/flock v0.7.1 h1:DP+LD/t0njgoPBvT5MJLeliUIVQR03hiKR6vezdwHlc=
|
||||
github.com/gofrs/flock v0.7.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
|
||||
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
|
||||
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/gomodule/redigo v2.0.0+incompatible h1:K/R+8tc58AaqLkqG2Ol3Qk+DR/TlNuhuh457pBFPtt0=
|
||||
github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
|
||||
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
|
||||
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/grokify/html-strip-tags-go v0.0.0-20190424092004-025bd760b278 h1:DZo48DQFIDo/YWjUeFip1dfJztBhRuaxfUnPd+gAfcs=
|
||||
github.com/grokify/html-strip-tags-go v0.0.0-20190424092004-025bd760b278/go.mod h1:Xk7G0nwBiIloTMbLddk4WWJOqi4i/JLhadLd0HUXO30=
|
||||
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
|
||||
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
|
||||
github.com/olekukonko/tablewriter v0.0.1 h1:b3iUnf1v+ppJiOfNX4yxxqfWKMQPZR5yoh8urCTFX88=
|
||||
github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
|
||||
github.com/theckman/go-flock v0.7.1 h1:YdJyIjDuQdEU7voZ9YaeXSO4OnrxdI+WejPUwyZ/Txs=
|
||||
github.com/theckman/go-flock v0.7.1/go.mod h1:kjuth3y9VJ2aNlkNEO99G/8lp9fMIKaGyBmh84IBheM=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190603091049-60506f45cf65 h1:+rhAzEzT3f4JtomfC371qB+0Ola2caSKcY69NUBZrRQ=
|
||||
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190910064555-bbd175535a8b h1:3S2h5FadpNr0zUUCVZjlKIEYF+KaX/OBplTGo89CYHI=
|
||||
golang.org/x/sys v0.0.0-20190910064555-bbd175535a8b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
|
||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
|
||||
google.golang.org/appengine v1.6.2 h1:j8RI1yW0SkI+paT6uGwMlrMI/6zwYA6/CFil8rxOzGI=
|
||||
google.golang.org/appengine v1.6.2/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20190905181640-827449938966 h1:B0J02caTR6tpSJozBJyiAzT6CtBzjclw4pgm9gg8Ys0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20190905181640-827449938966/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
@ -38,8 +38,11 @@ type Request struct {
|
||||
hasServeHandler bool // 是否检索到服务函数
|
||||
parsedGet bool // GET参数是否已经解析
|
||||
parsedPost bool // POST参数是否已经解析
|
||||
queryVars map[string][]string // GET参数
|
||||
routerVars map[string][]string // 路由解析参数
|
||||
parsedRaw bool // 原始参数是否已经解析
|
||||
getMap map[string]interface{} // GET解析参数
|
||||
postMap map[string]interface{} // POST解析参数
|
||||
routerMap map[string]interface{} // 路由解析参数
|
||||
rawVarMap map[string]interface{} // 原始数据参数
|
||||
error error // 当前请求执行错误
|
||||
exit bool // 是否退出当前请求流程执行
|
||||
params map[string]interface{} // 开发者自定义参数(请求流程中有效)
|
||||
@ -52,17 +55,17 @@ type Request struct {
|
||||
// 创建一个Request对象
|
||||
func newRequest(s *Server, r *http.Request, w http.ResponseWriter) *Request {
|
||||
request := &Request{
|
||||
routerVars: make(map[string][]string),
|
||||
Id: s.servedCount.Add(1),
|
||||
Server: s,
|
||||
Request: r,
|
||||
Response: newResponse(s, w),
|
||||
EnterTime: gtime.Microsecond(),
|
||||
routerMap: make(map[string]interface{}),
|
||||
Id: s.servedCount.Add(1),
|
||||
Server: s,
|
||||
Request: r,
|
||||
Response: newResponse(s, w),
|
||||
EnterTime: gtime.Microsecond(),
|
||||
}
|
||||
// 会话处理
|
||||
request.Cookie = GetCookie(request)
|
||||
request.Session = s.sessionManager.New(request.GetSessionId())
|
||||
request.Response.request = request
|
||||
request.Response.Request = request
|
||||
request.Middleware = &Middleware{
|
||||
request: request,
|
||||
}
|
||||
@ -71,7 +74,7 @@ func newRequest(s *Server, r *http.Request, w http.ResponseWriter) *Request {
|
||||
|
||||
// 获取Web Socket连接对象(如果是非WS请求会失败,注意检查返回的error结果)
|
||||
func (r *Request) WebSocket() (*WebSocket, error) {
|
||||
if conn, err := wsUpgrader.Upgrade(r.Response.ResponseWriter.ResponseWriter, r.Request, nil); err == nil {
|
||||
if conn, err := wsUpgrader.Upgrade(r.Response.Writer, r.Request, nil); err == nil {
|
||||
return &WebSocket{
|
||||
conn,
|
||||
}, nil
|
||||
@ -82,8 +85,8 @@ func (r *Request) WebSocket() (*WebSocket, error) {
|
||||
|
||||
// 获得指定名称的参数字符串(Router/GET/POST),同 GetRequestString
|
||||
// 这是常用方法的简化别名
|
||||
func (r *Request) Get(key string, def ...interface{}) string {
|
||||
return r.GetRequestString(key, def...)
|
||||
func (r *Request) Get(key string, def ...interface{}) interface{} {
|
||||
return r.GetRequest(key, def...)
|
||||
}
|
||||
|
||||
// 建议都用该参数替代参数获取
|
||||
@ -105,14 +108,18 @@ func (r *Request) GetRawString() string {
|
||||
}
|
||||
|
||||
// 获取原始json请求输入字符串,并解析为json对象
|
||||
func (r *Request) GetJson() *gjson.Json {
|
||||
return gjson.New(r.GetRaw())
|
||||
func (r *Request) GetJson() (*gjson.Json, error) {
|
||||
return gjson.LoadJson(r.GetRaw())
|
||||
}
|
||||
|
||||
func (r *Request) GetString(key string, def ...interface{}) string {
|
||||
return r.GetRequestString(key, def...)
|
||||
}
|
||||
|
||||
func (r *Request) GetBool(key string, def ...interface{}) bool {
|
||||
return r.GetRequestBool(key, def...)
|
||||
}
|
||||
|
||||
func (r *Request) GetInt(key string, def ...interface{}) int {
|
||||
return r.GetRequestInt(key, def...)
|
||||
}
|
||||
@ -149,10 +156,14 @@ func (r *Request) GetInterfaces(key string, def ...interface{}) []interface{} {
|
||||
return r.GetRequestInterfaces(key, def...)
|
||||
}
|
||||
|
||||
func (r *Request) GetMap(def ...map[string]string) map[string]string {
|
||||
func (r *Request) GetMap(def ...map[string]interface{}) map[string]interface{} {
|
||||
return r.GetRequestMap(def...)
|
||||
}
|
||||
|
||||
func (r *Request) GetMapStrStr(def ...map[string]interface{}) map[string]string {
|
||||
return r.GetRequestMapStrStr(def...)
|
||||
}
|
||||
|
||||
// 将所有的request参数映射到struct属性上,参数pointer应当为一个struct对象的指针,
|
||||
// mapping为非必需参数,自定义参数与属性的映射关系
|
||||
func (r *Request) GetToStruct(pointer interface{}, mapping ...map[string]string) error {
|
||||
@ -236,14 +247,6 @@ func (r *Request) GetSessionId() string {
|
||||
return id
|
||||
}
|
||||
|
||||
// 生成随机的SESSIONID
|
||||
func (r *Request) MakeSessionId() string {
|
||||
id := gsession.NewSessionId()
|
||||
r.Cookie.SetSessionId(id)
|
||||
r.Response.Header().Set(r.Server.GetSessionIdName(), id)
|
||||
return id
|
||||
}
|
||||
|
||||
// 获得请求来源URL地址
|
||||
func (r *Request) GetReferer() string {
|
||||
return r.Header.Get("Referer")
|
||||
|
||||
@ -38,7 +38,7 @@ func (m *Middleware) Next() {
|
||||
}
|
||||
// 路由参数赋值
|
||||
for k, v := range item.values {
|
||||
m.request.routerVars[k] = v
|
||||
m.request.routerMap[k] = v
|
||||
}
|
||||
m.request.Router = item.handler.router
|
||||
// 执行函数处理
|
||||
|
||||
@ -17,14 +17,17 @@ func (r *Request) SetParam(key string, value interface{}) {
|
||||
}
|
||||
|
||||
// 获取请求流程共享变量
|
||||
func (r *Request) GetParam(key string, def ...interface{}) *gvar.Var {
|
||||
func (r *Request) GetParam(key string, def ...interface{}) interface{} {
|
||||
if r.params != nil {
|
||||
if v, ok := r.params[key]; ok {
|
||||
return gvar.New(v)
|
||||
}
|
||||
return r.params[key]
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return gvar.New(def[0])
|
||||
return def[0]
|
||||
}
|
||||
return gvar.New(nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获取请求流程共享变量
|
||||
func (r *Request) GetParamVar(key string, def ...interface{}) *gvar.Var {
|
||||
return gvar.New(r.GetParam(key, def...))
|
||||
}
|
||||
|
||||
@ -7,156 +7,176 @@
|
||||
package ghttp
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/encoding/gurl"
|
||||
|
||||
"github.com/gogf/gf/container/gvar"
|
||||
"github.com/gogf/gf/internal/structs"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
)
|
||||
|
||||
// 初始化POST请求参数
|
||||
func (r *Request) initPost() {
|
||||
if !r.parsedPost {
|
||||
// MultiMedia表单请求解析允许最大使用内存:1GB
|
||||
if r.ParseMultipartForm(1024*1024*1024) == nil {
|
||||
r.parsedPost = true
|
||||
r.parsedPost = true
|
||||
if v := r.Header.Get("Content-Type"); v != "" && gstr.Contains(v, "multipart/") {
|
||||
// multipart/form-data, multipart/mixed
|
||||
r.ParseMultipartForm(r.Server.config.FormParsingMemory)
|
||||
if len(r.PostForm) > 0 {
|
||||
// 重新组织数据格式,使用统一的数据Parse方式
|
||||
params := ""
|
||||
for name, values := range r.PostForm {
|
||||
if len(values) == 1 {
|
||||
if len(params) > 0 {
|
||||
params += "&"
|
||||
}
|
||||
params += name + "=" + gurl.Encode(values[0])
|
||||
} else {
|
||||
if len(name) > 2 && name[len(name)-2:] == "[]" {
|
||||
name = name[:len(name)-2]
|
||||
for _, v := range values {
|
||||
if len(params) > 0 {
|
||||
params += "&"
|
||||
}
|
||||
params += name + "[]=" + gurl.Encode(v)
|
||||
}
|
||||
} else {
|
||||
if len(params) > 0 {
|
||||
params += "&"
|
||||
}
|
||||
params += name + "=" + gurl.Encode(values[len(values)-1])
|
||||
}
|
||||
}
|
||||
}
|
||||
r.postMap, _ = gstr.Parse(params)
|
||||
}
|
||||
} else if strings.EqualFold(r.Method, "POST") {
|
||||
r.parsedRaw = true
|
||||
if raw := r.GetRawString(); len(raw) > 0 {
|
||||
r.postMap, _ = gstr.Parse(raw)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 设置POST参数,仅在ghttp.Server内有效,**注意并发安全性**
|
||||
func (r *Request) SetPost(key string, value string) {
|
||||
// 设置当前请求的POST参数
|
||||
func (r *Request) SetPost(key string, value interface{}) {
|
||||
r.initPost()
|
||||
r.PostForm[key] = []string{value}
|
||||
r.postMap[key] = value
|
||||
}
|
||||
|
||||
func (r *Request) AddPost(key string, value string) {
|
||||
func (r *Request) GetPost(key string, def ...interface{}) interface{} {
|
||||
r.initPost()
|
||||
r.PostForm[key] = append(r.PostForm[key], value)
|
||||
}
|
||||
|
||||
func (r *Request) GetPost(key string, def ...interface{}) []string {
|
||||
r.initPost()
|
||||
if v, ok := r.PostForm[key]; ok {
|
||||
if v, ok := r.postMap[key]; ok {
|
||||
return v
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return gconv.Strings(def[0])
|
||||
return def[0]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Request) GetPostVar(key string, def ...interface{}) *gvar.Var {
|
||||
return gvar.New(r.GetPostString(key, def...))
|
||||
return gvar.New(r.GetPost(key, def...))
|
||||
}
|
||||
|
||||
func (r *Request) GetPostString(key string, def ...interface{}) string {
|
||||
value := r.GetPost(key, def...)
|
||||
if value != nil && value[0] != "" {
|
||||
return value[0]
|
||||
}
|
||||
return ""
|
||||
return r.GetPostVar(key, def...).String()
|
||||
}
|
||||
|
||||
func (r *Request) GetPostBool(key string, def ...interface{}) bool {
|
||||
value := r.GetPostString(key, def...)
|
||||
if value != "" {
|
||||
return gconv.Bool(value)
|
||||
}
|
||||
return false
|
||||
return r.GetPostVar(key, def...).Bool()
|
||||
}
|
||||
|
||||
func (r *Request) GetPostInt(key string, def ...interface{}) int {
|
||||
value := r.GetPostString(key, def...)
|
||||
if value != "" {
|
||||
return gconv.Int(value)
|
||||
}
|
||||
return 0
|
||||
return r.GetPostVar(key, def...).Int()
|
||||
}
|
||||
|
||||
func (r *Request) GetPostInts(key string, def ...interface{}) []int {
|
||||
value := r.GetPost(key, def...)
|
||||
if value != nil {
|
||||
return gconv.Ints(value)
|
||||
}
|
||||
return nil
|
||||
return r.GetPostVar(key, def...).Ints()
|
||||
}
|
||||
|
||||
func (r *Request) GetPostUint(key string, def ...interface{}) uint {
|
||||
value := r.GetPostString(key, def...)
|
||||
if value != "" {
|
||||
return gconv.Uint(value)
|
||||
}
|
||||
return 0
|
||||
return r.GetPostVar(key, def...).Uint()
|
||||
}
|
||||
|
||||
func (r *Request) GetPostFloat32(key string, def ...interface{}) float32 {
|
||||
value := r.GetPostString(key, def...)
|
||||
if value != "" {
|
||||
return gconv.Float32(value)
|
||||
}
|
||||
return 0
|
||||
return r.GetPostVar(key, def...).Float32()
|
||||
}
|
||||
|
||||
func (r *Request) GetPostFloat64(key string, def ...interface{}) float64 {
|
||||
value := r.GetPostString(key, def...)
|
||||
if value != "" {
|
||||
return gconv.Float64(value)
|
||||
}
|
||||
return 0
|
||||
return r.GetPostVar(key, def...).Float64()
|
||||
}
|
||||
|
||||
func (r *Request) GetPostFloats(key string, def ...interface{}) []float64 {
|
||||
value := r.GetPost(key, def...)
|
||||
if value != nil {
|
||||
return gconv.Floats(value)
|
||||
}
|
||||
return nil
|
||||
return r.GetPostVar(key, def...).Floats()
|
||||
}
|
||||
|
||||
func (r *Request) GetPostArray(key string, def ...interface{}) []string {
|
||||
return r.GetPost(key, def...)
|
||||
return r.GetPostVar(key, def...).Strings()
|
||||
}
|
||||
|
||||
func (r *Request) GetPostStrings(key string, def ...interface{}) []string {
|
||||
return r.GetPost(key, def...)
|
||||
return r.GetPostVar(key, def...).Strings()
|
||||
}
|
||||
|
||||
func (r *Request) GetPostInterfaces(key string, def ...interface{}) []interface{} {
|
||||
value := r.GetPost(key, def...)
|
||||
if value != nil {
|
||||
return gconv.Interfaces(value)
|
||||
return r.GetPostVar(key, def...).Interfaces()
|
||||
}
|
||||
|
||||
// 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值。
|
||||
// 当不指定键值对关联数组时,默认获取POST方式提交的所有的提交键值对数据。
|
||||
func (r *Request) GetPostMap(kvMap ...map[string]interface{}) map[string]interface{} {
|
||||
r.initPost()
|
||||
if len(kvMap) > 0 {
|
||||
m := make(map[string]interface{})
|
||||
for k, defValue := range kvMap[0] {
|
||||
if postValue, ok := r.postMap[k]; ok {
|
||||
m[k] = postValue
|
||||
} else {
|
||||
m[k] = defValue
|
||||
}
|
||||
}
|
||||
return m
|
||||
} else {
|
||||
return r.postMap
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Request) GetPostMapStrStr(kvMap ...map[string]interface{}) map[string]string {
|
||||
postMap := r.GetPostMap(kvMap...)
|
||||
if len(postMap) > 0 {
|
||||
m := make(map[string]string)
|
||||
for k, v := range postMap {
|
||||
m[k] = gconv.String(v)
|
||||
}
|
||||
return m
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值
|
||||
// 需要注意的是,如果其中一个字段为数组形式,那么只会返回第一个元素,如果需要获取全部的元素,请使用GetPostArray获取特定字段内容
|
||||
func (r *Request) GetPostMap(def ...map[string]string) map[string]string {
|
||||
r.initPost()
|
||||
m := make(map[string]string)
|
||||
for k, v := range r.PostForm {
|
||||
m[k] = v[0]
|
||||
}
|
||||
if len(def) > 0 {
|
||||
for k, v := range def[0] {
|
||||
if _, ok := m[k]; !ok {
|
||||
m[k] = v
|
||||
}
|
||||
func (r *Request) GetPostMapStrVar(kvMap ...map[string]interface{}) map[string]*gvar.Var {
|
||||
postMap := r.GetPostMap(kvMap...)
|
||||
if len(postMap) > 0 {
|
||||
m := make(map[string]*gvar.Var)
|
||||
for k, v := range postMap {
|
||||
m[k] = gvar.New(v)
|
||||
}
|
||||
return m
|
||||
}
|
||||
return m
|
||||
return nil
|
||||
}
|
||||
|
||||
// 将所有的request参数映射到struct属性上,参数object应当为一个struct对象的指针, mapping为非必需参数,自定义参数与属性的映射关系
|
||||
func (r *Request) GetPostToStruct(pointer interface{}, mapping ...map[string]string) error {
|
||||
r.initPost()
|
||||
tagMap := structs.TagMapName(pointer, paramTagPriority, true)
|
||||
if len(mapping) > 0 {
|
||||
for k, v := range mapping[0] {
|
||||
tagMap[k] = v
|
||||
}
|
||||
}
|
||||
params := make(map[string]interface{})
|
||||
for k, v := range r.GetPostMap() {
|
||||
params[k] = v
|
||||
}
|
||||
return gconv.StructDeep(params, pointer, tagMap)
|
||||
return gconv.StructDeep(r.postMap, pointer, tagMap)
|
||||
}
|
||||
|
||||
@ -9,6 +9,8 @@ package ghttp
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
|
||||
"github.com/gogf/gf/container/gvar"
|
||||
|
||||
"github.com/gogf/gf/internal/structs"
|
||||
@ -18,156 +20,135 @@ import (
|
||||
// 初始化GET请求参数
|
||||
func (r *Request) initGet() {
|
||||
if !r.parsedGet {
|
||||
r.queryVars = r.URL.Query()
|
||||
if strings.EqualFold(r.Method, "GET") {
|
||||
r.parsedGet = true
|
||||
if r.URL.RawQuery != "" {
|
||||
r.getMap, _ = gstr.Parse(r.URL.RawQuery)
|
||||
} else if strings.EqualFold(r.Method, "GET") {
|
||||
r.parsedRaw = true
|
||||
if raw := r.GetRawString(); len(raw) > 0 {
|
||||
var array []string
|
||||
for _, item := range strings.Split(raw, "&") {
|
||||
array = strings.Split(item, "=")
|
||||
r.queryVars[array[0]] = append(r.queryVars[array[0]], array[1])
|
||||
}
|
||||
r.getMap, _ = gstr.Parse(raw)
|
||||
}
|
||||
}
|
||||
r.parsedGet = true
|
||||
}
|
||||
}
|
||||
|
||||
// 设置GET参数,仅在ghttp.Server内有效,**注意并发安全性**
|
||||
func (r *Request) SetQuery(key string, value string) {
|
||||
// 设置当前请求的GET参数
|
||||
func (r *Request) SetQuery(key string, value interface{}) {
|
||||
r.initGet()
|
||||
r.queryVars[key] = []string{value}
|
||||
}
|
||||
|
||||
// 添加GET参数,构成[]string
|
||||
func (r *Request) AddQuery(key string, value string) {
|
||||
r.initGet()
|
||||
r.queryVars[key] = append(r.queryVars[key], value)
|
||||
r.getMap[key] = value
|
||||
}
|
||||
|
||||
// 获得指定名称的get参数列表
|
||||
func (r *Request) GetQuery(key string, def ...interface{}) []string {
|
||||
func (r *Request) GetQuery(key string, def ...interface{}) interface{} {
|
||||
r.initGet()
|
||||
if v, ok := r.queryVars[key]; ok {
|
||||
if v, ok := r.getMap[key]; ok {
|
||||
return v
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return gconv.Strings(def[0])
|
||||
return def[0]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryVar(key string, def ...interface{}) *gvar.Var {
|
||||
return gvar.New(r.GetQueryString(key, def...))
|
||||
return gvar.New(r.GetQuery(key, def...))
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryString(key string, def ...interface{}) string {
|
||||
value := r.GetQuery(key, def...)
|
||||
if value != nil && value[0] != "" {
|
||||
return value[0]
|
||||
}
|
||||
return ""
|
||||
return r.GetQueryVar(key, def...).String()
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryBool(key string, def ...interface{}) bool {
|
||||
value := r.GetQueryString(key, def...)
|
||||
if value != "" {
|
||||
return gconv.Bool(value)
|
||||
}
|
||||
return false
|
||||
return r.GetQueryVar(key, def...).Bool()
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryInt(key string, def ...interface{}) int {
|
||||
value := r.GetQueryString(key, def...)
|
||||
if value != "" {
|
||||
return gconv.Int(value)
|
||||
}
|
||||
return 0
|
||||
return r.GetQueryVar(key, def...).Int()
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryInts(key string, def ...interface{}) []int {
|
||||
value := r.GetQuery(key, def...)
|
||||
if value != nil {
|
||||
return gconv.Ints(value)
|
||||
}
|
||||
return nil
|
||||
return r.GetQueryVar(key, def...).Ints()
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryUint(key string, def ...interface{}) uint {
|
||||
value := r.GetQueryString(key, def...)
|
||||
if value != "" {
|
||||
return gconv.Uint(value)
|
||||
}
|
||||
return 0
|
||||
return r.GetQueryVar(key, def...).Uint()
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryFloat32(key string, def ...interface{}) float32 {
|
||||
value := r.GetQueryString(key, def...)
|
||||
if value != "" {
|
||||
return gconv.Float32(value)
|
||||
}
|
||||
return 0
|
||||
return r.GetQueryVar(key, def...).Float32()
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryFloat64(key string, def ...interface{}) float64 {
|
||||
value := r.GetQueryString(key, def...)
|
||||
if value != "" {
|
||||
return gconv.Float64(value)
|
||||
}
|
||||
return 0
|
||||
return r.GetQueryVar(key, def...).Float64()
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryFloats(key string, def ...interface{}) []float64 {
|
||||
value := r.GetQuery(key, def...)
|
||||
if value != nil {
|
||||
return gconv.Floats(value)
|
||||
}
|
||||
return nil
|
||||
return r.GetQueryVar(key, def...).Floats()
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryArray(key string, def ...interface{}) []string {
|
||||
return r.GetQuery(key, def...)
|
||||
return r.GetQueryVar(key, def...).Strings()
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryStrings(key string, def ...interface{}) []string {
|
||||
return r.GetQuery(key, def...)
|
||||
return r.GetQueryVar(key, def...).Strings()
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryInterfaces(key string, def ...interface{}) []interface{} {
|
||||
value := r.GetQuery(key, def...)
|
||||
if value != nil {
|
||||
return gconv.Interfaces(value)
|
||||
return r.GetQueryVar(key, def...).Interfaces()
|
||||
}
|
||||
|
||||
// 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值。
|
||||
// 当不指定键值对关联数组时,默认获取GET方式提交的所有的提交键值对数据。
|
||||
func (r *Request) GetQueryMap(kvMap ...map[string]interface{}) map[string]interface{} {
|
||||
r.initGet()
|
||||
if len(kvMap) > 0 {
|
||||
m := make(map[string]interface{})
|
||||
for k, defValue := range kvMap[0] {
|
||||
if queryValue, ok := r.getMap[k]; ok {
|
||||
m[k] = queryValue
|
||||
} else {
|
||||
m[k] = defValue
|
||||
}
|
||||
}
|
||||
return m
|
||||
} else {
|
||||
return r.getMap
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Request) GetQueryMapStrStr(kvMap ...map[string]interface{}) map[string]string {
|
||||
queryMap := r.GetQueryMap(kvMap...)
|
||||
if len(queryMap) > 0 {
|
||||
m := make(map[string]string)
|
||||
for k, v := range queryMap {
|
||||
m[k] = gconv.String(v)
|
||||
}
|
||||
return m
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值
|
||||
func (r *Request) GetQueryMap(def ...map[string]string) map[string]string {
|
||||
r.initGet()
|
||||
m := make(map[string]string)
|
||||
for k, v := range r.queryVars {
|
||||
m[k] = v[0]
|
||||
}
|
||||
if len(def) > 0 {
|
||||
for k, v := range def[0] {
|
||||
if _, ok := m[k]; !ok {
|
||||
m[k] = v
|
||||
}
|
||||
func (r *Request) GetQueryMapStrVar(kvMap ...map[string]interface{}) map[string]*gvar.Var {
|
||||
queryMap := r.GetQueryMap(kvMap...)
|
||||
if len(queryMap) > 0 {
|
||||
m := make(map[string]*gvar.Var)
|
||||
for k, v := range queryMap {
|
||||
m[k] = gvar.New(v)
|
||||
}
|
||||
return m
|
||||
}
|
||||
return m
|
||||
return nil
|
||||
}
|
||||
|
||||
// 将所有的get参数映射到struct属性上,参数object应当为一个struct对象的指针, mapping为非必需参数,自定义参数与属性的映射关系
|
||||
func (r *Request) GetQueryToStruct(pointer interface{}, mapping ...map[string]string) error {
|
||||
tagmap := structs.TagMapName(pointer, paramTagPriority, true)
|
||||
r.initGet()
|
||||
tagMap := structs.TagMapName(pointer, paramTagPriority, true)
|
||||
if len(mapping) > 0 {
|
||||
for k, v := range mapping[0] {
|
||||
tagmap[k] = v
|
||||
tagMap[k] = v
|
||||
}
|
||||
}
|
||||
params := make(map[string]interface{})
|
||||
for k, v := range r.GetQueryMap() {
|
||||
params[k] = v
|
||||
}
|
||||
return gconv.StructDeep(params, pointer, tagmap)
|
||||
return gconv.StructDeep(r.getMap, pointer, tagMap)
|
||||
}
|
||||
|
||||
@ -9,129 +9,139 @@ package ghttp
|
||||
import (
|
||||
"github.com/gogf/gf/container/gvar"
|
||||
"github.com/gogf/gf/internal/structs"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
)
|
||||
|
||||
// 初始化RAW请求参数
|
||||
func (r *Request) initRaw() {
|
||||
if !r.parsedRaw {
|
||||
r.parsedRaw = true
|
||||
if raw := r.GetRawString(); len(raw) > 0 {
|
||||
r.rawVarMap, _ = gstr.Parse(raw)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获得router、post或者get提交的参数,如果有同名参数,那么按照router->get->post优先级进行覆盖
|
||||
func (r *Request) GetRequest(key string, def ...interface{}) []string {
|
||||
v := r.GetRouterArray(key)
|
||||
func (r *Request) GetRequest(key string, def ...interface{}) interface{} {
|
||||
v := r.GetRouterValue(key)
|
||||
if v == nil {
|
||||
v = r.GetQuery(key)
|
||||
}
|
||||
if v == nil {
|
||||
v = r.GetPost(key)
|
||||
}
|
||||
if v != nil {
|
||||
return v
|
||||
}
|
||||
r.initRaw()
|
||||
v = r.rawVarMap[key]
|
||||
if v == nil && len(def) > 0 {
|
||||
return gconv.Strings(def[0])
|
||||
return def[0]
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestVar(key string, def ...interface{}) *gvar.Var {
|
||||
value := r.GetRequest(key, def...)
|
||||
if value != nil {
|
||||
return gvar.New(value[0])
|
||||
}
|
||||
return gvar.New(nil)
|
||||
return gvar.New(r.GetRequest(key, def...))
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestString(key string, def ...interface{}) string {
|
||||
value := r.GetRequest(key, def...)
|
||||
if value != nil && value[0] != "" {
|
||||
return value[0]
|
||||
}
|
||||
return ""
|
||||
return r.GetRequestVar(key, def...).String()
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestBool(key string, def ...interface{}) bool {
|
||||
value := r.GetRequestString(key, def...)
|
||||
if value != "" {
|
||||
return gconv.Bool(value)
|
||||
}
|
||||
return false
|
||||
return r.GetRequestVar(key, def...).Bool()
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestInt(key string, def ...interface{}) int {
|
||||
value := r.GetRequestString(key, def...)
|
||||
if value != "" {
|
||||
return gconv.Int(value)
|
||||
}
|
||||
return 0
|
||||
return r.GetRequestVar(key, def...).Int()
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestInts(key string, def ...interface{}) []int {
|
||||
value := r.GetRequest(key, def...)
|
||||
if value != nil {
|
||||
return gconv.Ints(value)
|
||||
}
|
||||
return nil
|
||||
return r.GetRequestVar(key, def...).Ints()
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestUint(key string, def ...interface{}) uint {
|
||||
value := r.GetRequestString(key, def...)
|
||||
if value != "" {
|
||||
return gconv.Uint(value)
|
||||
}
|
||||
return 0
|
||||
return r.GetRequestVar(key, def...).Uint()
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestFloat32(key string, def ...interface{}) float32 {
|
||||
value := r.GetRequestString(key, def...)
|
||||
if value != "" {
|
||||
return gconv.Float32(value)
|
||||
}
|
||||
return 0
|
||||
return r.GetRequestVar(key, def...).Float32()
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestFloat64(key string, def ...interface{}) float64 {
|
||||
value := r.GetRequestString(key, def...)
|
||||
if value != "" {
|
||||
return gconv.Float64(value)
|
||||
}
|
||||
return 0
|
||||
return r.GetRequestVar(key, def...).Float64()
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestFloats(key string, def ...interface{}) []float64 {
|
||||
value := r.GetRequest(key, def...)
|
||||
if value != nil {
|
||||
return gconv.Floats(value)
|
||||
}
|
||||
return nil
|
||||
return r.GetRequestVar(key, def...).Floats()
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestArray(key string, def ...interface{}) []string {
|
||||
return r.GetRequest(key, def...)
|
||||
return r.GetRequestVar(key, def...).Strings()
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestStrings(key string, def ...interface{}) []string {
|
||||
return r.GetRequest(key, def...)
|
||||
return r.GetRequestVar(key, def...).Strings()
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestInterfaces(key string, def ...interface{}) []interface{} {
|
||||
value := r.GetRequest(key, def...)
|
||||
if value != nil {
|
||||
return gconv.Interfaces(value)
|
||||
}
|
||||
return nil
|
||||
return r.GetRequestVar(key, def...).Interfaces()
|
||||
}
|
||||
|
||||
// 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值
|
||||
// 需要注意的是,如果其中一个字段为数组形式,那么只会返回第一个元素,如果需要获取全部的元素,请使用GetRequestArray获取特定字段内容
|
||||
func (r *Request) GetRequestMap(def ...map[string]string) map[string]string {
|
||||
m := r.GetQueryMap()
|
||||
if len(m) == 0 {
|
||||
m = r.GetPostMap()
|
||||
}
|
||||
if len(def) > 0 {
|
||||
for k, v := range def[0] {
|
||||
if _, ok := m[k]; !ok {
|
||||
m[k] = v
|
||||
func (r *Request) GetRequestMap(kvMap ...map[string]interface{}) map[string]interface{} {
|
||||
r.initRaw()
|
||||
m := r.rawVarMap
|
||||
if len(kvMap) > 0 {
|
||||
m = make(map[string]interface{})
|
||||
for k, defValue := range kvMap[0] {
|
||||
if rawValue, ok := r.rawVarMap[k]; ok {
|
||||
m[k] = rawValue
|
||||
} else {
|
||||
m[k] = defValue
|
||||
}
|
||||
}
|
||||
}
|
||||
if m == nil {
|
||||
m = make(map[string]interface{})
|
||||
}
|
||||
for k, v := range r.GetPostMap(kvMap...) {
|
||||
m[k] = v
|
||||
}
|
||||
for k, v := range r.GetQueryMap(kvMap...) {
|
||||
m[k] = v
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestMapStrStr(kvMap ...map[string]interface{}) map[string]string {
|
||||
requestMap := r.GetRequestMap(kvMap...)
|
||||
if len(requestMap) > 0 {
|
||||
m := make(map[string]string)
|
||||
for k, v := range requestMap {
|
||||
m[k] = gconv.String(v)
|
||||
}
|
||||
return m
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Request) GetRequestMapStrVar(kvMap ...map[string]interface{}) map[string]*gvar.Var {
|
||||
requestMap := r.GetRequestMap(kvMap...)
|
||||
if len(requestMap) > 0 {
|
||||
m := make(map[string]*gvar.Var)
|
||||
for k, v := range requestMap {
|
||||
m[k] = gvar.New(v)
|
||||
}
|
||||
return m
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 将所有的request参数映射到struct属性上,参数object应当为一个struct对象的指针, mapping为非必需参数,自定义参数与属性的映射关系
|
||||
func (r *Request) GetRequestToStruct(pointer interface{}, mapping ...map[string]string) error {
|
||||
tagMap := structs.TagMapName(pointer, paramTagPriority, true)
|
||||
@ -140,14 +150,5 @@ func (r *Request) GetRequestToStruct(pointer interface{}, mapping ...map[string]
|
||||
tagMap[k] = v
|
||||
}
|
||||
}
|
||||
params := make(map[string]interface{})
|
||||
for k, v := range r.GetRequestMap() {
|
||||
params[k] = v
|
||||
}
|
||||
if len(params) == 0 {
|
||||
if j := r.GetJson(); j != nil {
|
||||
params = j.ToMap()
|
||||
}
|
||||
}
|
||||
return gconv.StructDeep(params, pointer, tagMap)
|
||||
return gconv.StructDeep(r.GetRequestMap(), pointer, tagMap)
|
||||
}
|
||||
|
||||
@ -6,26 +6,28 @@
|
||||
|
||||
package ghttp
|
||||
|
||||
func (r *Request) SetRouterString(key, value string) {
|
||||
r.routerVars[key] = []string{value}
|
||||
}
|
||||
import "github.com/gogf/gf/container/gvar"
|
||||
|
||||
func (r *Request) AddRouterString(key, value string) {
|
||||
r.routerVars[key] = append(r.routerVars[key], value)
|
||||
func (r *Request) SetRouterValue(key string, value interface{}) {
|
||||
r.routerMap[key] = value
|
||||
}
|
||||
|
||||
// 获得路由解析参数
|
||||
func (r *Request) GetRouterString(key string) string {
|
||||
if v := r.GetRouterArray(key); v != nil {
|
||||
return v[0]
|
||||
func (r *Request) GetRouterValue(key string, def ...interface{}) interface{} {
|
||||
if r.routerMap != nil {
|
||||
return r.routerMap[key]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// 获得路由解析参数
|
||||
func (r *Request) GetRouterArray(key string) []string {
|
||||
if v, ok := r.routerVars[key]; ok {
|
||||
return v
|
||||
if len(def) > 0 {
|
||||
return def[0]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获得路由解析参数
|
||||
func (r *Request) GetRouterVar(key string, def ...interface{}) *gvar.Var {
|
||||
return gvar.New(r.GetRouterValue(key, def...))
|
||||
}
|
||||
|
||||
func (r *Request) GetRouterString(key string, def ...interface{}) string {
|
||||
return r.GetRouterVar(key, def...).String()
|
||||
}
|
||||
|
||||
@ -23,22 +23,22 @@ import (
|
||||
// 服务端请求返回对象。
|
||||
// 注意该对象并没有实现http.ResponseWriter接口,而是依靠ghttp.ResponseWriter实现。
|
||||
type Response struct {
|
||||
ResponseWriter
|
||||
Server *Server // 所属Web Server
|
||||
Writer *ResponseWriter // ResponseWriter的别名
|
||||
request *Request // 关联的Request请求对象
|
||||
*ResponseWriter // Underlying ResponseWriter.
|
||||
Server *Server // Parent server.
|
||||
Writer *ResponseWriter // Alias of ResponseWriter.
|
||||
Request *Request // According request.
|
||||
}
|
||||
|
||||
// 创建一个ghttp.Response对象指针
|
||||
func newResponse(s *Server, w http.ResponseWriter) *Response {
|
||||
r := &Response{
|
||||
Server: s,
|
||||
ResponseWriter: ResponseWriter{
|
||||
ResponseWriter: w,
|
||||
buffer: bytes.NewBuffer(nil),
|
||||
ResponseWriter: &ResponseWriter{
|
||||
writer: w,
|
||||
buffer: bytes.NewBuffer(nil),
|
||||
},
|
||||
}
|
||||
r.Writer = &r.ResponseWriter
|
||||
r.Writer = r.ResponseWriter
|
||||
return r
|
||||
}
|
||||
|
||||
@ -47,7 +47,7 @@ func (r *Response) Write(content ...interface{}) {
|
||||
if len(content) == 0 {
|
||||
return
|
||||
}
|
||||
if r.Status == 0 && r.request.hasServeHandler {
|
||||
if r.Status == 0 && r.Request.hasServeHandler {
|
||||
r.Status = http.StatusOK
|
||||
}
|
||||
for _, v := range content {
|
||||
@ -99,7 +99,7 @@ func (r *Response) WriteJsonP(content interface{}) error {
|
||||
return err
|
||||
} else {
|
||||
//r.Header().Set("Content-Type", "application/json")
|
||||
if callback := r.request.Get("callback"); callback != "" {
|
||||
if callback := r.Request.GetString("callback"); callback != "" {
|
||||
buffer := []byte(callback)
|
||||
buffer = append(buffer, byte('('))
|
||||
buffer = append(buffer, b...)
|
||||
@ -128,9 +128,9 @@ func (r *Response) WriteStatus(status int, content ...interface{}) {
|
||||
if r.buffer.Len() == 0 {
|
||||
// 状态码注册回调函数处理
|
||||
if status != http.StatusOK {
|
||||
if f := r.request.Server.getStatusHandler(status, r.request); f != nil {
|
||||
if f := r.Request.Server.getStatusHandler(status, r.Request); f != nil {
|
||||
niceCallFunc(func() {
|
||||
f(r.request)
|
||||
f(r.Request)
|
||||
})
|
||||
// 防止多次设置(http: multiple response.WriteHeader calls)
|
||||
if r.Status == 0 {
|
||||
@ -168,7 +168,7 @@ func (r *Response) ServeFile(path string, allowIndex ...bool) {
|
||||
}
|
||||
serveFile = &staticServeFile{path: path}
|
||||
}
|
||||
r.Server.serveFile(r.request, serveFile, allowIndex...)
|
||||
r.Server.serveFile(r.Request, serveFile, allowIndex...)
|
||||
}
|
||||
|
||||
// 静态文件下载处理
|
||||
@ -200,7 +200,7 @@ func (r *Response) ServeFileDownload(path string, name ...string) {
|
||||
r.Header().Set("Content-Type", "application/force-download")
|
||||
r.Header().Set("Accept-Ranges", "bytes")
|
||||
r.Header().Set("Content-Disposition", fmt.Sprintf(`attachment;filename="%s"`, downloadName))
|
||||
r.Server.serveFile(r.request, serveFile)
|
||||
r.Server.serveFile(r.Request, serveFile)
|
||||
}
|
||||
|
||||
// 返回location标识,引导客户端跳转。
|
||||
@ -208,12 +208,12 @@ func (r *Response) ServeFileDownload(path string, name ...string) {
|
||||
func (r *Response) RedirectTo(location string) {
|
||||
r.Header().Set("Location", location)
|
||||
r.WriteHeader(http.StatusFound)
|
||||
r.request.Exit()
|
||||
r.Request.Exit()
|
||||
}
|
||||
|
||||
// 返回location标识,引导客户端跳转到来源页面
|
||||
func (r *Response) RedirectBack() {
|
||||
r.RedirectTo(r.request.GetReferer())
|
||||
r.RedirectTo(r.Request.GetReferer())
|
||||
}
|
||||
|
||||
// 获取当前缓冲区中的数据
|
||||
@ -221,6 +221,11 @@ func (r *Response) Buffer() []byte {
|
||||
return r.buffer.Bytes()
|
||||
}
|
||||
|
||||
// 获取当前缓冲区中的数据(string)
|
||||
func (r *Response) BufferString() string {
|
||||
return r.buffer.String()
|
||||
}
|
||||
|
||||
// 获取当前缓冲区中的数据大小
|
||||
func (r *Response) BufferLength() int {
|
||||
return r.buffer.Len()
|
||||
|
||||
@ -8,22 +8,25 @@
|
||||
package ghttp
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
)
|
||||
|
||||
// See https://www.w3.org/TR/cors/ .
|
||||
// 服务端允许跨域请求选项
|
||||
type CORSOptions struct {
|
||||
AllowOrigin string // Access-Control-Allow-Origin
|
||||
AllowCredentials string // Access-Control-Allow-Credentials
|
||||
ExposeHeaders string // Access-Control-Expose-Headers
|
||||
MaxAge int // Access-Control-Max-Age
|
||||
AllowMethods string // Access-Control-Allow-Methods
|
||||
AllowHeaders string // Access-Control-Allow-Headers
|
||||
AllowDomain []string // Used for allowing requests from custom domains
|
||||
AllowOrigin string // Access-Control-Allow-Origin
|
||||
AllowCredentials string // Access-Control-Allow-Credentials
|
||||
ExposeHeaders string // Access-Control-Expose-Headers
|
||||
MaxAge int // Access-Control-Max-Age
|
||||
AllowMethods string // Access-Control-Allow-Methods
|
||||
AllowHeaders string // Access-Control-Allow-Headers
|
||||
}
|
||||
|
||||
// 默认的CORS配置
|
||||
// DefaultCORSOptions returns the default CORS options,
|
||||
// which allows any cross-domain request.
|
||||
func (r *Response) DefaultCORSOptions() CORSOptions {
|
||||
options := CORSOptions{
|
||||
AllowOrigin: "*",
|
||||
@ -32,7 +35,9 @@ func (r *Response) DefaultCORSOptions() CORSOptions {
|
||||
AllowHeaders: "Origin, X-Requested-With, Content-Type, Accept, Key",
|
||||
MaxAge: 3628800,
|
||||
}
|
||||
if referer := r.request.Referer(); referer != "" {
|
||||
if origin := r.Header().Get("Origin"); origin != "" {
|
||||
options.AllowOrigin = origin
|
||||
} else if referer := r.Request.Referer(); referer != "" {
|
||||
if p := gstr.PosR(referer, "/", 6); p != -1 {
|
||||
options.AllowOrigin = referer[:p]
|
||||
} else {
|
||||
@ -42,10 +47,10 @@ func (r *Response) DefaultCORSOptions() CORSOptions {
|
||||
return options
|
||||
}
|
||||
|
||||
// CORS sets custom CORS options.
|
||||
// See https://www.w3.org/TR/cors/ .
|
||||
// 允许请求跨域访问.
|
||||
func (r *Response) CORS(options CORSOptions) {
|
||||
if options.AllowOrigin != "" {
|
||||
if r.CORSAllowedOrigin(options) {
|
||||
r.Header().Set("Access-Control-Allow-Origin", options.AllowOrigin)
|
||||
}
|
||||
if options.AllowCredentials != "" {
|
||||
@ -65,7 +70,29 @@ func (r *Response) CORS(options CORSOptions) {
|
||||
}
|
||||
}
|
||||
|
||||
// 允许请求跨域访问(使用默认配置).
|
||||
// CORSAllowed checks whether the current request origin is allowed CORS.
|
||||
func (r *Response) CORSAllowedOrigin(options CORSOptions) bool {
|
||||
if options.AllowDomain == nil {
|
||||
return true
|
||||
}
|
||||
origin := r.Request.Header.Get("Origin")
|
||||
if origin == "" {
|
||||
return true
|
||||
}
|
||||
parsed, err := url.Parse(origin)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
for _, v := range options.AllowDomain {
|
||||
if gstr.IsSubDomain(parsed.Host, v) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// CORSDefault sets CORS with default CORS options,
|
||||
// which allows any cross-domain request.
|
||||
func (r *Response) CORSDefault() {
|
||||
r.CORS(r.DefaultCORSOptions())
|
||||
}
|
||||
|
||||
@ -67,9 +67,9 @@ func (r *Response) buildInVars(params ...map[string]interface{}) map[string]inte
|
||||
if c := gins.Config(); c.FilePath() != "" {
|
||||
vars["Config"] = c.GetMap(".")
|
||||
}
|
||||
vars["Cookie"] = r.request.Cookie.Map()
|
||||
vars["Session"] = r.request.Session.Map()
|
||||
vars["Get"] = r.request.GetQueryMap()
|
||||
vars["Post"] = r.request.GetPostMap()
|
||||
vars["Get"] = r.Request.GetQueryMap()
|
||||
vars["Post"] = r.Request.GetPostMap()
|
||||
vars["Cookie"] = r.Request.Cookie.Map()
|
||||
vars["Session"] = r.Request.Session.Map()
|
||||
return vars
|
||||
}
|
||||
|
||||
@ -8,35 +8,47 @@
|
||||
package ghttp
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"net"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// 自定义的ResponseWriter,用于写入流的控制
|
||||
// Custom ResponseWriter, which is used for controlling the output buffer.
|
||||
type ResponseWriter struct {
|
||||
http.ResponseWriter
|
||||
Status int // http status
|
||||
buffer *bytes.Buffer // 缓冲区内容
|
||||
Status int // HTTP status.
|
||||
writer http.ResponseWriter // The underlying ResponseWriter.
|
||||
buffer *bytes.Buffer // The output buffer.
|
||||
}
|
||||
|
||||
// 覆盖父级的WriteHeader方法
|
||||
// Header implements the interface function of http.ResponseWriter.Header.
|
||||
func (w *ResponseWriter) Header() http.Header {
|
||||
return w.writer.Header()
|
||||
}
|
||||
|
||||
// Write implements the interface function of http.ResponseWriter.Write.
|
||||
func (w *ResponseWriter) Write(data []byte) (int, error) {
|
||||
w.buffer.Write(data)
|
||||
return len(data), nil
|
||||
}
|
||||
|
||||
// 覆盖父级的WriteHeader方法, 这里只会记录Status做缓冲处理, 并不会立即输出到HEADER。
|
||||
// WriteHeader implements the interface of http.ResponseWriter.WriteHeader.
|
||||
func (w *ResponseWriter) WriteHeader(status int) {
|
||||
w.Status = status
|
||||
}
|
||||
|
||||
// 输出buffer数据到客户端.
|
||||
// Hijack implements the interface function of http.Hijacker.Hijack.
|
||||
func (w *ResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
||||
return w.writer.(http.Hijacker).Hijack()
|
||||
}
|
||||
|
||||
// OutputBuffer outputs the buffer to client.
|
||||
func (w *ResponseWriter) OutputBuffer() {
|
||||
if w.Status != 0 {
|
||||
w.ResponseWriter.WriteHeader(w.Status)
|
||||
w.writer.WriteHeader(w.Status)
|
||||
}
|
||||
if w.buffer.Len() > 0 {
|
||||
w.ResponseWriter.Write(w.buffer.Bytes())
|
||||
w.writer.Write(w.buffer.Bytes())
|
||||
w.buffer.Reset()
|
||||
}
|
||||
}
|
||||
|
||||
@ -75,8 +75,8 @@ type (
|
||||
|
||||
// 根据特定URL.Path解析后的路由检索结果项
|
||||
handlerParsedItem struct {
|
||||
handler *handlerItem // 路由注册项
|
||||
values map[string][]string // 特定URL.Path的Router解析参数
|
||||
handler *handlerItem // 路由注册项
|
||||
values map[string]string // 特定URL.Path的Router解析参数
|
||||
}
|
||||
|
||||
// 控制器服务函数反射信息
|
||||
|
||||
@ -26,10 +26,10 @@ import (
|
||||
const (
|
||||
gDEFAULT_HTTP_ADDR = ":80" // 默认HTTP监听地址
|
||||
gDEFAULT_HTTPS_ADDR = ":443" // 默认HTTPS监听地址
|
||||
NAME_TO_URI_TYPE_DEFAULT = 0 // 服务注册时对象和方法名称转换为URI时,全部转为小写,单词以'-'连接符号连接
|
||||
NAME_TO_URI_TYPE_FULLNAME = 1 // 不处理名称,以原有名称构建成URI
|
||||
NAME_TO_URI_TYPE_ALLLOWER = 2 // 仅转为小写,单词间不使用连接符号
|
||||
NAME_TO_URI_TYPE_CAMEL = 3 // 采用驼峰命名方式
|
||||
URI_TYPE_DEFAULT = 0 // 服务注册时对象和方法名称转换为URI时,全部转为小写,单词以'-'连接符号连接
|
||||
URI_TYPE_FULLNAME = 1 // 不处理名称,以原有名称构建成URI
|
||||
URI_TYPE_ALLLOWER = 2 // 仅转为小写,单词间不使用连接符号
|
||||
URI_TYPE_CAMEL = 3 // 采用驼峰命名方式
|
||||
gCHANGE_CONFIG_WHILE_RUNNING_ERROR = "server's configuration cannot be changed while running"
|
||||
)
|
||||
|
||||
@ -73,6 +73,7 @@ type ServerConfig struct {
|
||||
ErrorStack bool // Logging: 当产生错误时打印调用链详细堆栈
|
||||
ErrorLogEnabled bool // Logging: 是否开启error log(默认开启)
|
||||
AccessLogEnabled bool // Logging: 是否开启access log(默认关闭)
|
||||
FormParsingMemory int64 // Mess: 表单解析内存限制(byte)
|
||||
NameToUriType int // Mess: 服务注册时对象和方法名称转换为URI时的规则
|
||||
GzipContentTypes []string // Mess: 允许进行gzip压缩的文件类型
|
||||
DumpRouteMap bool // Mess: 是否在程序启动时默认打印路由表信息
|
||||
@ -105,6 +106,7 @@ var defaultServerConfig = ServerConfig{
|
||||
ErrorLogEnabled: true,
|
||||
AccessLogEnabled: false,
|
||||
DumpRouteMap: true,
|
||||
FormParsingMemory: 1024 * 1024 * 1024,
|
||||
RouterCacheExpire: 60,
|
||||
Rewrites: make(map[string]string),
|
||||
}
|
||||
@ -149,12 +151,12 @@ func (s *Server) SetConfigWithMap(m map[string]interface{}) {
|
||||
}
|
||||
|
||||
// 设置http server参数 - Addr
|
||||
func (s *Server) SetAddr(itemFunc string) {
|
||||
func (s *Server) SetAddr(address string) {
|
||||
if s.Status() == SERVER_STATUS_RUNNING {
|
||||
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
|
||||
return
|
||||
}
|
||||
s.config.Addr = itemFunc
|
||||
s.config.Addr = address
|
||||
}
|
||||
|
||||
// 设置http server参数 - Port
|
||||
@ -174,12 +176,12 @@ func (s *Server) SetPort(port ...int) {
|
||||
}
|
||||
|
||||
// 设置http server参数 - HTTPS Addr
|
||||
func (s *Server) SetHTTPSAddr(itemFunc string) {
|
||||
func (s *Server) SetHTTPSAddr(address string) {
|
||||
if s.Status() == SERVER_STATUS_RUNNING {
|
||||
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
|
||||
return
|
||||
}
|
||||
s.config.HTTPSAddr = itemFunc
|
||||
s.config.HTTPSAddr = address
|
||||
}
|
||||
|
||||
// 设置http server参数 - HTTPS Port
|
||||
@ -287,41 +289,6 @@ func (s *Server) SetServerAgent(agent string) {
|
||||
s.config.ServerAgent = agent
|
||||
}
|
||||
|
||||
func (s *Server) SetGzipContentTypes(types []string) {
|
||||
if s.Status() == SERVER_STATUS_RUNNING {
|
||||
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
|
||||
return
|
||||
}
|
||||
s.config.GzipContentTypes = types
|
||||
}
|
||||
|
||||
// 服务注册时对象和方法名称转换为URI时的规则
|
||||
func (s *Server) SetNameToUriType(t int) {
|
||||
if s.Status() == SERVER_STATUS_RUNNING {
|
||||
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
|
||||
return
|
||||
}
|
||||
s.config.NameToUriType = t
|
||||
}
|
||||
|
||||
// 是否在程序启动时打印路由表信息
|
||||
func (s *Server) SetDumpRouteMap(enabled bool) {
|
||||
if s.Status() == SERVER_STATUS_RUNNING {
|
||||
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
|
||||
return
|
||||
}
|
||||
s.config.DumpRouteMap = enabled
|
||||
}
|
||||
|
||||
// 设置路由缓存过期时间(秒)
|
||||
func (s *Server) SetRouterCacheExpire(expire int) {
|
||||
if s.Status() == SERVER_STATUS_RUNNING {
|
||||
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
|
||||
return
|
||||
}
|
||||
s.config.RouterCacheExpire = expire
|
||||
}
|
||||
|
||||
// 设置KeepAlive
|
||||
func (s *Server) SetKeepAlive(enabled bool) {
|
||||
if s.Status() == SERVER_STATUS_RUNNING {
|
||||
|
||||
52
net/ghttp/ghttp_server_config_mess.go
Normal file
52
net/ghttp/ghttp_server_config_mess.go
Normal file
@ -0,0 +1,52 @@
|
||||
// Copyright 2019 gf Author(https://github.com/gogf/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://github.com/gogf/gf.
|
||||
|
||||
package ghttp
|
||||
|
||||
import "github.com/gogf/gf/os/glog"
|
||||
|
||||
func (s *Server) SetGzipContentTypes(types []string) {
|
||||
if s.Status() == SERVER_STATUS_RUNNING {
|
||||
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
|
||||
return
|
||||
}
|
||||
s.config.GzipContentTypes = types
|
||||
}
|
||||
|
||||
// 服务注册时对象和方法名称转换为URI时的规则
|
||||
func (s *Server) SetNameToUriType(t int) {
|
||||
if s.Status() == SERVER_STATUS_RUNNING {
|
||||
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
|
||||
return
|
||||
}
|
||||
s.config.NameToUriType = t
|
||||
}
|
||||
|
||||
// 是否在程序启动时打印路由表信息
|
||||
func (s *Server) SetDumpRouteMap(enabled bool) {
|
||||
if s.Status() == SERVER_STATUS_RUNNING {
|
||||
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
|
||||
return
|
||||
}
|
||||
s.config.DumpRouteMap = enabled
|
||||
}
|
||||
|
||||
// 设置路由缓存过期时间(秒)
|
||||
func (s *Server) SetRouterCacheExpire(expire int) {
|
||||
if s.Status() == SERVER_STATUS_RUNNING {
|
||||
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
|
||||
return
|
||||
}
|
||||
s.config.RouterCacheExpire = expire
|
||||
}
|
||||
|
||||
func (s *Server) SetFormParsingMemory(maxMemory int64) {
|
||||
if s.Status() == SERVER_STATUS_RUNNING {
|
||||
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
|
||||
return
|
||||
}
|
||||
s.config.FormParsingMemory = maxMemory
|
||||
}
|
||||
@ -12,6 +12,8 @@ import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
|
||||
"github.com/gogf/gf/errors/gerror"
|
||||
|
||||
"github.com/gogf/gf/os/gres"
|
||||
@ -276,7 +278,11 @@ func (s *Server) listDir(r *Request, f http.File) {
|
||||
r.Response.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
}
|
||||
r.Response.Write(`<html>`)
|
||||
r.Response.Write(`<head></head>`)
|
||||
r.Response.Write(`<head>`)
|
||||
r.Response.Write(`<style>`)
|
||||
r.Response.Write(`body {font-family:Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace;}`)
|
||||
r.Response.Write(`</style>`)
|
||||
r.Response.Write(`</head>`)
|
||||
r.Response.Write(`<body>`)
|
||||
r.Response.Writef(`<h1>Index of %s</h1>`, r.URL.Path)
|
||||
r.Response.Writef(`<hr />`)
|
||||
@ -288,6 +294,7 @@ func (s *Server) listDir(r *Request, f http.File) {
|
||||
}
|
||||
name := ""
|
||||
size := ""
|
||||
prefix := gstr.TrimRight(r.URL.Path, "/")
|
||||
for _, file := range files {
|
||||
name = file.Name()
|
||||
size = gfile.FormatSize(file.Size())
|
||||
@ -296,9 +303,9 @@ func (s *Server) listDir(r *Request, f http.File) {
|
||||
size = "-"
|
||||
}
|
||||
r.Response.Write(`<tr>`)
|
||||
r.Response.Writef(`<td><a href="%s/%s">%s</a></td>`, r.URL.Path, name, ghtml.SpecialChars(name))
|
||||
r.Response.Writef(`<td><a href="%s/%s">%s</a></td>`, prefix, name, ghtml.SpecialChars(name))
|
||||
r.Response.Writef(`<td style="width:300px;text-align:center;">%s</td>`, gtime.New(file.ModTime()).ISO8601())
|
||||
r.Response.Writef(`<td style="width:80px;text-align:center;">%s</td>`, size)
|
||||
r.Response.Writef(`<td style="width:80px;text-align:right;">%s</td>`, size)
|
||||
r.Response.Write(`</tr>`)
|
||||
}
|
||||
r.Response.Write(`</table>`)
|
||||
|
||||
@ -15,7 +15,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
gPATH_FILTER_KEY = "/gf/net/ghttp/ghttp"
|
||||
gPATH_FILTER_KEY = "/net/ghttp/ghttp"
|
||||
)
|
||||
|
||||
// 处理服务错误信息,主要是panic,http请求的status由access log进行管理
|
||||
|
||||
@ -20,7 +20,7 @@ type utilPprof struct{}
|
||||
|
||||
func (p *utilPprof) Index(r *Request) {
|
||||
profiles := runpprof.Profiles()
|
||||
action := r.Get("action")
|
||||
action := r.GetString("action")
|
||||
data := map[string]interface{}{
|
||||
"uri": strings.TrimRight(r.URL.Path, "/") + "/",
|
||||
"profiles": profiles,
|
||||
|
||||
@ -9,9 +9,10 @@ package ghttp
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/debug/gdebug"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/debug/gdebug"
|
||||
|
||||
"github.com/gogf/gf/container/glist"
|
||||
"github.com/gogf/gf/os/glog"
|
||||
"github.com/gogf/gf/text/gregex"
|
||||
@ -19,7 +20,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
gFILTER_KEY = "/gf/net/ghttp/ghttp"
|
||||
gFILTER_KEY = "/net/ghttp/ghttp"
|
||||
)
|
||||
|
||||
// 解析pattern
|
||||
|
||||
@ -216,6 +216,14 @@ func (g *RouterGroup) Middleware(handlers ...HandlerFunc) *RouterGroup {
|
||||
return group
|
||||
}
|
||||
|
||||
func (g *RouterGroup) MiddlewarePattern(pattern string, handlers ...HandlerFunc) *RouterGroup {
|
||||
group := g.Clone()
|
||||
for _, handler := range handlers {
|
||||
group.preBind("MIDDLEWARE", pattern, handler)
|
||||
}
|
||||
return group
|
||||
}
|
||||
|
||||
func (g *RouterGroup) preBind(bindType string, pattern string, object interface{}, params ...interface{}) *RouterGroup {
|
||||
preBindItems = append(preBindItems, groupPreBindItem{
|
||||
group: g,
|
||||
|
||||
@ -7,6 +7,7 @@
|
||||
package ghttp
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"reflect"
|
||||
"runtime"
|
||||
)
|
||||
@ -34,20 +35,20 @@ func (s *Server) callHookHandler(hook string, r *Request) {
|
||||
hookItems := r.getHookHandlers(hook)
|
||||
if len(hookItems) > 0 {
|
||||
// 备份原有的router变量
|
||||
oldRouterVars := r.routerVars
|
||||
oldRouterVars := r.routerMap
|
||||
for _, item := range hookItems {
|
||||
// hook方法不能更改serve方法的路由参数,其匹配的路由参数只能自己使用,
|
||||
// 且在多个hook方法之间不能共享路由参数,单可以使用匹配的serve方法路由参数。
|
||||
// 当前回调函数的路由参数只在当前回调函数下有效。
|
||||
r.routerVars = make(map[string][]string)
|
||||
r.routerMap = make(map[string]interface{})
|
||||
if len(oldRouterVars) > 0 {
|
||||
for k, v := range oldRouterVars {
|
||||
r.routerVars[k] = v
|
||||
r.routerMap[k] = v
|
||||
}
|
||||
}
|
||||
if len(item.values) > 0 {
|
||||
for k, v := range item.values {
|
||||
r.routerVars[k] = v
|
||||
r.routerMap[k] = v
|
||||
}
|
||||
}
|
||||
// 不使用hook的router对象,保留路由注册服务的router对象,不能覆盖
|
||||
@ -61,12 +62,13 @@ func (s *Server) callHookHandler(hook string, r *Request) {
|
||||
case gEXCEPTION_EXIT_HOOK:
|
||||
return
|
||||
default:
|
||||
r.Response.WriteStatus(http.StatusInternalServerError, err)
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
// 恢复原有的router变量
|
||||
r.routerVars = oldRouterVars
|
||||
r.routerMap = oldRouterVars
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -27,7 +27,7 @@ func (s *Server) BindMiddleware(pattern string, handlers ...HandlerFunc) {
|
||||
}
|
||||
|
||||
// 注册中间件,绑定到全局路由规则("/*")上,中间件参数支持多个。
|
||||
func (s *Server) AddMiddleware(handlers ...HandlerFunc) {
|
||||
func (s *Server) BindMiddlewareDefault(handlers ...HandlerFunc) {
|
||||
for _, handler := range handlers {
|
||||
s.setHandler(gDEFAULT_MIDDLEWARE_PATTERN, &handlerItem{
|
||||
itemType: gHANDLER_TYPE_MIDDLEWARE,
|
||||
|
||||
@ -113,14 +113,10 @@ func (s *Server) searchHandlers(method, path, domain string) (parsedItems []*han
|
||||
// 如果需要query匹配,那么需要重新正则解析URL
|
||||
if len(item.router.RegNames) > 0 {
|
||||
if len(match) > len(item.router.RegNames) {
|
||||
parsedItem.values = make(map[string][]string)
|
||||
// 如果存在存在同名路由参数名称,那么执行数组追加
|
||||
parsedItem.values = make(map[string]string)
|
||||
// 如果存在存在同名路由参数名称,那么执行覆盖
|
||||
for i, name := range item.router.RegNames {
|
||||
if _, ok := parsedItem.values[name]; ok {
|
||||
parsedItem.values[name] = append(parsedItem.values[name], match[i+1])
|
||||
} else {
|
||||
parsedItem.values[name] = []string{match[i+1]}
|
||||
}
|
||||
parsedItem.values[name] = match[i+1]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -77,13 +77,13 @@ func (s *Server) mergeBuildInNameToPattern(pattern string, structName, methodNam
|
||||
// 规则3: 采用驼峰命名方式
|
||||
func (s *Server) nameToUrlPart(name string) string {
|
||||
switch s.config.NameToUriType {
|
||||
case NAME_TO_URI_TYPE_FULLNAME:
|
||||
case URI_TYPE_FULLNAME:
|
||||
return name
|
||||
|
||||
case NAME_TO_URI_TYPE_ALLLOWER:
|
||||
case URI_TYPE_ALLLOWER:
|
||||
return strings.ToLower(name)
|
||||
|
||||
case NAME_TO_URI_TYPE_CAMEL:
|
||||
case URI_TYPE_CAMEL:
|
||||
part := bytes.NewBuffer(nil)
|
||||
if gstr.IsLetterUpper(name[0]) {
|
||||
part.WriteByte(name[0] + 32)
|
||||
@ -93,7 +93,7 @@ func (s *Server) nameToUrlPart(name string) string {
|
||||
part.WriteString(name[1:])
|
||||
return part.String()
|
||||
|
||||
case NAME_TO_URI_TYPE_DEFAULT:
|
||||
case URI_TYPE_DEFAULT:
|
||||
fallthrough
|
||||
default:
|
||||
part := bytes.NewBuffer(nil)
|
||||
|
||||
@ -20,14 +20,13 @@ func Test_Cookie(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/set", func(r *ghttp.Request) {
|
||||
r.Cookie.Set(r.Get("k"), r.Get("v"))
|
||||
r.Cookie.Set(r.GetString("k"), r.GetString("v"))
|
||||
})
|
||||
s.BindHandler("/get", func(r *ghttp.Request) {
|
||||
//fmt.Println(r.Cookie.Map())
|
||||
r.Response.Write(r.Cookie.Get(r.Get("k")))
|
||||
r.Response.Write(r.Cookie.Get(r.GetString("k")))
|
||||
})
|
||||
s.BindHandler("/remove", func(r *ghttp.Request) {
|
||||
r.Cookie.Remove(r.Get("k"))
|
||||
r.Cookie.Remove(r.GetString("k"))
|
||||
})
|
||||
s.SetPort(p)
|
||||
s.SetDumpRouteMap(false)
|
||||
|
||||
@ -130,18 +130,18 @@ func Test_BindMiddleware_Status(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AddMiddleware_Basic1(t *testing.T) {
|
||||
func Test_BindMiddlewareDefault_Basic1(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
||||
r.Response.Write("test")
|
||||
})
|
||||
s.AddMiddleware(func(r *ghttp.Request) {
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
r.Response.Write("1")
|
||||
r.Middleware.Next()
|
||||
r.Response.Write("2")
|
||||
})
|
||||
s.AddMiddleware(func(r *ghttp.Request) {
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
r.Response.Write("3")
|
||||
r.Middleware.Next()
|
||||
r.Response.Write("4")
|
||||
@ -162,18 +162,18 @@ func Test_AddMiddleware_Basic1(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AddMiddleware_Basic2(t *testing.T) {
|
||||
func Test_BindMiddlewareDefault_Basic2(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("PUT:/test/test", func(r *ghttp.Request) {
|
||||
r.Response.Write("test")
|
||||
})
|
||||
s.AddMiddleware(func(r *ghttp.Request) {
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
r.Response.Write("1")
|
||||
r.Middleware.Next()
|
||||
r.Response.Write("2")
|
||||
})
|
||||
s.AddMiddleware(func(r *ghttp.Request) {
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
r.Response.Write("3")
|
||||
r.Middleware.Next()
|
||||
r.Response.Write("4")
|
||||
@ -196,17 +196,17 @@ func Test_AddMiddleware_Basic2(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AddMiddleware_Basic3(t *testing.T) {
|
||||
func Test_BindMiddlewareDefault_Basic3(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
||||
r.Response.Write("test")
|
||||
})
|
||||
s.AddMiddleware(func(r *ghttp.Request) {
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
r.Response.Write("1")
|
||||
r.Middleware.Next()
|
||||
})
|
||||
s.AddMiddleware(func(r *ghttp.Request) {
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
r.Middleware.Next()
|
||||
r.Response.Write("2")
|
||||
})
|
||||
@ -226,17 +226,17 @@ func Test_AddMiddleware_Basic3(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AddMiddleware_Basic4(t *testing.T) {
|
||||
func Test_BindMiddlewareDefault_Basic4(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
||||
r.Response.Write("test")
|
||||
})
|
||||
s.AddMiddleware(func(r *ghttp.Request) {
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
r.Middleware.Next()
|
||||
r.Response.Write("1")
|
||||
})
|
||||
s.AddMiddleware(func(r *ghttp.Request) {
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
r.Response.Write("2")
|
||||
r.Middleware.Next()
|
||||
})
|
||||
@ -256,17 +256,17 @@ func Test_AddMiddleware_Basic4(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AddMiddleware_Basic5(t *testing.T) {
|
||||
func Test_BindMiddlewareDefault_Basic5(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
||||
r.Response.Write("test")
|
||||
})
|
||||
s.AddMiddleware(func(r *ghttp.Request) {
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
r.Response.Write("1")
|
||||
r.Middleware.Next()
|
||||
})
|
||||
s.AddMiddleware(func(r *ghttp.Request) {
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
r.Response.Write("2")
|
||||
r.Middleware.Next()
|
||||
})
|
||||
@ -286,13 +286,13 @@ func Test_AddMiddleware_Basic5(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AddMiddleware_Status(t *testing.T) {
|
||||
func Test_BindMiddlewareDefault_Status(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
||||
r.Response.Write("test")
|
||||
})
|
||||
s.AddMiddleware(func(r *ghttp.Request) {
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
r.Middleware.Next()
|
||||
})
|
||||
s.SetPort(p)
|
||||
@ -333,16 +333,16 @@ func (o *ObjectMiddleware) Info(r *ghttp.Request) {
|
||||
r.Response.Write("Object Info")
|
||||
}
|
||||
|
||||
func Test_AddMiddleware_Basic6(t *testing.T) {
|
||||
func Test_BindMiddlewareDefault_Basic6(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindObject("/", new(ObjectMiddleware))
|
||||
s.AddMiddleware(func(r *ghttp.Request) {
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
r.Response.Write("1")
|
||||
r.Middleware.Next()
|
||||
r.Response.Write("2")
|
||||
})
|
||||
s.AddMiddleware(func(r *ghttp.Request) {
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
r.Response.Write("3")
|
||||
r.Middleware.Next()
|
||||
r.Response.Write("4")
|
||||
@ -385,12 +385,12 @@ func Test_Hook_Middleware_Basic1(t *testing.T) {
|
||||
s.BindHookHandler("/*", ghttp.HOOK_AFTER_SERVE, func(r *ghttp.Request) {
|
||||
r.Response.Write("d")
|
||||
})
|
||||
s.AddMiddleware(func(r *ghttp.Request) {
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
r.Response.Write("1")
|
||||
r.Middleware.Next()
|
||||
r.Response.Write("2")
|
||||
})
|
||||
s.AddMiddleware(func(r *ghttp.Request) {
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
r.Response.Write("3")
|
||||
r.Middleware.Next()
|
||||
r.Response.Write("4")
|
||||
|
||||
@ -26,6 +26,9 @@ func Test_Params_Basic(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/get", func(r *ghttp.Request) {
|
||||
if r.GetQuery("array") != nil {
|
||||
r.Response.Write(r.GetQuery("array"))
|
||||
}
|
||||
if r.GetQuery("slice") != nil {
|
||||
r.Response.Write(r.GetQuery("slice"))
|
||||
}
|
||||
@ -47,8 +50,49 @@ func Test_Params_Basic(t *testing.T) {
|
||||
if r.GetQuery("string") != nil {
|
||||
r.Response.Write(r.GetQueryString("string"))
|
||||
}
|
||||
if r.GetQuery("map") != nil {
|
||||
r.Response.Write(r.GetQueryMap()["map"].(map[string]interface{})["b"])
|
||||
}
|
||||
if r.GetQuery("a") != nil {
|
||||
r.Response.Write(r.GetQueryMapStrStr()["a"])
|
||||
}
|
||||
})
|
||||
s.BindHandler("/put", func(r *ghttp.Request) {
|
||||
if r.Get("array") != nil {
|
||||
r.Response.Write(r.Get("array"))
|
||||
}
|
||||
if r.Get("slice") != nil {
|
||||
r.Response.Write(r.Get("slice"))
|
||||
}
|
||||
if r.Get("bool") != nil {
|
||||
r.Response.Write(r.GetBool("bool"))
|
||||
}
|
||||
if r.Get("float32") != nil {
|
||||
r.Response.Write(r.GetFloat32("float32"))
|
||||
}
|
||||
if r.Get("float64") != nil {
|
||||
r.Response.Write(r.GetFloat64("float64"))
|
||||
}
|
||||
if r.Get("int") != nil {
|
||||
r.Response.Write(r.GetInt("int"))
|
||||
}
|
||||
if r.Get("uint") != nil {
|
||||
r.Response.Write(r.GetUint("uint"))
|
||||
}
|
||||
if r.Get("string") != nil {
|
||||
r.Response.Write(r.GetString("string"))
|
||||
}
|
||||
if r.Get("map") != nil {
|
||||
r.Response.Write(r.GetMap()["map"].(map[string]interface{})["b"])
|
||||
}
|
||||
if r.Get("a") != nil {
|
||||
r.Response.Write(r.GetMapStrStr()["a"])
|
||||
}
|
||||
})
|
||||
s.BindHandler("/post", func(r *ghttp.Request) {
|
||||
if r.GetPost("array") != nil {
|
||||
r.Response.Write(r.GetPost("array"))
|
||||
}
|
||||
if r.GetPost("slice") != nil {
|
||||
r.Response.Write(r.GetPost("slice"))
|
||||
}
|
||||
@ -70,6 +114,12 @@ func Test_Params_Basic(t *testing.T) {
|
||||
if r.GetPost("string") != nil {
|
||||
r.Response.Write(r.GetPostString("string"))
|
||||
}
|
||||
if r.GetPost("map") != nil {
|
||||
r.Response.Write(r.GetPostMap()["map"].(map[string]interface{})["b"])
|
||||
}
|
||||
if r.GetPost("a") != nil {
|
||||
r.Response.Write(r.GetPostMapStrStr()["a"])
|
||||
}
|
||||
})
|
||||
s.BindHandler("/map", func(r *ghttp.Request) {
|
||||
if m := r.GetQueryMap(); len(m) > 0 {
|
||||
@ -83,7 +133,12 @@ func Test_Params_Basic(t *testing.T) {
|
||||
r.Response.Write(r.GetRaw())
|
||||
})
|
||||
s.BindHandler("/json", func(r *ghttp.Request) {
|
||||
r.Response.Write(r.GetJson().Get("name"))
|
||||
j, err := r.GetJson()
|
||||
if err != nil {
|
||||
r.Response.Write(err)
|
||||
return
|
||||
}
|
||||
r.Response.Write(j.Get("name"))
|
||||
})
|
||||
s.BindHandler("/struct", func(r *ghttp.Request) {
|
||||
if m := r.GetQueryMap(); len(m) > 0 {
|
||||
@ -132,7 +187,8 @@ func Test_Params_Basic(t *testing.T) {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
// GET
|
||||
gtest.Assert(client.GetContent("/get", "slice=1&slice=2"), `["1","2"]`)
|
||||
gtest.Assert(client.GetContent("/get", "array[]=1&array[]=2"), `["1","2"]`)
|
||||
gtest.Assert(client.GetContent("/get", "slice=1&slice=2"), `2`)
|
||||
gtest.Assert(client.GetContent("/get", "bool=1"), `true`)
|
||||
gtest.Assert(client.GetContent("/get", "bool=0"), `false`)
|
||||
gtest.Assert(client.GetContent("/get", "float32=0.11"), `0.11`)
|
||||
@ -142,9 +198,27 @@ func Test_Params_Basic(t *testing.T) {
|
||||
gtest.Assert(client.GetContent("/get", "uint=10000"), `10000`)
|
||||
gtest.Assert(client.GetContent("/get", "uint=9"), `9`)
|
||||
gtest.Assert(client.GetContent("/get", "string=key"), `key`)
|
||||
gtest.Assert(client.GetContent("/get", "map[a]=1&map[b]=2"), `2`)
|
||||
gtest.Assert(client.GetContent("/get", "a=1&b=2"), `1`)
|
||||
|
||||
// PUT
|
||||
gtest.Assert(client.PutContent("/put", "array[]=1&array[]=2"), `["1","2"]`)
|
||||
gtest.Assert(client.PutContent("/put", "slice=1&slice=2"), `2`)
|
||||
gtest.Assert(client.PutContent("/put", "bool=1"), `true`)
|
||||
gtest.Assert(client.PutContent("/put", "bool=0"), `false`)
|
||||
gtest.Assert(client.PutContent("/put", "float32=0.11"), `0.11`)
|
||||
gtest.Assert(client.PutContent("/put", "float64=0.22"), `0.22`)
|
||||
gtest.Assert(client.PutContent("/put", "int=-10000"), `-10000`)
|
||||
gtest.Assert(client.PutContent("/put", "int=10000"), `10000`)
|
||||
gtest.Assert(client.PutContent("/put", "uint=10000"), `10000`)
|
||||
gtest.Assert(client.PutContent("/put", "uint=9"), `9`)
|
||||
gtest.Assert(client.PutContent("/put", "string=key"), `key`)
|
||||
gtest.Assert(client.PutContent("/put", "map[a]=1&map[b]=2"), `2`)
|
||||
gtest.Assert(client.PutContent("/put", "a=1&b=2"), `1`)
|
||||
|
||||
// POST
|
||||
gtest.Assert(client.PostContent("/post", "slice=1&slice=2"), `["1","2"]`)
|
||||
gtest.Assert(client.PostContent("/post", "array[]=1&array[]=2"), `["1","2"]`)
|
||||
gtest.Assert(client.PostContent("/post", "slice=1&slice=2"), `2`)
|
||||
gtest.Assert(client.PostContent("/post", "bool=1"), `true`)
|
||||
gtest.Assert(client.PostContent("/post", "bool=0"), `false`)
|
||||
gtest.Assert(client.PostContent("/post", "float32=0.11"), `0.11`)
|
||||
@ -154,6 +228,8 @@ func Test_Params_Basic(t *testing.T) {
|
||||
gtest.Assert(client.PostContent("/post", "uint=10000"), `10000`)
|
||||
gtest.Assert(client.PostContent("/post", "uint=9"), `9`)
|
||||
gtest.Assert(client.PostContent("/post", "string=key"), `key`)
|
||||
gtest.Assert(client.PostContent("/post", "map[a]=1&map[b]=2"), `2`)
|
||||
gtest.Assert(client.PostContent("/post", "a=1&b=2"), `1`)
|
||||
|
||||
// Map
|
||||
gtest.Assert(client.GetContent("/map", "id=1&name=john"), `john`)
|
||||
|
||||
@ -25,7 +25,7 @@ func (o *NamesObject) ShowName(r *ghttp.Request) {
|
||||
func Test_NameToUri_FullName(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_FULLNAME)
|
||||
s.SetNameToUriType(ghttp.URI_TYPE_FULLNAME)
|
||||
s.BindObject("/{.struct}/{.method}", new(NamesObject))
|
||||
s.SetPort(p)
|
||||
s.SetDumpRouteMap(false)
|
||||
@ -47,7 +47,7 @@ func Test_NameToUri_FullName(t *testing.T) {
|
||||
func Test_NameToUri_AllLower(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_ALLLOWER)
|
||||
s.SetNameToUriType(ghttp.URI_TYPE_ALLLOWER)
|
||||
s.BindObject("/{.struct}/{.method}", new(NamesObject))
|
||||
s.SetPort(p)
|
||||
s.SetDumpRouteMap(false)
|
||||
@ -69,7 +69,7 @@ func Test_NameToUri_AllLower(t *testing.T) {
|
||||
func Test_NameToUri_Camel(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_CAMEL)
|
||||
s.SetNameToUriType(ghttp.URI_TYPE_CAMEL)
|
||||
s.BindObject("/{.struct}/{.method}", new(NamesObject))
|
||||
s.SetPort(p)
|
||||
s.SetDumpRouteMap(false)
|
||||
@ -91,7 +91,7 @@ func Test_NameToUri_Camel(t *testing.T) {
|
||||
func Test_NameToUri_Default(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_DEFAULT)
|
||||
s.SetNameToUriType(ghttp.URI_TYPE_DEFAULT)
|
||||
s.BindObject("/{.struct}/{.method}", new(NamesObject))
|
||||
s.SetPort(p)
|
||||
s.SetDumpRouteMap(false)
|
||||
|
||||
@ -20,13 +20,13 @@ func Test_Session_Cookie(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/set", func(r *ghttp.Request) {
|
||||
r.Session.Set(r.Get("k"), r.Get("v"))
|
||||
r.Session.Set(r.GetString("k"), r.GetString("v"))
|
||||
})
|
||||
s.BindHandler("/get", func(r *ghttp.Request) {
|
||||
r.Response.Write(r.Session.Get(r.Get("k")))
|
||||
r.Response.Write(r.Session.Get(r.GetString("k")))
|
||||
})
|
||||
s.BindHandler("/remove", func(r *ghttp.Request) {
|
||||
r.Session.Remove(r.Get("k"))
|
||||
r.Session.Remove(r.GetString("k"))
|
||||
})
|
||||
s.BindHandler("/clear", func(r *ghttp.Request) {
|
||||
r.Session.Clear()
|
||||
@ -68,13 +68,13 @@ func Test_Session_Header(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/set", func(r *ghttp.Request) {
|
||||
r.Session.Set(r.Get("k"), r.Get("v"))
|
||||
r.Session.Set(r.GetString("k"), r.GetString("v"))
|
||||
})
|
||||
s.BindHandler("/get", func(r *ghttp.Request) {
|
||||
r.Response.Write(r.Session.Get(r.Get("k")))
|
||||
r.Response.Write(r.Session.Get(r.GetString("k")))
|
||||
})
|
||||
s.BindHandler("/remove", func(r *ghttp.Request) {
|
||||
r.Session.Remove(r.Get("k"))
|
||||
r.Session.Remove(r.GetString("k"))
|
||||
})
|
||||
s.BindHandler("/clear", func(r *ghttp.Request) {
|
||||
r.Session.Clear()
|
||||
@ -121,8 +121,8 @@ func Test_Session_StorageFile(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/set", func(r *ghttp.Request) {
|
||||
r.Session.Set(r.Get("k"), r.Get("v"))
|
||||
r.Response.Write(r.Get("k"), "=", r.Get("v"))
|
||||
r.Session.Set(r.GetString("k"), r.GetString("v"))
|
||||
r.Response.Write(r.GetString("k"), "=", r.GetString("v"))
|
||||
})
|
||||
s.SetPort(p)
|
||||
s.SetDumpRouteMap(false)
|
||||
@ -146,7 +146,7 @@ func Test_Session_StorageFile(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/get", func(r *ghttp.Request) {
|
||||
r.Response.Write(r.Session.Get(r.Get("k")))
|
||||
r.Response.Write(r.Session.Get(r.GetString("k")))
|
||||
})
|
||||
s.SetPort(p)
|
||||
s.SetDumpRouteMap(false)
|
||||
|
||||
@ -12,6 +12,8 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
@ -96,6 +98,7 @@ func Test_Static_IndexFolder(t *testing.T) {
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
gtest.AssertNE(client.GetContent("/"), "Forbidden")
|
||||
gtest.AssertNE(gstr.Pos(client.GetContent("/"), `<a href="/test.html"`), -1)
|
||||
gtest.Assert(client.GetContent("/index.html"), "Not Found")
|
||||
gtest.Assert(client.GetContent("/test.html"), "test")
|
||||
})
|
||||
|
||||
60
net/ghttp/ghttp_unit_websocket_test.go
Normal file
60
net/ghttp/ghttp_unit_websocket_test.go
Normal file
@ -0,0 +1,60 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/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://github.com/gogf/gf.
|
||||
|
||||
package ghttp_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
)
|
||||
|
||||
func Test_WebSocket(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/ws", func(r *ghttp.Request) {
|
||||
ws, err := r.WebSocket()
|
||||
if err != nil {
|
||||
r.Exit()
|
||||
}
|
||||
for {
|
||||
msgType, msg, err := ws.ReadMessage()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if err = ws.WriteMessage(msgType, msg); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
})
|
||||
s.SetPort(p)
|
||||
s.SetDumpRouteMap(false)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
conn, _, err := websocket.DefaultDialer.Dial(fmt.Sprintf("ws://127.0.0.1:%d/ws", p), nil)
|
||||
gtest.Assert(err, nil)
|
||||
defer conn.Close()
|
||||
|
||||
msg := []byte("hello")
|
||||
err = conn.WriteMessage(websocket.TextMessage, msg)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
t, data, err := conn.ReadMessage()
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t, websocket.TextMessage)
|
||||
gtest.Assert(data, msg)
|
||||
})
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user