ghttp.Response.WriteString -> ghttp.Response.Write,REAME updates

This commit is contained in:
John
2018-04-14 12:20:14 +08:00
parent 8eff3fea81
commit 13cc2e900c
26 changed files with 496 additions and 65 deletions

468
README.MD
View File

@ -1,16 +1,11 @@
<div align=center>
<img src="http://cover.kancloud.cn/johng/gf" width="200"/>
<img src="http://cover.kancloud.cn/johng/gf" width="200"/>
</div>
<div align=center>
<a href="https://godoc.org/github.com/johng-cn/gf" target="_blank">
<img src="https://godoc.org/github.com/johng-cn/gf?status.svg"/>
</a>
<!--
<a href="https://www.codetriage.com/johng-cn/gf" target="_blank">
<img src="https://www.codetriage.com/johng-cn/gf/badges/users.svg"/>
</a>
-->
</div>
## 介绍
@ -33,26 +28,463 @@ gf是开源的免费的基于MIT协议进行分发开源项目地址(gi
1. 丰富详尽的框架文档及专业的技术支持/讨论群,易于使用及维护;
1. 更多特点请查阅框架手册( http://gf.johng.cn )
## 安装
```
go get -u gitee.com/johng/gf
```
## 使用
```go
package main
1. **Hello World!**
```go
package main
import "gitee.com/johng/gf/g/net/ghttp"
func main() {
s := ghttp.GetServer()
s.BindHandler("/", func(r *ghttp.Request){
r.Response.Write("Hello World!")
})
s.Run()
}
```
1. **Web Server**
```go
package main
import "gitee.com/johng/gf/g/net/ghttp"
func main() {
s := ghttp.GetServer()
s.SetIndexFolder(true)
s.SetServerRoot("/home/www/")
s.Run()
}
```
1. **多Web Server支持**
```go
package main
import (
"gitee.com/johng/gf/g/net/ghttp"
)
func main() {
s1 := ghttp.GetServer("s1")
s1.SetAddr(":8080")
s1.SetIndexFolder(true)
s1.SetServerRoot("/home/www/static1")
go s1.Run()
s2 := ghttp.GetServer("s2")
s2.SetAddr(":8081")
s2.SetIndexFolder(true)
s2.SetServerRoot("/home/www/static2")
go s2.Run()
select{}
}
```
1. **域名 & 多域名支持**
1. 自定义域名支持:
```go
package main
import "gitee.com/johng/gf/g/net/ghttp"
func main() {
s := ghttp.GetServer()
s.Domain("127.0.0.1").BindHandler("/", func(r *ghttp.Request) {
r.Response.Write("127.0.0.1")
})
s.Domain("localhost").BindHandler("/", func(r *ghttp.Request) {
r.Response.Write("localhost")
})
s.Run()
}
```
1. 多域名绑定支持:
```go
package main
import "gitee.com/johng/gf/g/net/ghttp"
func main() {
s := ghttp.GetServer()
s.Domain("localhost").BindHandler("/", func(r *ghttp.Request) {
r.Response.Write("localhost")
})
s.Domain("localhost1localhost2localhost3").BindHandler("/", func(r *ghttp.Request) {
r.Response.Write("localhostx")
})
s.Run()
}
```
1. **服务注册**
gf框架提供了非常强大的服务注册方式这也是相对于其他框架如gin/beego/httprouter最突出的特点之一。
gf框架提供了三种服务注册方式控制器注册、执行对象注册、回调函数注册具体介绍请查看官方文档([http://gf.johng.cn/494368](http://gf.johng.cn/494368))。服务注册方式比较:
| 注册方式 | 使用难度 | 安全系数 | 执行性能 | 内存消耗 |
| --- | --- | --- | --- | ---|
| 控制器注册 | 低 | 高 | 低 | 高 |
| 执行对象注册 | 中 | 中 | 中 | 中 |
| 回调函数注册 | 高 | 低 | 高 | 低 |
1. **控制器注册**
```go
package main
import (
"gitee.com/johng/gf/g/net/ghttp"
"gitee.com/johng/gf/g/frame/gmvc"
)
// 定义业务相关的控制器对象
type ControllerUser struct {
gmvc.Controller
}
// 定义操作逻辑 - 展示姓名
func (c *ControllerUser) Name() {
c.Response.Write("John")
}
// 定义操作逻辑 - 展示年龄
func (c *ControllerUser) Age() {
c.Response.Write("18")
}
func main() {
// 绑定控制器到指定URI所有控制器的公开方法将会映射到指定URI末尾
// 例如该方法执行后,查看效果可访问:
// http://127.0.0.1:8199/user/name
// http://127.0.0.1:8199/user/age
s := ghttp.GetServer()
s.BindController("/user", &ControllerUser{})
s.SetPort(8199)
s.Run()
}
```
1. **RESTful控制器注册**
支持的HTTP Method: ```GET,PUT,POST,DELETE,PATCH,HEAD,CONNECT,OPTIONS,TRACE```
```go
package main
import (
"gitee.com/johng/gf/g/net/ghttp"
"gitee.com/johng/gf/g/frame/gmvc"
)
// 测试控制器
type ControllerRest struct {
gmvc.Controller
}
// RESTFul - GET
func (c *ControllerRest) Get() {
c.Response.Write("RESTful HTTP Method GET")
}
// RESTFul - POST
func (c *ControllerRest) Post() {
c.Response.Write("RESTful HTTP Method POST")
}
// RESTFul - DELETE
func (c *ControllerRest) Delete() {
c.Response.Write("RESTful HTTP Method DELETE")
}
// 该方法无法映射,将会无法访问到
func (c *ControllerRest) Hello() {
c.Response.Write("Hello")
}
func main() {
// 控制器公开方法中与HTTP Method方法同名的方法将会自动绑定映射
s := ghttp.GetServer()
s.BindControllerRest("/user.rest", &ControllerRest{})
s.SetPort(8199)
s.Run()
}
```
1. **其他注册方式**
由于服务注册方式功能相当丰富,其他注册方式请查看官方开发文档 - 服务注册章节([http://gf.johng.cn/494368](http://gf.johng.cn/494368))。
1. **路由控制**
gf框架提供了自建的非常强大的路由控制功能支持流行的命名匹配规则及模糊匹配规则并提供了优秀的优先级管理机制。
```go
package main
import "gitee.com/johng/gf/g/net/ghttp"
func main() {
s := ghttp.GetServer()
s.BindHandler("/:name", func(r *ghttp.Request){
r.Response.Write(r.GetQueryString("name"))
})
s.BindHandler("/:name/:action", func(r *ghttp.Request){
r.Response.Write(r.GetQueryString("name"))
r.Response.Write(r.GetQueryString("action"))
})
s.BindHandler("/:name/:any", func(r *ghttp.Request){
r.Response.Write(r.GetQueryString("name"))
r.Response.Write(r.GetQueryString("any"))
})
s.SetPort(8199)
s.Run()
}
```
1. **命名匹配规则**
使用```:name```方式进行匹配(```name```为自定义的匹配名称)匹配成功后对应匹配参数会被解析为GET参数并传递给注册的服务使用。
匹配示例1
```shell
rule: /user/:user
/user/john match
/user/you match
/user/john/profile no match
/user/ no match
```
匹配示例2
```shell
rule: /:name/action
/john/name no match
/john/action match
/smith/info no match
/smith/info/age no match
/smith/action match
```
匹配示例3
```shell
rule: /:name/:action
/john/name match
/john/info match
/smith/info match
/smith/info/age no match
/smith/action/del no match
```
1. **模糊匹配规则**
使用```*any```方式进行匹配(```any```为自定义的匹配名称)一般常用语末尾匹配将会匹配URI随后所有的参数并将匹配参数解析为GET参数并传递给注册的服务使用。
匹配示例1
```shell
rule: /src/*path
/src/ match
/src/somefile.go match
/src/subdir/somefile.go match
/user/ no match
/user/john no match
```
匹配示例2
```shell
rule: /src/*path/:action
/src/ no match
/src/somefile.go no match
/src/somefile.go/del match
/src/subdir/file.go/del match
```
匹配示例3
```shell
rule: /src/*path/show
/src/ no match
/src/somefile.go no match
/src/somefile.go/del no match
/src/somefile.go/show match
/src/subdir/file.go/show match
```
1. **路由优先级控制**
优先级控制最主要的是两点因素1、层级越深的规则优先级越高2、命名匹配比模糊匹配优先级高。
我们来看示例(左边的规则优先级比右边高):
```shell
/user/name > /user/:action
/:name/info > /:name/:action
/:name/:action > /:name/*action
/src/path/del > /src/path
/src/path/del > /src/path/:action
/src/path/*any > /src/path
```
1. **数据库ORM**
1. **方法操作**
1. **获取ORM单例对象**
```go
// 获取默认配置的数据库对象(配置名称为"default")
db, err := gdb.Instance()
// 获取配置分组名称为"user-center"的数据库对象
db, err := gdb.Instance("user-center")
```
2. **数据写入**
```go
r, err := db.Insert("user", gdb.Map {
"name": "john",
})
```
3. **数据查询(列表)**
```go
list, err := db.GetAll("select * from user limit 2")
```
4. **数据查询(单条)**
```go
one, err := db.GetOne("select * from user limit 2")
// 或者
one, err := db.GetOne("select * from user where uid=1000")
```
5. **数据保存**
```go
r, err := db.Save("user", gdb.Map {
"uid" : 1,
"name" : "john",
})
```
6. **批量操作**
```go
// BatchInsert/BatchReplace/BatchSave 同理
_, err := db.BatchInsert("user", gdb.List {
{"name": "john_1"},
{"name": "john_2"},
{"name": "john_3"},
{"name": "john_4"},
}, 10)
```
7. **数据更新/删除**
```go
// db.Update/db.Delete 同理
r, err := db.Update("user", gdb.Map {"name": "john"}, "uid=?", 10000)
r, err := db.Update("user", "name='john'", "uid=10000")
r, err := db.Update("user", "name=?", "uid=?", "john", 10000)
```
注意参数域支持并建议使用预处理模式进行输入避免SQL注入风险。
1. **链式操作**
1. **链式查询**
```go
// 查询多条记录并使用Limit分页
r, err := db.Table("user u").LeftJoin("user_detail ud", "u.uid=ud.uid").Fields("u.*, ud.site").Where("u.uid > ?", 1).Limit(0, 10).Select()
// 查询符合条件的单条记录(第一条)
r, err := db.Table("user u").LeftJoin("user_detail ud", "u.uid=ud.uid").Fields("u.*,ud.site").Where("u.uid=?", 1).One()
// 查询字段值
r, err := db.Table("user u").LeftJoin("user_detail ud", "u.uid=ud.uid").Fields("ud.site").Where("u.uid=?", 1).Value()
// 分组及排序
r, err := db.Table("user u").LeftJoin("user_detail ud", "u.uid=ud.uid").Fields("u.*,ud.city").GroupBy("city").OrderBy("register_time asc").Select()
```
2. **链式更新/删除**
```go
// 更新
r, err := db.Table("user").Data(gdb.Map{"name" : "john2"}).Where("name=?", "john").Update()
r, err := db.Table("user").Data("name='john3'").Where("name=?", "john2").Update()
// 删除
r, err := db.Table("user").Where("uid=?", 10).Delete()
```
3. **链式写入/保存**
```go
r, err := db.Table("user").Data(gdb.Map{"name": "john"}).Insert()
r, err := db.Table("user").Data(gdb.Map{"uid": 10000, "name": "john"}).Replace()
r, err := db.Table("user").Data(gdb.Map{"uid": 10001, "name": "john"}).Save()
```
4. **链式批量写入**
```go
r, err := db.Table("user").Data(gdb.List{
{"name": "john_1"},
{"name": "john_2"},
{"name": "john_3"},
{"name": "john_4"},
}).Insert()
```
可以指定批量操作中分批写入数据库的每批次写入条数数量:
```go
r, err := db.Table("user").Data(gdb.List{
{"name": "john_1"},
{"name": "john_2"},
{"name": "john_3"},
{"name": "john_4"},
}).Batch(2).Insert()
```
5. **链式批量保存**
```go
r, err := db.Table("user").Data(gdb.List{
{"uid":10000, "name": "john_1"},
{"uid":10001, "name": "john_2"},
{"uid":10002, "name": "john_3"},
{"uid":10003, "name": "john_4"},
}).Save()
```
1. **事务操作**
开启事务操作可以通过执行```db.Begin```方法,该方法返回事务的操作对象,类型为```*gdb.Tx```,通过该对象执行后续的数据库操作,并可通过```tx.Commit```提交修改,或者通过```tx.Rollback```回滚修改。
1. **开启事务操作**
```go
if tx, err := db.Begin(); err == nil {
fmt.Println("开启事务操作")
}
```
事务操作对象可以执行所有db对象的方法具体请参考[API文档](https://godoc.org/github.com/johng-cn/gf/g/database/gdb)。
2. **事务回滚操作**
```go
if tx, err := db.Begin(); err == nil {
r, err := tx.Save("user", gdb.Map{
"uid" : 1,
"name" : "john",
})
tx.Rollback()
fmt.Println(r, err)
}
```
3. **事务提交操作**
```go
if tx, err := db.Begin(); err == nil {
r, err := tx.Save("user", gdb.Map{
"uid" : 1,
"name" : "john",
})
tx.Commit()
fmt.Println(r, err)
}
```
4. **事务链式操作**
事务操作对象仍然可以通过```tx.Table```或者```tx.From```方法返回一个链式操作的对象,该对象与```db.Table```或者```db.From```方法返回值相同,只不过数据库操作在事务上执行,可提交或回滚。
```go
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)
}
```
其他链式操作请参考上述链式操作章节。
import "gitee.com/johng/gf/g/net/ghttp"
func main() {
s := ghttp.GetServer()
s.BindHandler("/", func(r *ghttp.Request){
r.Response.WriteString("Hello World!")
})
s.Run()
}
```
## 文档
* [框架介绍](http://gf.johng.cn/494364)
* [加入团队](http://gf.johng.cn/512841)

2
TODO
View File

@ -1,3 +1,3 @@
1. gdb Where方法参数的改进研究是否可以将string参数类型修改为interfaceP{
1. gdb Where方法参数的改进研究是否可以将string参数类型修改为interface{}
2. 增加对于数据表Model的封装
3. ghttp.Server请求执行中增加服务退出的方法不再执行后续操作

View File

@ -19,7 +19,7 @@ import (
)
const (
gHTTP_METHODS = "GET,POST,DELETE,PUT,PATCH,HEAD,CONNECT,OPTIONS,TRACE"
gHTTP_METHODS = "GET,PUT,POST,DELETE,PATCH,HEAD,CONNECT,OPTIONS,TRACE"
gDEFAULT_SERVER = "default"
gDEFAULT_DOMAIN = "default"
gDEFAULT_METHOD = "ALL"

View File

@ -266,7 +266,6 @@ func (s *Server) patternToRegRule(rule string) (regrule string, names string) {
names += ","
}
names += v[1:]
return
default:
regrule += "/" + v
}

View File

@ -9,13 +9,13 @@ func init() {
}
func Apple(r *ghttp.Request) {
r.Response.WriteString("Apple")
r.Response.Write("Apple")
}
func Pen(r *ghttp.Request) {
r.Response.WriteString("Pen")
r.Response.Write("Pen")
}
func ApplePen(r *ghttp.Request) {
r.Response.WriteString("Apple-Pen")
r.Response.Write("Apple-Pen")
}

View File

@ -7,6 +7,6 @@ import (
func init() {
ghttp.GetServer().BindHandler("/config", func (r *ghttp.Request) {
r.Response.WriteString(gins.Config().GetString("database.default.0.host"))
r.Response.Write(gins.Config().GetString("database.default.0.host"))
})
}

View File

@ -12,5 +12,5 @@ func init() {
func Cookie(r *ghttp.Request) {
datetime := r.Cookie.Get("datetime")
r.Cookie.Set("datetime", gtime.Datetime())
r.Response.WriteString("datetime:" + datetime)
r.Response.Write("datetime:" + datetime)
}

View File

@ -16,6 +16,6 @@ func init() {
// 用于对象映射
func (d *ControllerDomain) Show(r *ghttp.Request) {
r.Response.WriteString("It's show time bibi!")
r.Response.Write("It's show time bibi!")
}

View File

@ -13,7 +13,7 @@ func Form(r *ghttp.Request) {
}
func FormShow(r *ghttp.Request) {
r.Response.WriteString(`
r.Response.Write(`
<html>
<head>
<title>表单提交</title>

View File

@ -4,6 +4,6 @@ import "gitee.com/johng/gf/g/net/ghttp"
func init() {
ghttp.GetServer().BindHandler("/", func(r *ghttp.Request){
r.Response.WriteString("Hello World!")
r.Response.Write("Hello World!")
})
}

View File

@ -4,18 +4,18 @@ import "gitee.com/johng/gf/g/net/ghttp"
func init() {
ghttp.GetServer().BindHandler("/hook", func(r *ghttp.Request){
r.Response.WriteString("This is hook content!\n")
r.Response.Write("This is hook content!\n")
})
ghttp.GetServer().BindHookHandlerInit("/hook", func(r *ghttp.Request){
r.Response.WriteString("Init hook 1!\n")
r.Response.Write("Init hook 1!\n")
})
ghttp.GetServer().BindHookHandlerInit("/hook", func(r *ghttp.Request){
r.Response.WriteString("Init hook 2!\n")
r.Response.Write("Init hook 2!\n")
})
ghttp.GetServer().BindHookHandlerShut("/hook", func(r *ghttp.Request){
r.Response.WriteString("Shut hook 1!\n")
r.Response.Write("Shut hook 1!\n")
})
ghttp.GetServer().BindHookHandlerShut("/hook", func(r *ghttp.Request){
r.Response.WriteString("Shut hook 2!\n")
r.Response.Write("Shut hook 2!\n")
})
}

View File

@ -14,15 +14,15 @@ func init() {
}
func (c *ControllerMethod) Name() {
c.Response.WriteString("John")
c.Response.Write("John")
}
func (c *ControllerMethod) Age() {
c.Response.WriteString("18")
c.Response.Write("18")
}
func (c *ControllerMethod) Info() {
c.Response.WriteString("Info")
c.Response.Write("Info")
}

View File

@ -9,6 +9,6 @@ func init() {
}
func (o *Object) Show(r *ghttp.Request) {
r.Response.WriteString("It's show time bibi!")
r.Response.Write("It's show time bibi!")
}

View File

@ -13,19 +13,19 @@ func init() {
}
func (o *ObjectMethod) Show1(r *ghttp.Request) {
r.Response.WriteString("show 1")
r.Response.Write("show 1")
}
func (o *ObjectMethod) Show2(r *ghttp.Request) {
r.Response.WriteString("show 2")
r.Response.Write("show 2")
}
func (o *ObjectMethod) Show3(r *ghttp.Request) {
r.Response.WriteString("show 3")
r.Response.Write("show 3")
}
func (o *ObjectMethod) Show4(r *ghttp.Request) {
r.Response.WriteString("show 4")
r.Response.Write("show 4")
}

View File

@ -11,20 +11,20 @@ func init() {
// RESTFul - GET
func (o *ObjectRest) Get(r *ghttp.Request) {
r.Response.WriteString("RESTFul HTTP Method GET")
r.Response.Write("RESTFul HTTP Method GET")
}
// RESTFul - POST
func (c *ObjectRest) Post(r *ghttp.Request) {
r.Response.WriteString("RESTFul HTTP Method POST")
r.Response.Write("RESTFul HTTP Method POST")
}
// RESTFul - DELETE
func (c *ObjectRest) Delete(r *ghttp.Request) {
r.Response.WriteString("RESTFul HTTP Method DELETE")
r.Response.Write("RESTFul HTTP Method DELETE")
}
// 该方法无法映射,将会无法访问到
func (c *ObjectRest) Hello(r *ghttp.Request) {
r.Response.WriteString("Hello")
r.Response.Write("Hello")
}

View File

@ -18,22 +18,22 @@ func init() {
// RESTFul - GET
func (c *ControllerUser) Get() {
c.Response.WriteString("RESTFul HTTP Method GET")
c.Response.Write("RESTFul HTTP Method GET")
}
// RESTFul - POST
func (c *ControllerUser) Post() {
c.Response.WriteString("RESTFul HTTP Method POST")
c.Response.Write("RESTFul HTTP Method POST")
}
// RESTFul - DELETE
func (c *ControllerUser) Delete() {
c.Response.WriteString("RESTFul HTTP Method DELETE")
c.Response.Write("RESTFul HTTP Method DELETE")
}
// 该方法无法映射,将会无法访问到
func (c *ControllerUser) Hello() {
c.Response.WriteString("Hello")
c.Response.Write("Hello")
}

View File

@ -8,5 +8,5 @@ func init() {
}
func List(r *ghttp.Request) {
r.Response.WriteString("list page:" + r.GetQueryString("page"))
r.Response.Write("list page:" + r.GetQueryString("page"))
}

View File

@ -9,9 +9,9 @@ func init() {
}
func List1(r *ghttp.Request) {
r.Response.WriteString("list page:" + r.GetQueryString("page"))
r.Response.Write("list page:" + r.GetQueryString("page"))
}
func List2(r *ghttp.Request) {
r.Response.WriteString("customed list page")
r.Response.Write("customed list page")
}

View File

@ -8,5 +8,5 @@ func init() {
}
func RouterPatch(r *ghttp.Request) {
r.Response.WriteString(`<a href="/list?page=2&ajax=1">page2</a>`)
r.Response.Write(`<a href="/list?page=2&ajax=1">page2</a>`)
}

View File

@ -12,5 +12,5 @@ func init() {
func Session(r *ghttp.Request) {
id := r.Session.GetInt("id")
r.Session.Set("id", id + 1)
r.Response.WriteString("id:" + strconv.Itoa(id))
r.Response.Write("id:" + strconv.Itoa(id))
}

View File

@ -12,14 +12,14 @@ func Upload(r *ghttp.Request) {
buffer := make([]byte, h.Size)
f.Read(buffer)
gfile.PutBinContents("/tmp/" + fname, buffer)
r.Response.WriteString(fname + " uploaded successly")
r.Response.Write(fname + " uploaded successly")
} else {
r.Response.WriteString(e.Error())
r.Response.Write(e.Error())
}
}
func UploadShow(r *ghttp.Request) {
r.Response.WriteString(`
r.Response.Write(`
<html>
<head>
<title>上传文件</title>

View File

@ -22,12 +22,12 @@ func init() {
// 定义操作逻辑 - 展示姓名
func (c *ControllerUser) Name() {
c.Response.WriteString("John")
c.Response.Write("John")
}
// 定义操作逻辑 - 展示年龄
func (c *ControllerUser) Age() {
c.Response.WriteString("18")
c.Response.Write("18")
}

View File

@ -3,11 +3,11 @@ package main
import "gitee.com/johng/gf/g/net/ghttp"
func Hello1(r *ghttp.Request) {
r.Response.WriteString("Hello World1!")
r.Response.Write("Hello World1!")
}
func Hello2(r *ghttp.Request) {
r.Response.WriteString("Hello World2!")
r.Response.Write("Hello World2!")
}
func main() {

View File

@ -18,7 +18,7 @@ func main() {
"AfterClose" : func(r *ghttp.Request){ fmt.Println("AfterClose") },
})
ghttp.GetServer().BindHandler(pattern, func(r *ghttp.Request) {
r.Response.WriteString("Hello World!")
r.Response.Write("Hello World!")
})
ghttp.GetServer().SetPort(10000)
ghttp.GetServer().Run()

View File

@ -17,7 +17,7 @@ func main() {
r.Response.Write(r.GetQueryString("any"))
})
//ghttp.GetServer().BindHandler("/:name/action", func(r *ghttp.Request) {
// r.Response.WriteString(r.GetQueryString("name"))
// r.Response.Write(r.GetQueryString("name"))
//})
ghttp.GetServer().BindHandler("/:name/:action/:aaa", func(r *ghttp.Request) {
r.Response.Write("name")

View File

@ -4,7 +4,7 @@ import "gitee.com/johng/gf/g/net/ghttp"
func main () {
ghttp.GetServer().BindHandler("/router/*name", func(r *ghttp.Request) {
r.Response.WriteString(r.GetQueryString("name"))
r.Response.Write(r.GetQueryString("name"))
})
ghttp.GetServer().SetPort(10000)
ghttp.GetServer().Run()