mirror of
https://gitee.com/johng/gf
synced 2026-06-07 18:26:02 +08:00
Compare commits
224 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8021f39710 | |||
| f2af08270b | |||
| c2f028848c | |||
| d9c7224861 | |||
| f59a1ada88 | |||
| 705ab1d33f | |||
| c07c4d7217 | |||
| b867b2a0bc | |||
| 872d674182 | |||
| 4682abafdf | |||
| b7d194cf52 | |||
| edf2366296 | |||
| 22af5be71f | |||
| f662ff8051 | |||
| 8c51121b3b | |||
| e9a0805801 | |||
| afadbc6621 | |||
| ca546fc30b | |||
| 7c7c168c3d | |||
| 16f0bb96db | |||
| 33a899d32e | |||
| f3a208f02f | |||
| 81fd3d06bb | |||
| 9227139cf8 | |||
| f2190e50b2 | |||
| c4537b4753 | |||
| 167d58490b | |||
| d36aceb9f1 | |||
| eb31922124 | |||
| bec9f5a847 | |||
| 506552c3a9 | |||
| 2bacc77224 | |||
| bc53f265af | |||
| 344f232c36 | |||
| 27b677b0c0 | |||
| a5a0e381bd | |||
| d528d7f5ab | |||
| 821c71bd8d | |||
| 604a10400d | |||
| 9219471f67 | |||
| fe5d2e5685 | |||
| 0a89daa513 | |||
| d6e6ddf996 | |||
| 5dbda8aedc | |||
| 134e4cf28f | |||
| 56a85abef7 | |||
| 80c6ceaf26 | |||
| a10f428715 | |||
| 597f7468e9 | |||
| 5db8851213 | |||
| 1d53d760d8 | |||
| 922e720d63 | |||
| 50018773b7 | |||
| df99036d41 | |||
| ae0fa888f0 | |||
| 18892fb66d | |||
| 5f2be10563 | |||
| a5a88222a6 | |||
| 4facdd5c9e | |||
| 76bc9bd385 | |||
| 364452f3bb | |||
| 4996755f11 | |||
| e33230a88f | |||
| 951ce46932 | |||
| 795c7395e6 | |||
| 27cf47bcd3 | |||
| 58a25c6f61 | |||
| 2d754f80b1 | |||
| f4e8fbe767 | |||
| 458318d374 | |||
| e3f54e1353 | |||
| 4374996073 | |||
| 87295ef1fe | |||
| 81d4082b6a | |||
| d7e19bc3f3 | |||
| 34ef0ea792 | |||
| 2804834540 | |||
| add7dd5a45 | |||
| e40894ca45 | |||
| 28825f5395 | |||
| 6ca5141020 | |||
| fe4f8e1810 | |||
| 671157cb70 | |||
| f775479c3f | |||
| 64bb72842e | |||
| e1d4ba9d23 | |||
| a1edd83add | |||
| f7d6883405 | |||
| 8448a70646 | |||
| 4018bfa899 | |||
| 3cc9ce74e1 | |||
| eac60d845f | |||
| 835c045c92 | |||
| be15f85eae | |||
| 3e27ea0259 | |||
| 6abe660287 | |||
| a06ca31530 | |||
| 890865251b | |||
| 6af90cafee | |||
| 2e2363bb41 | |||
| 108ced2b0b | |||
| fcba650348 | |||
| 1b8a082942 | |||
| 40570cdb03 | |||
| 10451864e6 | |||
| 55e2646367 | |||
| c88839edb1 | |||
| 2a2cfc289c | |||
| 8bbeb186c2 | |||
| be4bf39719 | |||
| 459c69839a | |||
| a5407e57d9 | |||
| 5c749e7762 | |||
| b3fafc64f8 | |||
| c7b0763ab0 | |||
| 0cfdf60de5 | |||
| 096bff791d | |||
| 0cce858641 | |||
| bb45d8d578 | |||
| 7e43aa6b9d | |||
| 4ee7c82bf1 | |||
| 85caa40a3d | |||
| f4654bf446 | |||
| 94ed0bf9c9 | |||
| 22e3705d3e | |||
| 9b1cc6e9c7 | |||
| 9429c8ff83 | |||
| 056c6d4688 | |||
| 382356bc8d | |||
| 1deb3510f0 | |||
| dd7ae1b07a | |||
| 04a8755162 | |||
| 682f99a763 | |||
| 61282d6dab | |||
| ad540f7c25 | |||
| a4f191c1c6 | |||
| 43531c2680 | |||
| 635f5d36fd | |||
| 1becc4932c | |||
| e36dd06f22 | |||
| 8f2c62d444 | |||
| 21efde1a38 | |||
| d8b6466ed0 | |||
| d7b0228e9e | |||
| 9da1277b47 | |||
| a3fd0c9a4a | |||
| ad7375b44b | |||
| 05120b585d | |||
| 9e32d74c8c | |||
| 0e62510c6f | |||
| c492de4fa8 | |||
| 6308380541 | |||
| 2609db1aec | |||
| 6c54e73dbd | |||
| 21abe62633 | |||
| e30b2b0732 | |||
| dcb74ee9df | |||
| 82bf21e831 | |||
| 66355354fc | |||
| 4204125dce | |||
| e6aa9d3a46 | |||
| 9416cd1274 | |||
| 8b5ab846b2 | |||
| a15b93be90 | |||
| 38ee5f7d53 | |||
| 6f9bbbf416 | |||
| 143bc3d8e3 | |||
| ea75b1a936 | |||
| 0dde8c735e | |||
| 1ab7f00b91 | |||
| 3ef42bfbf0 | |||
| 05fec23457 | |||
| 7225e49aa0 | |||
| 14e9deb254 | |||
| f9569b387f | |||
| a0722ed51f | |||
| e9ace9b17a | |||
| 4791d10761 | |||
| c686b1c080 | |||
| 056d6ebbd9 | |||
| baa2cb68de | |||
| e4909b318b | |||
| f1b7cb37c6 | |||
| 734728fa9c | |||
| b373ace065 | |||
| bc7b5c8626 | |||
| f2b45622d6 | |||
| 77cb219057 | |||
| cf34d7bd56 | |||
| 5adc9be0d9 | |||
| 58b2efc900 | |||
| 28326606f5 | |||
| 91c98bbb60 | |||
| 63cd1128a7 | |||
| c0236d7dfa | |||
| d4051df5b6 | |||
| 88045417ff | |||
| 500efb5601 | |||
| 97fe8235da | |||
| e1164e935b | |||
| b26330aee1 | |||
| 2b083709b5 | |||
| 0ac45dc379 | |||
| 650916c22a | |||
| 2804183325 | |||
| 2dc2610621 | |||
| 1736e71e6b | |||
| 69ee5375b9 | |||
| 3ac0a66887 | |||
| 20e873a1fc | |||
| 142484d89c | |||
| 76a9f4ca14 | |||
| c4e5679d5c | |||
| b08d7c3c38 | |||
| 5092d8e6c5 | |||
| 74d625ff97 | |||
| f1119e28e8 | |||
| 3082c7f761 | |||
| 5f36614dd7 | |||
| 1dcc7a4887 | |||
| 41e9d35487 | |||
| cce8ac5118 | |||
| 419b58452f | |||
| b1835ea4e8 |
@ -10,10 +10,10 @@ func main() {
|
||||
// Push
|
||||
l.PushBack(1)
|
||||
l.PushBack(2)
|
||||
e0 := l.PushFront(0)
|
||||
e := l.PushFront(0)
|
||||
// Insert
|
||||
l.InsertBefore(e0, -1)
|
||||
l.InsertAfter(e0, "a")
|
||||
l.InsertBefore(e, -1)
|
||||
l.InsertAfter(e, "a")
|
||||
fmt.Println(l)
|
||||
// Pop
|
||||
fmt.Println(l.PopFront())
|
||||
|
||||
@ -9,7 +9,7 @@ import (
|
||||
func main() {
|
||||
// 创建一个默认的gmap对象,
|
||||
// 默认情况下该gmap对象不支持并发安全特性,
|
||||
// 初始化时可以给定true参数关闭并发安全特性,当做一个普通的map使用。
|
||||
// 初始化时可以给定true参数开启并发安全特性,用以并发安全场景。
|
||||
m := gmap.New()
|
||||
|
||||
// 设置键值对
|
||||
|
||||
@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
m1 := gmap.New()
|
||||
m1 := gmap.New(true)
|
||||
m1.Set("1", "1")
|
||||
|
||||
m2 := m1.Map()
|
||||
|
||||
@ -1,5 +1,16 @@
|
||||
|
||||
# MySQL数据库配置
|
||||
[database]
|
||||
link = "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
|
||||
# debug = true
|
||||
link = "mysql:root:12345678@tcp(127.0.0.1:3306)/test?parseTime=true&loc=Local"
|
||||
|
||||
#[database]
|
||||
# [[database.default]]
|
||||
# type = "mysql"
|
||||
# link = "root:12345678@tcp(127.0.0.1:3306)/test?parseTime=true&loc=Local"
|
||||
#
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
37
.example/database/gdb/mysql/config/gdb.go
Normal file
37
.example/database/gdb/mysql/config/gdb.go
Normal file
@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var db gdb.DB
|
||||
|
||||
func init() {
|
||||
gdb.AddDefaultConfigNode(gdb.ConfigNode{
|
||||
Host: "127.0.0.1",
|
||||
Port: "3306",
|
||||
User: "root",
|
||||
Pass: "12345678",
|
||||
Name: "test",
|
||||
Type: "mysql",
|
||||
Role: "master",
|
||||
Charset: "utf8",
|
||||
MaxOpenConnCount: 100,
|
||||
})
|
||||
db, _ = gdb.New()
|
||||
}
|
||||
|
||||
func main() {
|
||||
wg := sync.WaitGroup{}
|
||||
for i := 0; i < 100000; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
time.Sleep(10 * time.Second)
|
||||
db.Table("user").Where("id=1").All()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
@ -1,533 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
// 本文件用于gf框架的mysql数据库操作示例,不作为单元测试使用
|
||||
|
||||
var db gdb.DB
|
||||
|
||||
// 初始化配置及创建数据库
|
||||
func init() {
|
||||
gdb.AddDefaultConfigNode(gdb.ConfigNode{
|
||||
Host: "127.0.0.1",
|
||||
Port: "3306",
|
||||
User: "root",
|
||||
Pass: "12345678",
|
||||
Name: "test",
|
||||
Type: "mysql",
|
||||
Role: "master",
|
||||
Charset: "utf8",
|
||||
})
|
||||
db, _ = gdb.New()
|
||||
|
||||
//gins.Config().SetPath("/home/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/.example/frame")
|
||||
//db = g.Database()
|
||||
|
||||
//gdb.SetConfig(gdb.ConfigNode {
|
||||
// Host : "127.0.0.1",
|
||||
// Port : 3306,
|
||||
// User : "root",
|
||||
// Pass : "123456",
|
||||
// Name : "test",
|
||||
// Type : "mysql",
|
||||
//})
|
||||
//db, _ = gdb.Instance()
|
||||
|
||||
//gdb.SetConfig(gdb.Config {
|
||||
// "default" : gdb.ConfigGroup {
|
||||
// gdb.ConfigNode {
|
||||
// Host : "127.0.0.1",
|
||||
// Port : "3306",
|
||||
// User : "root",
|
||||
// Pass : "123456",
|
||||
// Name : "test",
|
||||
// Type : "mysql",
|
||||
// Role : "master",
|
||||
// Weight : 100,
|
||||
// },
|
||||
// gdb.ConfigNode {
|
||||
// Host : "127.0.0.2",
|
||||
// Port : "3306",
|
||||
// User : "root",
|
||||
// Pass : "123456",
|
||||
// Name : "test",
|
||||
// Type : "mysql",
|
||||
// Role : "master",
|
||||
// Weight : 100,
|
||||
// },
|
||||
// gdb.ConfigNode {
|
||||
// Host : "127.0.0.3",
|
||||
// Port : "3306",
|
||||
// User : "root",
|
||||
// Pass : "123456",
|
||||
// Name : "test",
|
||||
// Type : "mysql",
|
||||
// Role : "master",
|
||||
// Weight : 100,
|
||||
// },
|
||||
// gdb.ConfigNode {
|
||||
// Host : "127.0.0.4",
|
||||
// Port : "3306",
|
||||
// User : "root",
|
||||
// Pass : "123456",
|
||||
// Name : "test",
|
||||
// Type : "mysql",
|
||||
// Role : "master",
|
||||
// Weight : 100,
|
||||
// },
|
||||
// },
|
||||
//})
|
||||
//db, _ = gdb.Instance()
|
||||
}
|
||||
|
||||
// 创建测试数据库
|
||||
func create() {
|
||||
fmt.Println("create:")
|
||||
_, err := db.Exec("CREATE DATABASE IF NOT EXISTS test")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
s := `
|
||||
CREATE TABLE IF NOT EXISTS user (
|
||||
uid INT(10) UNSIGNED AUTO_INCREMENT,
|
||||
name VARCHAR(45),
|
||||
PRIMARY KEY (uid)
|
||||
)
|
||||
ENGINE = InnoDB
|
||||
DEFAULT CHARACTER SET = utf8
|
||||
`
|
||||
_, err = db.Exec(s)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
s = `
|
||||
CREATE TABLE IF NOT EXISTS user_detail (
|
||||
uid INT(10) UNSIGNED AUTO_INCREMENT,
|
||||
site VARCHAR(255),
|
||||
PRIMARY KEY (uid)
|
||||
)
|
||||
ENGINE = InnoDB
|
||||
DEFAULT CHARACTER SET = utf8
|
||||
`
|
||||
|
||||
_, err = db.Exec(s)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 数据写入
|
||||
func insert() {
|
||||
fmt.Println("insert:")
|
||||
r, err := db.Insert("user", gdb.Map{
|
||||
"name": "john",
|
||||
})
|
||||
if err == nil {
|
||||
uid, err2 := r.LastInsertId()
|
||||
if err2 == nil {
|
||||
r, err = db.Insert("user_detail", gdb.Map{
|
||||
"uid": uid,
|
||||
"site": "http://johng.cn",
|
||||
})
|
||||
if err == nil {
|
||||
fmt.Printf("uid: %d\n", uid)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
} else {
|
||||
fmt.Println(err2)
|
||||
}
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 基本sql查询
|
||||
func query() {
|
||||
fmt.Println("query:")
|
||||
list, err := db.GetAll("select * from user limit 2")
|
||||
if err == nil {
|
||||
fmt.Println(list)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// replace into
|
||||
func replace() {
|
||||
fmt.Println("replace:")
|
||||
r, err := db.Save("user", gdb.Map{
|
||||
"uid": 1,
|
||||
"name": "john",
|
||||
})
|
||||
if err == nil {
|
||||
fmt.Println(r.LastInsertId())
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 数据保存
|
||||
func save() {
|
||||
fmt.Println("save:")
|
||||
r, err := db.Save("user", gdb.Map{
|
||||
"uid": 1,
|
||||
"name": "john",
|
||||
})
|
||||
if err == nil {
|
||||
fmt.Println(r.LastInsertId())
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 批量写入
|
||||
func batchInsert() {
|
||||
fmt.Println("batchInsert:")
|
||||
_, err := db.BatchInsert("user", gdb.List{
|
||||
{"name": "john_1"},
|
||||
{"name": "john_2"},
|
||||
{"name": "john_3"},
|
||||
{"name": "john_4"},
|
||||
}, 10)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 数据更新
|
||||
func update1() {
|
||||
fmt.Println("update1:")
|
||||
r, err := db.Update("user", gdb.Map{"name": "john1"}, "uid=?", 1)
|
||||
if err == nil {
|
||||
fmt.Println(r.LastInsertId())
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 数据更新
|
||||
func update2() {
|
||||
fmt.Println("update2:")
|
||||
r, err := db.Update("user", gdb.Map{"name": "john6"}, "uid=?", 1)
|
||||
if err == nil {
|
||||
fmt.Println(r.LastInsertId())
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 数据更新
|
||||
func update3() {
|
||||
fmt.Println("update3:")
|
||||
r, err := db.Update("user", "name=?", "uid=?", "john2", 1)
|
||||
if err == nil {
|
||||
fmt.Println(r.LastInsertId())
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 链式查询操作1
|
||||
func linkopSelect1() {
|
||||
fmt.Println("linkopSelect1:")
|
||||
r, err := db.Table("user u").LeftJoin("user_detail ud", "u.uid=ud.uid").Fields("u.*, ud.site").Where("u.uid > ?", 1).Limit(0, 2).Select()
|
||||
if err == nil {
|
||||
fmt.Println(r)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 链式查询操作2
|
||||
func linkopSelect2() {
|
||||
fmt.Println("linkopSelect2:")
|
||||
r, err := db.Table("user u").LeftJoin("user_detail ud", "u.uid=ud.uid").Fields("u.*,ud.site").Where("u.uid=?", 1).One()
|
||||
if err == nil {
|
||||
fmt.Println(r)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 链式查询操作3
|
||||
func linkopSelect3() {
|
||||
fmt.Println("linkopSelect3:")
|
||||
r, err := db.Table("user u").LeftJoin("user_detail ud", "u.uid=ud.uid").Fields("ud.site").Where("u.uid=?", 1).Value()
|
||||
if err == nil {
|
||||
fmt.Println(r.String())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 链式查询数量1
|
||||
func linkopCount1() {
|
||||
fmt.Println("linkopCount1:")
|
||||
r, err := db.Table("user u").Fields("uid").LeftJoin("user_detail ud", "u.uid=ud.uid").Where("u.uid=?", 1).Count()
|
||||
if err == nil {
|
||||
fmt.Println(r)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 错误操作
|
||||
func linkopUpdate1() {
|
||||
fmt.Println("linkopUpdate1:")
|
||||
r, err := db.Table("henghe_setting").Update()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 通过Map指针方式传参方式
|
||||
func linkopUpdate2() {
|
||||
fmt.Println("linkopUpdate2:")
|
||||
r, err := db.Table("user").Data(gdb.Map{"name": "john2"}).Where("name=?", "john_1").Update()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 通过字符串方式传参
|
||||
func linkopUpdate3() {
|
||||
fmt.Println("linkopUpdate3:")
|
||||
r, err := db.Table("user").Data("name='john3'").Where("name=?", "john2").Update()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// Where条件使用Map
|
||||
func linkopUpdate4() {
|
||||
fmt.Println("linkopUpdate4:")
|
||||
r, err := db.Table("user").Data(gdb.Map{"name": "john11111"}).Where(g.Map{"uid": 1}).Update()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 链式批量写入
|
||||
func linkopBatchInsert1() {
|
||||
fmt.Println("linkopBatchInsert1:")
|
||||
r, err := db.Table("user").Data(gdb.List{
|
||||
{"name": "john_1"},
|
||||
{"name": "john_2"},
|
||||
{"name": "john_3"},
|
||||
{"name": "john_4"},
|
||||
}).Insert()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 链式批量写入,指定每批次写入的条数
|
||||
func linkopBatchInsert2() {
|
||||
fmt.Println("linkopBatchInsert2:")
|
||||
r, err := db.Table("user").Data(gdb.List{
|
||||
{"name": "john_1"},
|
||||
{"name": "john_2"},
|
||||
{"name": "john_3"},
|
||||
{"name": "john_4"},
|
||||
}).Batch(2).Insert()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 链式批量保存
|
||||
func linkopBatchSave() {
|
||||
fmt.Println("linkopBatchSave:")
|
||||
r, err := db.Table("user").Data(gdb.List{
|
||||
{"uid": 1, "name": "john_1"},
|
||||
{"uid": 2, "name": "john_2"},
|
||||
{"uid": 3, "name": "john_3"},
|
||||
{"uid": 4, "name": "john_4"},
|
||||
}).Save()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 事务操作示例1
|
||||
func transaction1() {
|
||||
fmt.Println("transaction1:")
|
||||
if tx, err := db.Begin(); err == nil {
|
||||
r, err := tx.Save("user", gdb.Map{
|
||||
"uid": 1,
|
||||
"name": "john",
|
||||
})
|
||||
tx.Rollback()
|
||||
fmt.Println(r, err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 事务操作示例2
|
||||
func transaction2() {
|
||||
fmt.Println("transaction2:")
|
||||
if tx, err := db.Begin(); err == nil {
|
||||
r, err := tx.Table("user").Data(gdb.Map{"uid": 1, "name": "john_1"}).Save()
|
||||
tx.Commit()
|
||||
fmt.Println(r, err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 主从io复用测试,在mysql中使用 show full processlist 查看链接信息
|
||||
func keepPing() {
|
||||
fmt.Println("keepPing:")
|
||||
for {
|
||||
fmt.Println("ping...")
|
||||
err := db.PingMaster()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
err = db.PingSlave()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
// like语句查询
|
||||
func likeQuery() {
|
||||
fmt.Println("likeQuery:")
|
||||
if r, err := db.Table("user").Where("name like ?", "%john%").Select(); err == nil {
|
||||
fmt.Println(r)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
// mapToStruct
|
||||
func mapToStruct() {
|
||||
type User struct {
|
||||
Uid int
|
||||
Name string
|
||||
}
|
||||
fmt.Println("mapToStruct:")
|
||||
if r, err := db.Table("user").Where("uid=?", 1).One(); err == nil {
|
||||
u := User{}
|
||||
if err := r.ToStruct(&u); err == nil {
|
||||
fmt.Println(r)
|
||||
fmt.Println(u)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
// getQueriedSqls
|
||||
func getQueriedSqls() {
|
||||
for k, v := range db.GetQueriedSqls() {
|
||||
fmt.Println(k, ":")
|
||||
fmt.Println("Sql :", v.Sql)
|
||||
fmt.Println("Args :", v.Args)
|
||||
fmt.Println("Error:", v.Error)
|
||||
fmt.Println("Func :", v.Func)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
//data := g.Map{
|
||||
// "nickname" : "john",
|
||||
//}
|
||||
//db.SetDebug(true)
|
||||
//r, err := db.Table("user").Where("id=1").Data(data).Update()
|
||||
//fmt.Println(err)
|
||||
//fmt.Println(r.RowsAffected())
|
||||
|
||||
//data2 := g.Map{
|
||||
// "adsys1" : "ss",
|
||||
//}
|
||||
//db.SetDebug(true)
|
||||
//r, err := db.Table("cd_adsys").Where("adsys0=1").Data(data2).Update()
|
||||
//fmt.Println(err)
|
||||
//fmt.Println(r.RowsAffected())
|
||||
//return
|
||||
//db.SetDebug(true)
|
||||
//r, err := db.Table("test").Where("id=1").One()
|
||||
//fmt.Println(r["datetime"])
|
||||
//fmt.Println(r["datetime"].Time().Date())
|
||||
//fmt.Println(err)
|
||||
//create()
|
||||
//create()
|
||||
//insert()
|
||||
//query()
|
||||
//replace()
|
||||
//save()
|
||||
//batchInsert()
|
||||
//update1()
|
||||
//update2()
|
||||
//update3()
|
||||
linkopSelect1()
|
||||
//linkopSelect2()
|
||||
//linkopSelect3()
|
||||
//linkopCount1()
|
||||
//linkopUpdate1()
|
||||
//linkopUpdate2()
|
||||
//linkopUpdate3()
|
||||
//linkopUpdate4()
|
||||
//
|
||||
//transaction1()
|
||||
//transaction2()
|
||||
//
|
||||
//keepPing()
|
||||
//likeQuery()
|
||||
//mapToStruct()
|
||||
//getQueriedSqls()
|
||||
}
|
||||
24
.example/database/gdb/mysql/gdb_complecated.go
Normal file
24
.example/database/gdb/mysql/gdb_complecated.go
Normal file
@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// error!
|
||||
r, err := g.DB().Table("user").Where(g.Map{
|
||||
"or": g.Map{
|
||||
"nickname": "jim",
|
||||
"create_time > ": "2019-10-01",
|
||||
},
|
||||
"and": g.Map{
|
||||
"nickname": "tom",
|
||||
"create_time > ": "2019-10-01",
|
||||
},
|
||||
}).All()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
g.Dump(r)
|
||||
|
||||
}
|
||||
@ -1,8 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/os/glog"
|
||||
@ -23,7 +21,7 @@ func main() {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
db.SetDebug(true)
|
||||
//db.SetDebug(false)
|
||||
|
||||
glog.SetPath("/tmp")
|
||||
|
||||
@ -36,7 +34,4 @@ func main() {
|
||||
|
||||
db.Table("user").Data(g.Map{"name": "smith"}).Where("uid=?", 1).Save()
|
||||
|
||||
db.PrintQueriedSqls()
|
||||
|
||||
fmt.Println(db.GetLastSql())
|
||||
}
|
||||
|
||||
@ -6,6 +6,7 @@ import (
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
|
||||
// 执行3条SQL查询
|
||||
for i := 1; i <= 3; i++ {
|
||||
db.Table("user").Where("id=?", i).One()
|
||||
|
||||
@ -2,22 +2,26 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
// 开启调试模式,以便于记录所有执行的SQL
|
||||
//db := g.DB()
|
||||
|
||||
gdb.AddDefaultConfigNode(gdb.ConfigNode{
|
||||
LinkInfo: "root:12345678@tcp(127.0.0.1:3306)/test?parseTime=true&loc=Local",
|
||||
Type: "mysql",
|
||||
Charset: "utf8",
|
||||
})
|
||||
db, _ := gdb.New()
|
||||
|
||||
db.SetDebug(true)
|
||||
|
||||
r, e := db.Table("user").Data(g.Map{
|
||||
"passport": "1",
|
||||
"password": "1",
|
||||
"nickname": "1",
|
||||
"create_time": time.Now(),
|
||||
}).Insert()
|
||||
type User struct {
|
||||
CreateTime time.Time `orm:"create_time"`
|
||||
}
|
||||
r, e := db.Table("user").Data(User{CreateTime: time.Now()}).Insert()
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
|
||||
@ -2,23 +2,16 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
db.SetDebug(true)
|
||||
|
||||
type User struct {
|
||||
Id int
|
||||
Name *gtime.Time
|
||||
}
|
||||
|
||||
user := new(User)
|
||||
e := db.Table("test").Where("id", 10000).Struct(user)
|
||||
one, e := db.Table("order.order o").LeftJoin("user.user u", "o.uid=u.id").Where("u.id", 1).One()
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
g.Dump(user)
|
||||
g.Dump(one)
|
||||
|
||||
}
|
||||
|
||||
34
.example/database/gdb/mysql/issue364.go
Normal file
34
.example/database/gdb/mysql/issue364.go
Normal file
@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"time"
|
||||
)
|
||||
|
||||
func test1() {
|
||||
db := g.DB()
|
||||
db.SetDebug(true)
|
||||
time.Sleep(1 * time.Minute)
|
||||
r, e := db.Table("test").Where("id", 10000).Count()
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
g.Dump(r)
|
||||
}
|
||||
|
||||
func test2() {
|
||||
db := g.DB()
|
||||
db.SetDebug(true)
|
||||
dao := db.Table("test").Safe()
|
||||
time.Sleep(1 * time.Minute)
|
||||
r, e := dao.Where("id", 10000).Count()
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
g.Dump(r)
|
||||
}
|
||||
|
||||
func main() {
|
||||
test1()
|
||||
test2()
|
||||
}
|
||||
@ -6,6 +6,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
gdebug.PrintStack()
|
||||
fmt.Println(gdebug.CallerPackage())
|
||||
fmt.Println(gdebug.CallerFunction())
|
||||
}
|
||||
|
||||
10
.example/debug/gdebug/gdebug_info.go
Normal file
10
.example/debug/gdebug/gdebug_info.go
Normal file
@ -0,0 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/debug/gdebug"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println(gdebug.BuildInfo())
|
||||
}
|
||||
19
.example/encoding/gjson/issue360.go
Normal file
19
.example/encoding/gjson/issue360.go
Normal file
@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/encoding/gjson"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := `
|
||||
{"apiVersion":"v1","kind":"Service","metadata":{"labels":{"name":"http-daemon"},"name":"http-daemon","namespace":"default"},"spec":{"ports":[{"name":"http-daemon","port":8080,"protocol":"TCP","targetPort":9212}],"selector":{"app":"http-daemon","version":"v0930-082326"}}}
|
||||
`
|
||||
js, err := gjson.DecodeToJson(s)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
//g.Dump(js.ToMap())
|
||||
y, _ := js.ToYamlString()
|
||||
fmt.Println(y)
|
||||
}
|
||||
@ -1,33 +0,0 @@
|
||||
{
|
||||
"viewpath" : "/home/www/templates/",
|
||||
"database" : {
|
||||
"default" : [
|
||||
{
|
||||
"host" : "127.0.0.1",
|
||||
"port" : "3306",
|
||||
"user" : "root",
|
||||
"pass" : "123456",
|
||||
"name" : "test",
|
||||
"type" : "mysql",
|
||||
"role" : "master",
|
||||
"charset" : "utf8",
|
||||
"priority" : "1"
|
||||
},
|
||||
{
|
||||
"host" : "127.0.0.1",
|
||||
"port" : "3306",
|
||||
"user" : "root",
|
||||
"pass" : "123456",
|
||||
"name" : "test",
|
||||
"type" : "mysql",
|
||||
"role" : "master",
|
||||
"charset" : "utf8",
|
||||
"priority" : "1"
|
||||
}
|
||||
]
|
||||
},
|
||||
"redis" : {
|
||||
"disk" : "127.0.0.1:6379,0",
|
||||
"cache" : "127.0.0.1:6379,1"
|
||||
}
|
||||
}
|
||||
@ -1,4 +0,0 @@
|
||||
viewpath = "/home/www/templates"
|
||||
[redis]
|
||||
disk = "127.0.0.1:6379,0"
|
||||
cache = "127.0.0.1:6379,1"
|
||||
@ -1,35 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<config>
|
||||
<!-- 模板引擎目录 -->
|
||||
<viewpath>/home/www/templates/</viewpath>
|
||||
<!-- MySQL数据库配置 -->
|
||||
<database>
|
||||
<default>
|
||||
<host>127.0.0.1</host>
|
||||
<port>3306</port>
|
||||
<user>root</user>
|
||||
<pass>123456</pass>
|
||||
<name>test</name>
|
||||
<role>master</role>
|
||||
<type>mysql</type>
|
||||
<charset>utf8</charset>
|
||||
<priority>1</priority>
|
||||
</default>
|
||||
<default>
|
||||
<host>127.0.0.1</host>
|
||||
<port>3306</port>
|
||||
<user>root</user>
|
||||
<pass>123456</pass>
|
||||
<name>test</name>
|
||||
<role>master</role>
|
||||
<type>mysql</type>
|
||||
<charset>utf8</charset>
|
||||
<priority>1</priority>
|
||||
</default>
|
||||
</database>
|
||||
<!-- Redis数据库配置 -->
|
||||
<redis>
|
||||
<disk>127.0.0.1:6379,0</disk>
|
||||
<cache>127.0.0.1:6379,1</cache>
|
||||
</redis>
|
||||
</config>
|
||||
@ -1,27 +0,0 @@
|
||||
# 模板引擎目录
|
||||
viewpath: /home/www/templates/
|
||||
# MySQL数据库配置
|
||||
database:
|
||||
default:
|
||||
- host: 127.0.0.1
|
||||
port: 3306
|
||||
user: root
|
||||
pass: "8692651"
|
||||
name: test
|
||||
type: mysql
|
||||
role: master
|
||||
charset: utf8
|
||||
priority: 1
|
||||
- host: 127.0.0.1
|
||||
port: 3306
|
||||
user: root
|
||||
pass: "8692651"
|
||||
name: test
|
||||
type: mysql
|
||||
role: master
|
||||
charset: utf8
|
||||
priority: 1
|
||||
# Redis数据库配置
|
||||
redis:
|
||||
default: 127.0.0.1:6379,0
|
||||
cache : 127.0.0.1:6379,2
|
||||
@ -1,25 +0,0 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func init() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/apple", Apple)
|
||||
s.BindHandler("/pen", Pen)
|
||||
s.BindHandler("/apple-pen", ApplePen)
|
||||
}
|
||||
|
||||
func Apple(r *ghttp.Request) {
|
||||
r.Response.Write("Apple")
|
||||
}
|
||||
|
||||
func Pen(r *ghttp.Request) {
|
||||
r.Response.Write("Pen")
|
||||
}
|
||||
|
||||
func ApplePen(r *ghttp.Request) {
|
||||
r.Response.Write("Apple-Pen")
|
||||
}
|
||||
@ -1,16 +0,0 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
type Order struct{}
|
||||
|
||||
func init() {
|
||||
g.Server().BindObject("/{.struct}-{.method}", new(Order))
|
||||
}
|
||||
|
||||
func (o *Order) List(r *ghttp.Request) {
|
||||
r.Response.Write("List")
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/gins"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func init() {
|
||||
ghttp.GetServer().BindHandler("/config", func(r *ghttp.Request) {
|
||||
r.Response.Write(gins.Config().GetString("database.default.0.host"))
|
||||
})
|
||||
}
|
||||
@ -1,16 +0,0 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
)
|
||||
|
||||
func init() {
|
||||
ghttp.GetServer().BindHandler("/cookie", Cookie)
|
||||
}
|
||||
|
||||
func Cookie(r *ghttp.Request) {
|
||||
datetime := r.Cookie.Get("datetime")
|
||||
r.Cookie.Set("datetime", gtime.Datetime())
|
||||
r.Response.Write("datetime:" + datetime)
|
||||
}
|
||||
@ -1,20 +0,0 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
type ControllerDomain struct{}
|
||||
|
||||
// 初始化控制器对象,并绑定操作到Web Server
|
||||
func init() {
|
||||
// 只有localhost域名下才能访问该对象,
|
||||
// 对应URL为:http://localhost:8199/test/show
|
||||
// 通过该地址将无法访问到内容:http://127.0.0.1:8199/test/show
|
||||
ghttp.GetServer().Domain("localhost").BindObject("/domain", &ControllerDomain{})
|
||||
}
|
||||
|
||||
// 用于对象映射
|
||||
func (d *ControllerDomain) Show(r *ghttp.Request) {
|
||||
r.Response.Write("It's show time bibi!")
|
||||
}
|
||||
@ -1,24 +0,0 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/gmvc"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
type ControllerExit struct {
|
||||
gmvc.Controller
|
||||
}
|
||||
|
||||
func (c *ControllerExit) Init(r *ghttp.Request) {
|
||||
c.Controller.Init(r)
|
||||
c.Response.Write("exit, it will not print \"show\"")
|
||||
c.Request.Exit()
|
||||
}
|
||||
|
||||
func (c *ControllerExit) Show() {
|
||||
c.Response.Write("show")
|
||||
}
|
||||
|
||||
func init() {
|
||||
ghttp.GetServer().BindController("/exit", &ControllerExit{})
|
||||
}
|
||||
@ -1,36 +0,0 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func Form(r *ghttp.Request) {
|
||||
fmt.Println(r.GetPostMap())
|
||||
fmt.Println(r.GetPostString("name"))
|
||||
fmt.Println(r.GetPostString("age"))
|
||||
|
||||
}
|
||||
|
||||
func FormShow(r *ghttp.Request) {
|
||||
r.Response.Write(`
|
||||
<html>
|
||||
<head>
|
||||
<title>表单提交</title>
|
||||
</head>
|
||||
<body>
|
||||
<form enctype="application/x-www-form-urlencoded" action="/form" method="post">
|
||||
<input type="input" name="name" />
|
||||
<input type="input" name="age" />
|
||||
<input type="submit" value="submit" />
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
`)
|
||||
}
|
||||
|
||||
func init() {
|
||||
ghttp.GetServer().BindHandler("/form", Form)
|
||||
ghttp.GetServer().BindHandler("/form/show", FormShow)
|
||||
}
|
||||
@ -1,9 +0,0 @@
|
||||
package demo
|
||||
|
||||
import "github.com/gogf/gf/net/ghttp"
|
||||
|
||||
func init() {
|
||||
ghttp.GetServer().BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.Write("Hello World!")
|
||||
})
|
||||
}
|
||||
@ -1,30 +0,0 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/frame/gmvc"
|
||||
)
|
||||
|
||||
type Method struct {
|
||||
gmvc.Controller
|
||||
}
|
||||
|
||||
func init() {
|
||||
// 第三个参数指定主要注册的方法,其他方法不注册,方法名称会自动追加到给定路由后面,构成新路由
|
||||
// 以下注册会中注册两个新路由: /method/name, /method/age
|
||||
g.Server().BindController("/method", new(Method), "Name, Age")
|
||||
// 绑定路由到指定的方法执行,以下注册只会注册一个路由: /method-name
|
||||
g.Server().BindControllerMethod("/method-name", new(Method), "Name")
|
||||
}
|
||||
|
||||
func (c *Method) Name() {
|
||||
c.Response.Write("John")
|
||||
}
|
||||
|
||||
func (c *Method) Age() {
|
||||
c.Response.Write("18")
|
||||
}
|
||||
|
||||
func (c *Method) Info() {
|
||||
c.Response.Write("Info")
|
||||
}
|
||||
@ -1,20 +0,0 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
type Object struct{}
|
||||
|
||||
func init() {
|
||||
g.Server().BindObject("/object", new(Object))
|
||||
}
|
||||
|
||||
func (o *Object) Index(r *ghttp.Request) {
|
||||
r.Response.Write("object index")
|
||||
}
|
||||
|
||||
func (o *Object) Show(r *ghttp.Request) {
|
||||
r.Response.Write("object show")
|
||||
}
|
||||
@ -1,31 +0,0 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
type ObjectMethod struct{}
|
||||
|
||||
func init() {
|
||||
obj := &ObjectMethod{}
|
||||
g.Server().BindObject("/object-method", obj, "Show1, Show2, Show3")
|
||||
g.Server().BindObjectMethod("/object-method-show1", obj, "Show1")
|
||||
g.Server().Domain("localhost").BindObject("/object-method", obj, "Show4")
|
||||
}
|
||||
|
||||
func (o *ObjectMethod) Show1(r *ghttp.Request) {
|
||||
r.Response.Write("show 1")
|
||||
}
|
||||
|
||||
func (o *ObjectMethod) Show2(r *ghttp.Request) {
|
||||
r.Response.Write("show 2")
|
||||
}
|
||||
|
||||
func (o *ObjectMethod) Show3(r *ghttp.Request) {
|
||||
r.Response.Write("show 3")
|
||||
}
|
||||
|
||||
func (o *ObjectMethod) Show4(r *ghttp.Request) {
|
||||
r.Response.Write("show 4")
|
||||
}
|
||||
@ -1,30 +0,0 @@
|
||||
package demo
|
||||
|
||||
import "github.com/gogf/gf/net/ghttp"
|
||||
|
||||
// 测试绑定对象
|
||||
type ObjectRest struct{}
|
||||
|
||||
func init() {
|
||||
ghttp.GetServer().BindObjectRest("/object-rest", &ObjectRest{})
|
||||
}
|
||||
|
||||
// RESTFul - GET
|
||||
func (o *ObjectRest) Get(r *ghttp.Request) {
|
||||
r.Response.Write("RESTFul HTTP Method GET")
|
||||
}
|
||||
|
||||
// RESTFul - POST
|
||||
func (c *ObjectRest) Post(r *ghttp.Request) {
|
||||
r.Response.Write("RESTFul HTTP Method POST")
|
||||
}
|
||||
|
||||
// RESTFul - DELETE
|
||||
func (c *ObjectRest) Delete(r *ghttp.Request) {
|
||||
r.Response.Write("RESTFul HTTP Method DELETE")
|
||||
}
|
||||
|
||||
// 该方法无法映射,将会无法访问到
|
||||
func (c *ObjectRest) Hello(r *ghttp.Request) {
|
||||
r.Response.Write("Hello")
|
||||
}
|
||||
@ -1,25 +0,0 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
type Product struct {
|
||||
total int
|
||||
}
|
||||
|
||||
func init() {
|
||||
p := &Product{}
|
||||
g.Server().BindHandler("/product/total", p.Total)
|
||||
g.Server().BindHandler("/product/list/{page}.html", p.List)
|
||||
}
|
||||
|
||||
func (p *Product) Total(r *ghttp.Request) {
|
||||
p.total++
|
||||
r.Response.Write("total: ", p.total)
|
||||
}
|
||||
|
||||
func (p *Product) List(r *ghttp.Request) {
|
||||
r.Response.Write("page: ", r.Get("page"))
|
||||
}
|
||||
@ -1,34 +0,0 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/frame/gmvc"
|
||||
)
|
||||
|
||||
type Rest struct {
|
||||
gmvc.Controller
|
||||
}
|
||||
|
||||
func init() {
|
||||
g.Server().BindControllerRest("/rest", &Rest{})
|
||||
}
|
||||
|
||||
// RESTFul - GET
|
||||
func (c *Rest) Get() {
|
||||
c.Response.Write("RESTFul HTTP Method GET")
|
||||
}
|
||||
|
||||
// RESTFul - POST
|
||||
func (c *Rest) Post() {
|
||||
c.Response.Write("RESTFul HTTP Method POST")
|
||||
}
|
||||
|
||||
// RESTFul - DELETE
|
||||
func (c *Rest) Delete() {
|
||||
c.Response.Write("RESTFul HTTP Method DELETE")
|
||||
}
|
||||
|
||||
// 该方法无法映射,将会无法访问到
|
||||
func (c *Rest) Hello() {
|
||||
c.Response.Write("Hello")
|
||||
}
|
||||
@ -1,18 +0,0 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/frame/gmvc"
|
||||
)
|
||||
|
||||
type ControllerRule struct {
|
||||
gmvc.Controller
|
||||
}
|
||||
|
||||
func init() {
|
||||
g.Server().BindController("/rule/{method}/:name", &ControllerRule{})
|
||||
}
|
||||
|
||||
func (c *ControllerRule) Show() {
|
||||
c.Response.Write(c.Request.Get("name"))
|
||||
}
|
||||
@ -1,17 +0,0 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func init() {
|
||||
ghttp.GetServer().BindHandler("/session", Session)
|
||||
}
|
||||
|
||||
func Session(r *ghttp.Request) {
|
||||
id := r.Session.GetInt("id")
|
||||
r.Session.Set("id", id+1)
|
||||
r.Response.Write("id:" + strconv.Itoa(id))
|
||||
}
|
||||
@ -1,23 +0,0 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/gmvc"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
type ControllerTemplate struct {
|
||||
gmvc.Controller
|
||||
}
|
||||
|
||||
func (c *ControllerTemplate) Info() {
|
||||
c.View.Assign("name", "john")
|
||||
c.View.Assigns(map[string]interface{}{
|
||||
"age": 18,
|
||||
"score": 100,
|
||||
})
|
||||
c.View.Display("view/user/index.tpl")
|
||||
}
|
||||
|
||||
func init() {
|
||||
ghttp.GetServer().BindController("/template", &ControllerTemplate{})
|
||||
}
|
||||
@ -1,16 +0,0 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func init() {
|
||||
ghttp.GetServer().BindHandler("/template2", func(r *ghttp.Request) {
|
||||
content, _ := g.View().Parse("index.tpl", map[string]interface{}{
|
||||
"id": 123,
|
||||
"name": "john",
|
||||
})
|
||||
r.Response.Write(content)
|
||||
})
|
||||
}
|
||||
@ -1,17 +0,0 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/gins"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func init() {
|
||||
gins.View().SetPath("/home/www/template/")
|
||||
ghttp.GetServer().BindHandler("/template3", func(r *ghttp.Request) {
|
||||
content, _ := gins.View().Parse("index.tpl", map[string]interface{}{
|
||||
"id": 123,
|
||||
"name": "john",
|
||||
})
|
||||
r.Response.Write(content)
|
||||
})
|
||||
}
|
||||
@ -1,40 +0,0 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
)
|
||||
|
||||
func Upload(r *ghttp.Request) {
|
||||
if f, h, e := r.FormFile("upload-file"); e == nil {
|
||||
defer f.Close()
|
||||
fname := gfile.Basename(h.Filename)
|
||||
buffer := make([]byte, h.Size)
|
||||
f.Read(buffer)
|
||||
gfile.PutBytes("/tmp/"+fname, buffer)
|
||||
r.Response.Write(fname + " uploaded successly")
|
||||
} else {
|
||||
r.Response.Write(e.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func UploadShow(r *ghttp.Request) {
|
||||
r.Response.Write(`
|
||||
<html>
|
||||
<head>
|
||||
<title>上传文件</title>
|
||||
</head>
|
||||
<body>
|
||||
<form enctype="multipart/form-data" action="/upload" method="post">
|
||||
<input type="file" name="upload-file" />
|
||||
<input type="submit" value="upload" />
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
`)
|
||||
}
|
||||
|
||||
func init() {
|
||||
ghttp.GetServer().BindHandler("/upload", Upload)
|
||||
ghttp.GetServer().BindHandler("/upload/show", UploadShow)
|
||||
}
|
||||
@ -1,29 +0,0 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/frame/gmvc"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
gmvc.Controller
|
||||
}
|
||||
|
||||
func init() {
|
||||
s := g.Server()
|
||||
s.BindController("/user", new(User))
|
||||
s.BindController("/user/{.method}/{uid}", new(User), "Info")
|
||||
s.BindController("/user/{.method}/{page}.html", new(User), "List")
|
||||
}
|
||||
|
||||
func (u *User) Index() {
|
||||
u.Response.Write("User")
|
||||
}
|
||||
|
||||
func (u *User) Info() {
|
||||
u.Response.Write("Info - Uid: ", u.Request.Get("uid"))
|
||||
}
|
||||
|
||||
func (u *User) List() {
|
||||
u.Response.Write("List - Page: ", u.Request.Get("page"))
|
||||
}
|
||||
@ -1,19 +0,0 @@
|
||||
package stats
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
var (
|
||||
total int
|
||||
)
|
||||
|
||||
func init() {
|
||||
g.Server().BindHandler("/stats/total", showTotal)
|
||||
}
|
||||
|
||||
func showTotal(r *ghttp.Request) {
|
||||
total++
|
||||
r.Response.Write("total:", total)
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
_ "github.com/gogf/gf/.example/frame/mvc/controller/demo"
|
||||
_ "github.com/gogf/gf/.example/frame/mvc/controller/stats"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
//g.Server().SetDumpRouteMap(false)
|
||||
g.Server().SetPort(8199)
|
||||
g.Server().Run()
|
||||
|
||||
}
|
||||
@ -1,8 +0,0 @@
|
||||
package test
|
||||
|
||||
import "github.com/gogf/gf/database/gdb"
|
||||
|
||||
var (
|
||||
// ConfigGroup is the configuration group name for this model.
|
||||
ConfigGroup = gdb.DEFAULT_GROUP_NAME
|
||||
)
|
||||
@ -1,92 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
)
|
||||
|
||||
// User is the golang structure for table user.
|
||||
type User struct {
|
||||
Id int `orm:"id,primary" json:"id"`
|
||||
Passport string `orm:"passport" json:"passport"`
|
||||
Password string `orm:"password" json:"password"`
|
||||
NickName string `orm:"nickname" json:"nick_name"`
|
||||
CreateTime *gtime.Time `orm:"create_time" json:"create_time"`
|
||||
}
|
||||
|
||||
// UserModel is the model of convenient operations for table user.
|
||||
type UserModel struct {
|
||||
*gdb.Model
|
||||
TableName string
|
||||
}
|
||||
|
||||
var (
|
||||
// UserTableName is the table name of user.
|
||||
UserTableName = "user"
|
||||
)
|
||||
|
||||
// ModelUser creates and returns a new model object for table user.
|
||||
func ModelUser() *UserModel {
|
||||
return &UserModel{
|
||||
g.DB(ConfigGroup).Table(UserTableName).Safe(),
|
||||
UserTableName,
|
||||
}
|
||||
}
|
||||
|
||||
// Inserts does "INSERT...INTO..." statement for inserting current object into table.
|
||||
func (r *User) Insert() (result sql.Result, err error) {
|
||||
return ModelUser().Data(r).Insert()
|
||||
}
|
||||
|
||||
// Replace does "REPLACE...INTO..." statement for inserting current object into table.
|
||||
// If there's already another same record in the table (it checks using primary key or unique index),
|
||||
// it deletes it and insert this one.
|
||||
func (r *User) Replace() (result sql.Result, err error) {
|
||||
return ModelUser().Data(r).Replace()
|
||||
}
|
||||
|
||||
// Save does "INSERT...INTO..." statement for inserting/updating current object into table.
|
||||
// It updates the record if there's already another same record in the table
|
||||
// (it checks using primary key or unique index).
|
||||
func (r *User) Save() (result sql.Result, err error) {
|
||||
return ModelUser().Data(r).Save()
|
||||
}
|
||||
|
||||
// Update does "UPDATE...WHERE..." statement for updating current object from table.
|
||||
// It updates the record if there's already another same record in the table
|
||||
// (it checks using primary key or unique index).
|
||||
func (r *User) Update() (result sql.Result, err error) {
|
||||
return ModelUser().Data(r).Where(gdb.GetWhereConditionOfStruct(r)).Update()
|
||||
}
|
||||
|
||||
// Delete does "DELETE FROM...WHERE..." statement for deleting current object from table.
|
||||
func (r *User) Delete() (result sql.Result, err error) {
|
||||
return ModelUser().Where(gdb.GetWhereConditionOfStruct(r)).Delete()
|
||||
}
|
||||
|
||||
// Select overwrite the Select method from gdb.Model for model
|
||||
// as retuning all objects with specified structure.
|
||||
func (m *UserModel) Select() ([]*User, error) {
|
||||
array := ([]*User)(nil)
|
||||
if err := m.Scan(&array); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return array, nil
|
||||
}
|
||||
|
||||
// First does the same logistics as One method from gdb.Model for model
|
||||
// as retuning first/one object with specified structure.
|
||||
func (m *UserModel) First() (*User, error) {
|
||||
list, err := m.Select()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(list) > 0 {
|
||||
return list[0], nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
@ -1,11 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>上传文件</title>
|
||||
</head>
|
||||
<body>
|
||||
<form enctype="multipart/form-data" action="/upload" method="post">
|
||||
<input type="file" name="upload-file" />
|
||||
<input type="submit" value="upload" />
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
@ -1,2 +0,0 @@
|
||||
<h3>This is footer</h3>
|
||||
<div style="color:red">tpl vals: {{.}}</div>
|
||||
@ -1,11 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<title></title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h3>This is index</h3>
|
||||
<p>tpl vals: {{.}}</p>
|
||||
{{include "user/footer.tpl" }}
|
||||
</body>
|
||||
</html>
|
||||
@ -1,19 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
c := ghttp.NewClient()
|
||||
c.Transport = &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
}
|
||||
r, e := c.Clone().Get("https://127.0.0.1:8199")
|
||||
fmt.Println(e)
|
||||
fmt.Println(r.StatusCode)
|
||||
r, err := ghttp.Get("http://127.0.0.1:8199/11111/11122")
|
||||
fmt.Println(err)
|
||||
fmt.Println(r.Header)
|
||||
}
|
||||
|
||||
22
.example/net/ghttp/client/upload-batch/client.go
Normal file
22
.example/net/ghttp/client/upload-batch/client.go
Normal file
@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/os/glog"
|
||||
)
|
||||
|
||||
func main() {
|
||||
path1 := "/Users/john/Pictures/logo1.png"
|
||||
path2 := "/Users/john/Pictures/logo2.png"
|
||||
r, e := ghttp.Post(
|
||||
"http://127.0.0.1:8199/upload",
|
||||
fmt.Sprintf(`upload-file=@file:%s&upload-file=@file:%s`, path1, path2),
|
||||
)
|
||||
if e != nil {
|
||||
glog.Error(e)
|
||||
} else {
|
||||
fmt.Println(string(r.ReadAll()))
|
||||
r.Close()
|
||||
}
|
||||
}
|
||||
22
.example/net/ghttp/client/upload-batch/client2.go
Normal file
22
.example/net/ghttp/client/upload-batch/client2.go
Normal file
@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/os/glog"
|
||||
)
|
||||
|
||||
func main() {
|
||||
path1 := "/Users/john/Pictures/logo1.png"
|
||||
path2 := "/Users/john/Pictures/logo2.png"
|
||||
r, e := ghttp.Post(
|
||||
"http://127.0.0.1:8199/upload",
|
||||
fmt.Sprintf(`upload-file[]=@file:%s&upload-file[]=@file:%s`, path1, path2),
|
||||
)
|
||||
if e != nil {
|
||||
glog.Error(e)
|
||||
} else {
|
||||
fmt.Println(string(r.ReadAll()))
|
||||
r.Close()
|
||||
}
|
||||
}
|
||||
80
.example/net/ghttp/client/upload-batch/server.go
Normal file
80
.example/net/ghttp/client/upload-batch/server.go
Normal file
@ -0,0 +1,80 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
"io"
|
||||
)
|
||||
|
||||
// Upload uploads files to /tmp .
|
||||
func Upload(r *ghttp.Request) {
|
||||
saveDir := "/tmp/"
|
||||
for _, item := range r.GetMultipartFiles("upload-file") {
|
||||
file, err := item.Open()
|
||||
if err != nil {
|
||||
r.Response.Write(err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
f, err := gfile.Create(saveDir + gfile.Basename(item.Filename))
|
||||
if err != nil {
|
||||
r.Response.Write(err)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
if _, err := io.Copy(f, file); err != nil {
|
||||
r.Response.Write(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
r.Response.Write("upload successfully")
|
||||
}
|
||||
|
||||
// UploadShow shows uploading simgle file page.
|
||||
func UploadShow(r *ghttp.Request) {
|
||||
r.Response.Write(`
|
||||
<html>
|
||||
<head>
|
||||
<title>GF Upload File Demo</title>
|
||||
</head>
|
||||
<body>
|
||||
<form enctype="multipart/form-data" action="/upload" method="post">
|
||||
<input type="file" name="upload-file" />
|
||||
<input type="submit" value="upload" />
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
`)
|
||||
}
|
||||
|
||||
// UploadShowBatch shows uploading multiple files page.
|
||||
func UploadShowBatch(r *ghttp.Request) {
|
||||
r.Response.Write(`
|
||||
<html>
|
||||
<head>
|
||||
<title>GF Upload Files Demo</title>
|
||||
</head>
|
||||
<body>
|
||||
<form enctype="multipart/form-data" action="/upload" method="post">
|
||||
<input type="file" name="upload-file" />
|
||||
<input type="file" name="upload-file" />
|
||||
<input type="submit" value="upload" />
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
`)
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/upload", func(group *ghttp.RouterGroup) {
|
||||
group.ALL("/", Upload)
|
||||
group.ALL("/show", UploadShow)
|
||||
group.ALL("/batch", UploadShowBatch)
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
@ -9,7 +9,7 @@ import (
|
||||
|
||||
func main() {
|
||||
path := "/home/john/Workspace/Go/github.com/gogf/gf/version.go"
|
||||
r, e := ghttp.Post("http://127.0.0.1:8199/upload", "name=john&age=18&upload-file=@file:"+path)
|
||||
r, e := ghttp.Post("http://127.0.0.1:8199/upload", "upload-file=@file:"+path)
|
||||
if e != nil {
|
||||
glog.Error(e)
|
||||
} else {
|
||||
|
||||
@ -4,28 +4,41 @@ import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
"io"
|
||||
)
|
||||
|
||||
// 执行文件上传处理,上传到系统临时目录 /tmp
|
||||
// Upload uploads files to /tmp .
|
||||
func Upload(r *ghttp.Request) {
|
||||
if f, h, e := r.FormFile("upload-file"); e == nil {
|
||||
saveDir := "/tmp/"
|
||||
for _, item := range r.GetMultipartFiles("upload-file") {
|
||||
file, err := item.Open()
|
||||
if err != nil {
|
||||
r.Response.Write(err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
f, err := gfile.Create(saveDir + gfile.Basename(item.Filename))
|
||||
if err != nil {
|
||||
r.Response.Write(err)
|
||||
return
|
||||
}
|
||||
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())
|
||||
|
||||
if _, err := io.Copy(f, file); err != nil {
|
||||
r.Response.Write(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
r.Response.Write("upload successfully")
|
||||
}
|
||||
|
||||
// 展示文件上传页面
|
||||
// UploadShow shows uploading simgle file page.
|
||||
func UploadShow(r *ghttp.Request) {
|
||||
r.Response.Write(`
|
||||
<html>
|
||||
<head>
|
||||
<title>上传文件</title>
|
||||
<title>GF Upload File Demo</title>
|
||||
</head>
|
||||
<body>
|
||||
<form enctype="multipart/form-data" action="/upload" method="post">
|
||||
@ -37,10 +50,31 @@ func UploadShow(r *ghttp.Request) {
|
||||
`)
|
||||
}
|
||||
|
||||
// UploadShowBatch shows uploading multiple files page.
|
||||
func UploadShowBatch(r *ghttp.Request) {
|
||||
r.Response.Write(`
|
||||
<html>
|
||||
<head>
|
||||
<title>GF Upload Files Demo</title>
|
||||
</head>
|
||||
<body>
|
||||
<form enctype="multipart/form-data" action="/upload" method="post">
|
||||
<input type="file" name="upload-file" />
|
||||
<input type="file" name="upload-file" />
|
||||
<input type="submit" value="upload" />
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
`)
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/upload", Upload)
|
||||
s.BindHandler("/upload/show", UploadShow)
|
||||
s.Group("/upload", func(group *ghttp.RouterGroup) {
|
||||
group.ALL("/", Upload)
|
||||
group.ALL("/show", UploadShow)
|
||||
group.ALL("/batch", UploadShowBatch)
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -16,8 +16,8 @@ func Order(r *ghttp.Request) {
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/api.v1", func(g *ghttp.RouterGroup) {
|
||||
g.Middleware(MiddlewareCORS)
|
||||
s.Group("/api.v1", func(group *ghttp.RouterGroup) {
|
||||
group.Middleware(MiddlewareCORS)
|
||||
g.GET("/order", Order)
|
||||
})
|
||||
s.SetPort(8199)
|
||||
|
||||
@ -18,8 +18,8 @@ func Order(r *ghttp.Request) {
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/api.v1", func(g *ghttp.RouterGroup) {
|
||||
g.Middleware(MiddlewareCORS)
|
||||
s.Group("/api.v1", func(group *ghttp.RouterGroup) {
|
||||
group.Middleware(MiddlewareCORS)
|
||||
g.GET("/order", Order)
|
||||
})
|
||||
s.SetPort(8199)
|
||||
|
||||
@ -24,8 +24,8 @@ func Order(r *ghttp.Request) {
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/api.v1", func(g *ghttp.RouterGroup) {
|
||||
g.Middleware(MiddlewareCORS)
|
||||
s.Group("/api.v1", func(group *ghttp.RouterGroup) {
|
||||
group.Middleware(MiddlewareCORS)
|
||||
g.GET("/order", Order)
|
||||
})
|
||||
s.SetPort(8199)
|
||||
|
||||
9
.example/net/ghttp/server/form/form-client.go
Normal file
9
.example/net/ghttp/server/form/form-client.go
Normal file
@ -0,0 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ghttp.PostContent("http://127.0.0.1:8199/", "array[]=1&array[]=2")
|
||||
}
|
||||
@ -8,7 +8,7 @@ import (
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
g.Dump(r.GetPostMap())
|
||||
g.Dump(r.GetForm("array"))
|
||||
r.Response.WriteTpl("form.html")
|
||||
})
|
||||
s.SetPort(8199)
|
||||
|
||||
@ -3,14 +3,16 @@ package main
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/os/glog"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.SetIndexFolder(true)
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
glog.Println(r.Header)
|
||||
r.Response.Write("hello world")
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.SetPort(8999)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -11,7 +11,7 @@ func Order(r *ghttp.Request) {
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/api.v1", func(g *ghttp.RouterGroup) {
|
||||
s.Group("/api.v1", func(group *ghttp.RouterGroup) {
|
||||
g.GET("/order", Order)
|
||||
})
|
||||
s.SetPort(8199)
|
||||
|
||||
@ -11,8 +11,8 @@ func Order(r *ghttp.Request) {
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/api.v1", func(g *ghttp.RouterGroup) {
|
||||
g.Hook("/*any", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
||||
s.Group("/api.v1", func(group *ghttp.RouterGroup) {
|
||||
group.Hook("/*any", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
||||
r.Response.CORSDefault()
|
||||
})
|
||||
g.GET("/order", Order)
|
||||
|
||||
@ -1,32 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
// 优先调用的HOOK
|
||||
func beforeServeHook1(r *ghttp.Request) {
|
||||
r.SetParam("name", "GoFrame")
|
||||
r.Response.Writeln("set name")
|
||||
}
|
||||
|
||||
// 随后调用的HOOK
|
||||
func beforeServeHook2(r *ghttp.Request) {
|
||||
r.SetParam("site", "https://goframe.org")
|
||||
r.Response.Writeln("set site")
|
||||
}
|
||||
|
||||
// 允许对同一个路由同一个事件注册多个回调函数,按照注册顺序进行优先级调用。
|
||||
// 为便于在路由表中对比查看优先级,这里讲HOOK回调函数单独定义为了两个函数。
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.Writeln(r.Get("name"))
|
||||
r.Response.Writeln(r.Get("site"))
|
||||
})
|
||||
s.BindHookHandler("/", ghttp.HOOK_BEFORE_SERVE, beforeServeHook1)
|
||||
s.BindHookHandler("/", ghttp.HOOK_BEFORE_SERVE, beforeServeHook2)
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
@ -1,11 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := ghttp.GetServer()
|
||||
s := g.Server()
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.Writeln("来自于HTTPS的:哈喽世界!")
|
||||
})
|
||||
|
||||
@ -1,11 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := ghttp.GetServer()
|
||||
s := g.Server()
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.Writeln("您可以同时通过HTTP和HTTPS方式看到该内容!")
|
||||
})
|
||||
|
||||
7
.example/net/ghttp/server/log/config.toml
Normal file
7
.example/net/ghttp/server/log/config.toml
Normal file
@ -0,0 +1,7 @@
|
||||
[server]
|
||||
LogPath = "/tmp/gflog/server"
|
||||
LogStdout = true
|
||||
ErrorLogEnabled = true
|
||||
ErrorLogPattern = "error.log"
|
||||
AccessLogEnabled = true
|
||||
AccessLogPattern = "access.log"
|
||||
@ -1,26 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := ghttp.GetServer()
|
||||
s.BindHandler("/log/handler", func(r *ghttp.Request) {
|
||||
r.Response.WriteStatus(http.StatusNotFound, "文件找不到了")
|
||||
s := g.Server()
|
||||
s.Group("/", func(group *ghttp.RouterGroup) {
|
||||
group.ALL("/", func(r *ghttp.Request) {
|
||||
r.Response.Write("halo world!")
|
||||
})
|
||||
group.ALL("/log/handler", func(r *ghttp.Request) {
|
||||
r.Response.WriteStatus(http.StatusNotFound, "File Not Found!")
|
||||
})
|
||||
})
|
||||
s.SetAccessLogEnabled(true)
|
||||
s.SetErrorLogEnabled(true)
|
||||
//s.SetLogHandler(func(r *ghttp.Request, error ...interface{}) {
|
||||
// if len(error) > 0 {
|
||||
// // 如果是错误日志
|
||||
// fmt.Println("错误产生了:", error[0])
|
||||
// }
|
||||
// // 这里是请求日志
|
||||
// fmt.Println("请求处理完成,请求地址:", r.URL.String(), "请求结果:", r.Response.Status)
|
||||
//})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -7,9 +7,7 @@ import (
|
||||
func main() {
|
||||
s := ghttp.GetServer()
|
||||
s.BindHandler("/log/error", func(r *ghttp.Request) {
|
||||
if j := r.GetJson(); j != nil {
|
||||
r.Response.Write(j.Get("test"))
|
||||
}
|
||||
panic("OMG")
|
||||
})
|
||||
s.SetErrorLogEnabled(true)
|
||||
s.SetPort(8199)
|
||||
|
||||
@ -23,9 +23,9 @@ func MiddlewareCORS(r *ghttp.Request) {
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/api.v2", func(g *ghttp.RouterGroup) {
|
||||
g.Middleware(MiddlewareAuth, MiddlewareCORS)
|
||||
g.ALL("/user/list", func(r *ghttp.Request) {
|
||||
s.Group("/api.v2", func(group *ghttp.RouterGroup) {
|
||||
group.Middleware(MiddlewareAuth, MiddlewareCORS)
|
||||
group.ALL("/user/list", func(r *ghttp.Request) {
|
||||
r.Response.Write("list")
|
||||
})
|
||||
})
|
||||
|
||||
@ -18,7 +18,7 @@ func MiddlewareAuth(r *ghttp.Request) {
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/admin", func(g *ghttp.RouterGroup) {
|
||||
s.Group("/admin", func(group *ghttp.RouterGroup) {
|
||||
g.MiddlewarePattern("/*action", func(r *ghttp.Request) {
|
||||
if action := r.GetRouterString("action"); action != "" {
|
||||
switch action {
|
||||
@ -29,10 +29,10 @@ func main() {
|
||||
}
|
||||
MiddlewareAuth(r)
|
||||
})
|
||||
g.ALL("/login", func(r *ghttp.Request) {
|
||||
group.ALL("/login", func(r *ghttp.Request) {
|
||||
r.Response.Write("login")
|
||||
})
|
||||
g.ALL("/dashboard", func(r *ghttp.Request) {
|
||||
group.ALL("/dashboard", func(r *ghttp.Request) {
|
||||
r.Response.Write("dashboard")
|
||||
})
|
||||
})
|
||||
|
||||
@ -12,9 +12,9 @@ func MiddlewareCORS(r *ghttp.Request) {
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/api.v2", func(g *ghttp.RouterGroup) {
|
||||
g.Middleware(MiddlewareCORS)
|
||||
g.ALL("/user/list", func(r *ghttp.Request) {
|
||||
s.Group("/api.v2", func(group *ghttp.RouterGroup) {
|
||||
group.Middleware(MiddlewareCORS)
|
||||
group.ALL("/user/list", func(r *ghttp.Request) {
|
||||
r.Response.Write("list")
|
||||
})
|
||||
})
|
||||
|
||||
@ -31,9 +31,9 @@ func MiddlewareError(r *ghttp.Request) {
|
||||
|
||||
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) {
|
||||
s.Group("/api.v2", func(group *ghttp.RouterGroup) {
|
||||
group.Middleware(MiddlewareAuth, MiddlewareCORS, MiddlewareError)
|
||||
group.ALL("/user/list", func(r *ghttp.Request) {
|
||||
panic("db error: sql is xxxxxxx")
|
||||
})
|
||||
})
|
||||
|
||||
21
.example/net/ghttp/server/middleware/issue355.go
Normal file
21
.example/net/ghttp/server/middleware/issue355.go
Normal file
@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
fmt.Println("cors")
|
||||
r.Response.CORSDefault()
|
||||
r.Middleware.Next()
|
||||
})
|
||||
s.BindHandler("/api/captcha", func(r *ghttp.Request) {
|
||||
r.Response.Write("captcha")
|
||||
})
|
||||
s.SetPort(8010)
|
||||
s.Run()
|
||||
}
|
||||
@ -30,12 +30,12 @@ func MiddlewareLog(r *ghttp.Request) {
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/", func(g *ghttp.RouterGroup) {
|
||||
g.Middleware(MiddlewareLog)
|
||||
s.Group("/", func(group *ghttp.RouterGroup) {
|
||||
group.Middleware(MiddlewareLog)
|
||||
})
|
||||
s.Group("/api.v2", func(g *ghttp.RouterGroup) {
|
||||
g.Middleware(MiddlewareAuth, MiddlewareCORS)
|
||||
g.ALL("/user/list", func(r *ghttp.Request) {
|
||||
s.Group("/api.v2", func(group *ghttp.RouterGroup) {
|
||||
group.Middleware(MiddlewareAuth, MiddlewareCORS)
|
||||
group.ALL("/user/list", func(r *ghttp.Request) {
|
||||
panic("custom error")
|
||||
})
|
||||
})
|
||||
|
||||
@ -7,18 +7,18 @@ import (
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/api.v2", func(g *ghttp.RouterGroup) {
|
||||
g.Middleware(func(r *ghttp.Request) {
|
||||
s.Group("/api.v2", func(group *ghttp.RouterGroup) {
|
||||
group.Middleware(func(r *ghttp.Request) {
|
||||
r.Response.Write("start")
|
||||
r.Middleware.Next()
|
||||
r.Response.Write("end")
|
||||
})
|
||||
g.Group("/order", func(g *ghttp.RouterGroup) {
|
||||
g.Group("/order", func(group *ghttp.RouterGroup) {
|
||||
g.GET("/list", func(r *ghttp.Request) {
|
||||
r.Response.Write("list")
|
||||
})
|
||||
})
|
||||
g.Group("/user", func(g *ghttp.RouterGroup) {
|
||||
g.Group("/user", func(group *ghttp.RouterGroup) {
|
||||
g.GET("/info", func(r *ghttp.Request) {
|
||||
r.Response.Write("info")
|
||||
})
|
||||
@ -26,11 +26,11 @@ func main() {
|
||||
r.Response.Write("edit")
|
||||
})
|
||||
})
|
||||
g.Group("/hook", func(g *ghttp.RouterGroup) {
|
||||
g.Hook("/*", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
||||
g.Group("/hook", func(group *ghttp.RouterGroup) {
|
||||
group.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) {
|
||||
group.Hook("/:name", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
||||
r.Response.Write("hook name")
|
||||
})
|
||||
})
|
||||
|
||||
36
.example/net/ghttp/server/middleware/param.go
Normal file
36
.example/net/ghttp/server/middleware/param.go
Normal file
@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
// 前置中间件1
|
||||
func MiddlewareBefore1(r *ghttp.Request) {
|
||||
r.SetParam("name", "GoFrame")
|
||||
r.Response.Writeln("set name")
|
||||
r.Middleware.Next()
|
||||
}
|
||||
|
||||
// 前置中间件2
|
||||
func MiddlewareBefore2(r *ghttp.Request) {
|
||||
r.SetParam("site", "https://goframe.org")
|
||||
r.Response.Writeln("set site")
|
||||
r.Middleware.Next()
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/", func(group *ghttp.RouterGroup) {
|
||||
group.Middleware(MiddlewareBefore1, MiddlewareBefore2)
|
||||
group.ALL("/", func(r *ghttp.Request) {
|
||||
r.Response.Writefln(
|
||||
"%s: %s",
|
||||
r.GetParamVar("name").String(),
|
||||
r.GetParamVar("site").String(),
|
||||
)
|
||||
})
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
@ -6,7 +6,7 @@ import (
|
||||
|
||||
func main() {
|
||||
s := ghttp.GetServer()
|
||||
s.EnablePprof()
|
||||
s.EnablePProf()
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.Writeln("哈喽世界!")
|
||||
})
|
||||
|
||||
17
.example/net/ghttp/server/request/basic.go
Normal file
17
.example/net/ghttp/server/request/basic.go
Normal file
@ -0,0 +1,17 @@
|
||||
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) {
|
||||
r.Response.Writeln(r.Get("amount"))
|
||||
r.Response.Writeln(r.GetInt("amount"))
|
||||
r.Response.Writeln(r.GetFloat32("amount"))
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
18
.example/net/ghttp/server/request/exit/exit.go
Normal file
18
.example/net/ghttp/server/request/exit/exit.go
Normal file
@ -0,0 +1,18 @@
|
||||
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) {
|
||||
if r.GetInt("type") == 1 {
|
||||
r.Response.Writeln("john")
|
||||
}
|
||||
r.Response.Writeln("smith")
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
15
.example/net/ghttp/server/request/json-xml/test1.go
Normal file
15
.example/net/ghttp/server/request/json-xml/test1.go
Normal file
@ -0,0 +1,15 @@
|
||||
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) {
|
||||
r.Response.Writef("name: %v, pass: %v", r.Get("name"), r.Get("pass"))
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
47
.example/net/ghttp/server/request/json-xml/test2.go
Normal file
47
.example/net/ghttp/server/request/json-xml/test2.go
Normal file
@ -0,0 +1,47 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/util/gvalid"
|
||||
)
|
||||
|
||||
type RegisterReq struct {
|
||||
Name string `p:"username" v:"required|length:6,30#请输入账号|账号长度为:min到:max位"`
|
||||
Pass string `p:"password1" v:"required|length:6,30#请输入密码|密码长度不够"`
|
||||
Pass2 string `p:"password2" v:"required|length:6,30|same:password1#请确认密码|两次密码不一致"`
|
||||
}
|
||||
|
||||
type RegisterRes struct {
|
||||
Code int `json:"code"`
|
||||
Error string `json:"error"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/register", func(r *ghttp.Request) {
|
||||
var req *RegisterReq
|
||||
//fmt.Println(r.GetBody())
|
||||
if err := r.Parse(&req); err != nil {
|
||||
// Validation error.
|
||||
if v, ok := err.(*gvalid.Error); ok {
|
||||
r.Response.WriteJsonExit(RegisterRes{
|
||||
Code: 1,
|
||||
Error: v.FirstString(),
|
||||
})
|
||||
}
|
||||
// Other error.
|
||||
r.Response.WriteJsonExit(RegisterRes{
|
||||
Code: 1,
|
||||
Error: err.Error(),
|
||||
})
|
||||
}
|
||||
// ...
|
||||
r.Response.WriteJsonExit(RegisterRes{
|
||||
Data: req,
|
||||
})
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
15
.example/net/ghttp/server/request/params/array.go
Normal file
15
.example/net/ghttp/server/request/params/array.go
Normal file
@ -0,0 +1,15 @@
|
||||
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) {
|
||||
r.Response.Write(r.Get("array"))
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
15
.example/net/ghttp/server/request/params/map.go
Normal file
15
.example/net/ghttp/server/request/params/map.go
Normal file
@ -0,0 +1,15 @@
|
||||
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) {
|
||||
r.Response.Write(r.Get("map"))
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
15
.example/net/ghttp/server/request/params/repeat.go
Normal file
15
.example/net/ghttp/server/request/params/repeat.go
Normal file
@ -0,0 +1,15 @@
|
||||
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) {
|
||||
r.Response.Write(r.Get("name"))
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
18
.example/net/ghttp/server/request/priority.go
Normal file
18
.example/net/ghttp/server/request/priority.go
Normal file
@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/input", func(r *ghttp.Request) {
|
||||
r.Response.Writeln(r.Get("amount"))
|
||||
})
|
||||
s.BindHandler("/query", func(r *ghttp.Request) {
|
||||
r.Response.Writeln(r.GetQuery("amount"))
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
@ -5,20 +5,20 @@ import (
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
Uid int `json:"uid"`
|
||||
Name string `json:"name" params:"username"`
|
||||
Pass1 string `json:"pass1" params:"password1,userpass1"`
|
||||
Pass2 string `json:"pass2" params:"password3,userpass2"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
type User struct {
|
||||
Uid int `json:"uid"`
|
||||
Name string `json:"name" p:"username"`
|
||||
Pass1 string `json:"pass1" p:"password1"`
|
||||
Pass2 string `json:"pass2" p:"password2"`
|
||||
}
|
||||
|
||||
s := g.Server()
|
||||
s.BindHandler("/user", func(r *ghttp.Request) {
|
||||
user := new(User)
|
||||
r.GetToStruct(user)
|
||||
//r.GetPostToStruct(user)
|
||||
//r.GetQueryToStruct(user)
|
||||
var user *User
|
||||
if err := r.Parse(&user); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
r.Response.WriteJson(user)
|
||||
})
|
||||
s.SetPort(8199)
|
||||
|
||||
@ -6,23 +6,35 @@ import (
|
||||
"github.com/gogf/gf/util/gvalid"
|
||||
)
|
||||
|
||||
func main() {
|
||||
type User struct {
|
||||
Uid int `gvalid:"uid@min:1"`
|
||||
Name string `params:"username" gvalid:"username @required|length:6,30"`
|
||||
Pass1 string `params:"password1" gvalid:"password1@required|password3"`
|
||||
Pass2 string `params:"password2" gvalid:"password2@required|password3|same:password1#||两次密码不一致,请重新输入"`
|
||||
}
|
||||
type User struct {
|
||||
Uid int `gvalid:"uid@min:1"`
|
||||
Name string `params:"username" gvalid:"username @required|length:6,30"`
|
||||
Pass1 string `params:"password1" gvalid:"password1@required|password3"`
|
||||
Pass2 string `params:"password2" gvalid:"password2@required|password3|same:password1#||两次密码不一致,请重新输入"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/user", func(r *ghttp.Request) {
|
||||
user := new(User)
|
||||
r.GetToStruct(user)
|
||||
if err := gvalid.CheckStruct(user, nil); err != nil {
|
||||
r.Response.WriteJson(err.Maps())
|
||||
} else {
|
||||
r.Response.Write("ok")
|
||||
}
|
||||
s.Group("/", func(rgroup *ghttp.RouterGroup) {
|
||||
rgroup.ALL("/user", func(r *ghttp.Request) {
|
||||
user := new(User)
|
||||
if err := r.GetToStruct(user); err != nil {
|
||||
r.Response.WriteJsonExit(g.Map{
|
||||
"message": err,
|
||||
"errcode": 1,
|
||||
})
|
||||
}
|
||||
if err := gvalid.CheckStruct(user, nil); err != nil {
|
||||
r.Response.WriteJsonExit(g.Map{
|
||||
"message": err.Maps(),
|
||||
"errcode": 1,
|
||||
})
|
||||
}
|
||||
r.Response.WriteJsonExit(g.Map{
|
||||
"message": "ok",
|
||||
"errcode": 0,
|
||||
})
|
||||
})
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
|
||||
25
.example/net/ghttp/server/request/struct/parse1.go
Normal file
25
.example/net/ghttp/server/request/struct/parse1.go
Normal file
@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
type User struct {
|
||||
Id int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Pass1 string `json:"password1" p:"password1"`
|
||||
Pass2 string `json:"password2" p:"password2"`
|
||||
}
|
||||
s := g.Server()
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
var user *User
|
||||
if err := r.Parse(&user); err != nil {
|
||||
r.Response.WriteExit(err)
|
||||
}
|
||||
r.Response.WriteExit(user)
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
37
.example/net/ghttp/server/request/struct/parse2.go
Normal file
37
.example/net/ghttp/server/request/struct/parse2.go
Normal file
@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
type RegisterReq struct {
|
||||
Name string
|
||||
Pass string `p:"password1"`
|
||||
Pass2 string `p:"password2"`
|
||||
}
|
||||
|
||||
type RegisterRes struct {
|
||||
Code int `json:"code"`
|
||||
Error string `json:"error"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/register", func(r *ghttp.Request) {
|
||||
var req *RegisterReq
|
||||
if err := r.Parse(&req); err != nil {
|
||||
r.Response.WriteJsonExit(RegisterRes{
|
||||
Code: 1,
|
||||
Error: err.Error(),
|
||||
})
|
||||
}
|
||||
// ...
|
||||
r.Response.WriteJsonExit(RegisterRes{
|
||||
Data: req,
|
||||
})
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
37
.example/net/ghttp/server/request/validation/validation1.go
Normal file
37
.example/net/ghttp/server/request/validation/validation1.go
Normal file
@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
type RegisterReq struct {
|
||||
Name string `p:"username" v:"required|length:6,30#请输入账号|账号长度为:min到:max位"`
|
||||
Pass string `p:"password1" v:"required|length:6,30#请输入密码|密码长度不够"`
|
||||
Pass2 string `p:"password2" v:"required|length:6,30|same:password1#请确认密码|两次密码不一致"`
|
||||
}
|
||||
|
||||
type RegisterRes struct {
|
||||
Code int `json:"code"`
|
||||
Error string `json:"error"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/register", func(r *ghttp.Request) {
|
||||
var req *RegisterReq
|
||||
if err := r.Parse(&req); err != nil {
|
||||
r.Response.WriteJsonExit(RegisterRes{
|
||||
Code: 1,
|
||||
Error: err.Error(),
|
||||
})
|
||||
}
|
||||
// ...
|
||||
r.Response.WriteJsonExit(RegisterRes{
|
||||
Data: req,
|
||||
})
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
46
.example/net/ghttp/server/request/validation/validation2.go
Normal file
46
.example/net/ghttp/server/request/validation/validation2.go
Normal file
@ -0,0 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/util/gvalid"
|
||||
)
|
||||
|
||||
type RegisterReq struct {
|
||||
Name string `p:"username" v:"required|length:6,30#请输入账号|账号长度为:min到:max位"`
|
||||
Pass string `p:"password1" v:"required|length:6,30#请输入密码|密码长度不够"`
|
||||
Pass2 string `p:"password2" v:"required|length:6,30|same:password1#请确认密码|两次密码不一致"`
|
||||
}
|
||||
|
||||
type RegisterRes struct {
|
||||
Code int `json:"code"`
|
||||
Error string `json:"error"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/register", func(r *ghttp.Request) {
|
||||
var req *RegisterReq
|
||||
if err := r.Parse(&req); err != nil {
|
||||
// Validation error.
|
||||
if v, ok := err.(*gvalid.Error); ok {
|
||||
r.Response.WriteJsonExit(RegisterRes{
|
||||
Code: 1,
|
||||
Error: v.FirstString(),
|
||||
})
|
||||
}
|
||||
// Other error.
|
||||
r.Response.WriteJsonExit(RegisterRes{
|
||||
Code: 1,
|
||||
Error: err.Error(),
|
||||
})
|
||||
}
|
||||
// ...
|
||||
r.Response.WriteJsonExit(RegisterRes{
|
||||
Data: req,
|
||||
})
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
@ -12,8 +12,8 @@ import (
|
||||
func main() {
|
||||
gres.Dump()
|
||||
|
||||
v := g.View()
|
||||
v.SetPath("template/layout1")
|
||||
//v := g.View()
|
||||
//v.SetPath("template/layout1")
|
||||
|
||||
s := g.Server()
|
||||
s.SetIndexFolder(true)
|
||||
@ -22,8 +22,8 @@ func main() {
|
||||
fmt.Println(r.URL.Path, r.IsFileRequest())
|
||||
})
|
||||
s.BindHandler("/template", func(r *ghttp.Request) {
|
||||
r.Response.WriteTpl("layout.html")
|
||||
r.Response.WriteTpl("layout1/layout.html")
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.SetPort(8198)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
22
.example/net/ghttp/server/router/group/basic.go
Normal file
22
.example/net/ghttp/server/router/group/basic.go
Normal file
@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
group := s.Group("/api")
|
||||
group.ALL("/all", func(r *ghttp.Request) {
|
||||
r.Response.Write("all")
|
||||
})
|
||||
group.GET("/get", func(r *ghttp.Request) {
|
||||
r.Response.Write("get")
|
||||
})
|
||||
group.POST("/post", func(r *ghttp.Request) {
|
||||
r.Response.Write("post")
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
38
.example/net/ghttp/server/router/group/batch.go
Normal file
38
.example/net/ghttp/server/router/group/batch.go
Normal file
@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
type Object struct{}
|
||||
|
||||
func (o *Object) Show(r *ghttp.Request) {
|
||||
r.Response.Writeln("Show")
|
||||
}
|
||||
|
||||
func (o *Object) Delete(r *ghttp.Request) {
|
||||
r.Response.Writeln("REST Delete")
|
||||
}
|
||||
|
||||
func Handler(r *ghttp.Request) {
|
||||
r.Response.Writeln("Handler")
|
||||
}
|
||||
|
||||
func HookHandler(r *ghttp.Request) {
|
||||
r.Response.Writeln("HOOK Handler")
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
obj := new(Object)
|
||||
s.Group("/api").Bind([]ghttp.GroupItem{
|
||||
{"ALL", "*", HookHandler, ghttp.HOOK_BEFORE_SERVE},
|
||||
{"ALL", "/handler", Handler},
|
||||
{"ALL", "/obj", obj},
|
||||
{"GET", "/obj/show", obj, "Show"},
|
||||
{"REST", "/obj/rest", obj},
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
@ -5,7 +5,6 @@ import (
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/os/glog"
|
||||
)
|
||||
|
||||
func MiddlewareAuth(r *ghttp.Request) {
|
||||
@ -24,43 +23,41 @@ func MiddlewareCORS(r *ghttp.Request) {
|
||||
|
||||
func MiddlewareLog(r *ghttp.Request) {
|
||||
r.Middleware.Next()
|
||||
glog.Println(r.Response.Status, r.URL.Path)
|
||||
g.Log().Println(r.Response.Status, r.URL.Path)
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/", func(g *ghttp.RouterGroup) {
|
||||
g.Middleware(MiddlewareLog)
|
||||
})
|
||||
s.Group("/api.v2", func(g *ghttp.RouterGroup) {
|
||||
g.Middleware(MiddlewareAuth, MiddlewareCORS)
|
||||
g.GET("/test", func(r *ghttp.Request) {
|
||||
s.Use(MiddlewareLog)
|
||||
s.Group("/api.v2", func(group *ghttp.RouterGroup) {
|
||||
group.Middleware(MiddlewareAuth, MiddlewareCORS)
|
||||
group.GET("/test", func(r *ghttp.Request) {
|
||||
r.Response.Write("test")
|
||||
})
|
||||
g.Group("/order", func(g *ghttp.RouterGroup) {
|
||||
g.GET("/list", func(r *ghttp.Request) {
|
||||
group.Group("/order", func(group *ghttp.RouterGroup) {
|
||||
group.GET("/list", func(r *ghttp.Request) {
|
||||
r.Response.Write("list")
|
||||
})
|
||||
g.PUT("/update", func(r *ghttp.Request) {
|
||||
group.PUT("/update", func(r *ghttp.Request) {
|
||||
r.Response.Write("update")
|
||||
})
|
||||
})
|
||||
g.Group("/user", func(g *ghttp.RouterGroup) {
|
||||
g.GET("/info", func(r *ghttp.Request) {
|
||||
group.Group("/user", func(group *ghttp.RouterGroup) {
|
||||
group.GET("/info", func(r *ghttp.Request) {
|
||||
r.Response.Write("info")
|
||||
})
|
||||
g.POST("/edit", func(r *ghttp.Request) {
|
||||
group.POST("/edit", func(r *ghttp.Request) {
|
||||
r.Response.Write("edit")
|
||||
})
|
||||
g.DELETE("/drop", func(r *ghttp.Request) {
|
||||
group.DELETE("/drop", func(r *ghttp.Request) {
|
||||
r.Response.Write("drop")
|
||||
})
|
||||
})
|
||||
g.Group("/hook", func(g *ghttp.RouterGroup) {
|
||||
g.Hook("/*", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
||||
group.Group("/hook", func(group *ghttp.RouterGroup) {
|
||||
group.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) {
|
||||
group.Hook("/:name", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
||||
r.Response.Write("hook name")
|
||||
})
|
||||
})
|
||||
|
||||
@ -1,9 +1,12 @@
|
||||
package main
|
||||
|
||||
import "github.com/gogf/gf/net/ghttp"
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := ghttp.GetServer()
|
||||
s := g.Server()
|
||||
s.BindHandler("/user/:name", func(r *ghttp.Request) {
|
||||
r.Response.Writeln(r.Router.Uri)
|
||||
})
|
||||
|
||||
@ -4,21 +4,21 @@ import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.SetSessionMaxAge(2 * time.Second)
|
||||
s.BindHandler("/set", func(r *ghttp.Request) {
|
||||
r.Session.Set("time", gtime.Second())
|
||||
r.Response.Write("ok")
|
||||
})
|
||||
s.BindHandler("/get", func(r *ghttp.Request) {
|
||||
r.Response.WriteJson(r.Session.Map())
|
||||
})
|
||||
s.BindHandler("/clear", func(r *ghttp.Request) {
|
||||
r.Session.Clear()
|
||||
s.Group("/", func(group *ghttp.RouterGroup) {
|
||||
g.GET("/set", func(r *ghttp.Request) {
|
||||
r.Session.Set("time", gtime.Second())
|
||||
r.Response.Write("ok")
|
||||
})
|
||||
g.GET("/get", func(r *ghttp.Request) {
|
||||
r.Response.WriteJson(r.Session.Map())
|
||||
})
|
||||
g.GET("/clear", func(r *ghttp.Request) {
|
||||
r.Session.Clear()
|
||||
})
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
|
||||
@ -1,25 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.SetSessionMaxAge(60)
|
||||
s.BindHandler("/set", func(r *ghttp.Request) {
|
||||
r.Session.Set("captcha", map[string]string{
|
||||
"key": "value",
|
||||
})
|
||||
r.Response.Write("ok")
|
||||
})
|
||||
s.BindHandler("/get", func(r *ghttp.Request) {
|
||||
fmt.Println(r.Session.Get("captcha"))
|
||||
r.Response.Write(r.Session.Get("captcha"))
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
@ -1,82 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
)
|
||||
|
||||
// 测试,SESSION写入
|
||||
func SessionSet(r *ghttp.Request) {
|
||||
r.Session.Set("time", gtime.Second())
|
||||
r.Response.WriteJson("ok")
|
||||
}
|
||||
|
||||
// 测试,SESSION读取
|
||||
func SessionGet(r *ghttp.Request) {
|
||||
r.Response.WriteJson(r.Session.Map())
|
||||
}
|
||||
|
||||
// 请求处理之前将Redis中的数据读取出来并存储到SESSION对象中。
|
||||
func RedisHandlerGet(r *ghttp.Request) {
|
||||
if !r.IsFileRequest() {
|
||||
id := r.Cookie.GetSessionId()
|
||||
if id == "" {
|
||||
return
|
||||
}
|
||||
// 应用服务器一般是多个节点构成的集群,
|
||||
// 当请求中带有SESSION ID时,自动从Redis读取并恢复数据。
|
||||
value, err := g.Redis().DoVar("GET", id)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if !value.IsNil() {
|
||||
if err := r.Session.Restore(value.Bytes()); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 请求结束时将SESSION数据存储到Redis中,或者在SESSION删除时也删除Redis中的数据。
|
||||
func RedisHandlerSet(r *ghttp.Request) {
|
||||
if !r.IsFileRequest() {
|
||||
id := r.Cookie.GetSessionId()
|
||||
if id == "" {
|
||||
return
|
||||
}
|
||||
err := (error)(nil)
|
||||
value := ([]byte)(nil)
|
||||
if r.Session.Size() > 0 {
|
||||
if value, err = r.Session.Export(); err == nil {
|
||||
if len(value) == 0 {
|
||||
return
|
||||
} else if !r.Session.IsDirty() {
|
||||
// 更新过期时间
|
||||
_, err = g.Redis().Do("EXPIRE", id, r.Server.GetSessionMaxAge())
|
||||
} else {
|
||||
// 更新Redis数据
|
||||
_, err = g.Redis().Do("SETEX", id, r.Server.GetSessionMaxAge(), value)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 清空SESSION后自动删除Redis数据
|
||||
_, err = g.Redis().Do("DEL", id)
|
||||
}
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/set", SessionSet)
|
||||
s.BindHandler("/get", SessionGet)
|
||||
s.BindHookHandlerByMap("/*", map[string]ghttp.HandlerFunc{
|
||||
ghttp.HOOK_BEFORE_SERVE: RedisHandlerGet,
|
||||
ghttp.HOOK_AFTER_SERVE: RedisHandlerSet,
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
@ -7,7 +7,9 @@ func main() {
|
||||
s := g.Server()
|
||||
s.SetIndexFolder(true)
|
||||
s.SetServerRoot("/Users/john/Downloads")
|
||||
s.AddSearchPath("/Users/john/Documents")
|
||||
//s.AddSearchPath("/Users/john/Documents")
|
||||
s.SetErrorLogEnabled(true)
|
||||
s.SetAccessLogEnabled(true)
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user