Compare commits

..

8 Commits

Author SHA1 Message Date
6c7e536eeb fix issue in gbase64/ghttp 2019-09-24 20:39:00 +08:00
36c2648be8 improve ghttp.Response; move gflock to new repo 2019-09-24 20:19:18 +08:00
a876b6133d fix issue in unit test case og grand 2019-09-23 23:24:36 +08:00
03ff358da8 update ci/readme/donator 2019-09-23 23:08:58 +08:00
037f74c549 Create FUNDING.yml 2019-09-23 22:32:31 +08:00
a8caf4ad21 RELEASE updates 2019-09-23 22:00:04 +08:00
51d9fe5253 add AllowDomain option for ghttp.CORS; add IsSubDomain function for gstr; improve grand.Intn 2019-09-23 19:25:03 +08:00
3218c89f17 improve gfile/gstr/ghttp 2019-09-23 16:21:19 +08:00
44 changed files with 575 additions and 643 deletions

View File

@ -7,7 +7,7 @@ import (
func main() {
s := g.Server()
s.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_FULLNAME)
s.SetNameToUriType(ghttp.URI_TYPE_FULLNAME)
s.EnableAdmin()
s.BindHandler("/", func(r *ghttp.Request) {
r.Response.Write("hello world")

View File

@ -0,0 +1,25 @@
package main
import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp"
)
func MiddlewareCORS(r *ghttp.Request) {
r.Response.CORSDefault()
r.Middleware.Next()
}
func Order(r *ghttp.Request) {
r.Response.Write("GET")
}
func main() {
s := g.Server()
s.Group("/api.v1", func(g *ghttp.RouterGroup) {
g.Middleware(MiddlewareCORS)
g.GET("/order", Order)
})
s.SetPort(8199)
s.Run()
}

View File

@ -0,0 +1,27 @@
package main
import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp"
)
func MiddlewareCORS(r *ghttp.Request) {
corsOptions := r.Response.DefaultCORSOptions()
corsOptions.AllowDomain = []string{"goframe.org", "baidu.com"}
r.Response.CORS(corsOptions)
r.Middleware.Next()
}
func Order(r *ghttp.Request) {
r.Response.Write("GET")
}
func main() {
s := g.Server()
s.Group("/api.v1", func(g *ghttp.RouterGroup) {
g.Middleware(MiddlewareCORS)
g.GET("/order", Order)
})
s.SetPort(8199)
s.Run()
}

View File

@ -0,0 +1,33 @@
package main
import (
"net/http"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp"
)
func MiddlewareCORS(r *ghttp.Request) {
corsOptions := r.Response.DefaultCORSOptions()
corsOptions.AllowDomain = []string{"goframe.org"}
if !r.Response.CORSAllowedOrigin(corsOptions) {
r.Response.WriteStatus(http.StatusForbidden)
return
}
r.Response.CORS(corsOptions)
r.Middleware.Next()
}
func Order(r *ghttp.Request) {
r.Response.Write("GET")
}
func main() {
s := g.Server()
s.Group("/api.v1", func(g *ghttp.RouterGroup) {
g.Middleware(MiddlewareCORS)
g.GET("/order", Order)
})
s.SetPort(8199)
s.Run()
}

View File

@ -17,10 +17,10 @@ func main() {
s3 := g.Server(3)
s4 := g.Server(4)
s1.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_DEFAULT)
s2.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_FULLNAME)
s3.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_ALLLOWER)
s4.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_CAMEL)
s1.SetNameToUriType(ghttp.URI_TYPE_DEFAULT)
s2.SetNameToUriType(ghttp.URI_TYPE_FULLNAME)
s3.SetNameToUriType(ghttp.URI_TYPE_ALLLOWER)
s4.SetNameToUriType(ghttp.URI_TYPE_CAMEL)
s1.BindObject("/{.struct}/{.method}", new(User))
s2.BindObject("/{.struct}/{.method}", new(User))

View File

@ -9,7 +9,7 @@ import (
func main() {
s := g.Server()
s.SetServerRoot("public")
s.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_ALLLOWER)
s.SetNameToUriType(ghttp.URI_TYPE_ALLLOWER)
s.SetErrorLogEnabled(true)
s.SetAccessLogEnabled(true)
s.SetPort(2333)

View File

@ -1,12 +1,48 @@
package main
import (
"github.com/gogf/gf/os/gcmd"
"github.com/gogf/gf/os/glog"
"fmt"
"github.com/gogf/gf/os/gfile"
"github.com/gogf/gf/text/gregex"
"github.com/gogf/gf/debug/gdebug"
)
func main() {
glog.SetFlags(glog.F_TIME_DATE | glog.F_TIME_TIME | glog.F_FILE_SHORT)
glog.Debug("dd")
glog.Println("timeout", gcmd.GetOpt("timeout"))
cdnUrl := "http://localhost"
content := `
<link rel="stylesheet" href="/plugin/amazeui-2.7.2/css/amazeui.min.css">
<link rel="stylesheet" href="/plugin/markdown-css/github-markdown.min.js">
<link rel="stylesheet" href="/plugin/prism/prism.css">
<link rel="stylesheet" href="/resource/css/document/style.css">
<link rel="icon" href="/resource/image/favicon.ico" type="image/x-icon">
`
s, err := gregex.ReplaceStringFuncMatch(`(href|src)=['"](.+?)['"]`, content, func(match []string) string {
link := match[2]
if len(link) == 0 {
return match[0]
}
if link[0:1] != "/" && link[0:1] != "#" {
if len(link) > 10 && link[0:10] == "javascript" {
return match[0]
}
if len(link) > 7 && link[0:7] == "mailto:" {
return match[0]
}
if len(link) > 4 && link[0:4] == "http" {
return match[0]
}
link = "/" + link
}
if link[0:1] == "/" {
switch gfile.ExtName(link) {
case "png", "jpg", "jpeg", "gif", "js", "css", "otf", "eot", "ttf", "woff", "woff2":
return fmt.Sprintf(`%s="%s%s?%s"`, match[1], cdnUrl, link, gdebug.BinVersion())
}
}
return match[0]
})
fmt.Println(err)
fmt.Println(s)
}

12
.github/FUNDING.yml vendored Normal file
View File

@ -0,0 +1,12 @@
# These are supported funding model platforms
github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
custom: https://github.com/gogf/gf#donators

1
.gitignore vendored
View File

@ -14,3 +14,4 @@ bin/
cbuild
**/.DS_Store
.vscode/
go.sum

View File

@ -13,6 +13,7 @@
|[zfan_codes](https://gitee.com/zfan_codes)|gitee|¥10.00
|[arden](https://github.com/arden)|alipay|¥10.00
|[macnie](https://www.macnie.com)|wechat|¥100.00
|lah|wechat|¥100.00
|x*z|wechat|¥20.00
|潘兄|wechat|¥100.00
|Fly的狐狸|wechat|¥100.00

191
README.MD
View File

@ -38,7 +38,6 @@ golang version >= 1.10
# Quick Start
## Hello World
```go
package main
@ -55,196 +54,6 @@ func main() {
s.Run()
}
```
## Router & Middleware
```go
package main
import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp"
)
func main() {
s := g.Server()
s.Group("/api.v2", func(g *ghttp.RouterGroup) {
g.Middleware(func(r *ghttp.Request) {
r.Response.Write("start")
r.Middleware.Next()
r.Response.Write("end")
})
g.Group("/order", func(g *ghttp.RouterGroup) {
g.GET("/list", func(r *ghttp.Request) {
r.Response.Write("list")
})
})
g.Group("/user", func(g *ghttp.RouterGroup) {
g.GET("/info", func(r *ghttp.Request) {
r.Response.Write("info")
})
g.POST("/edit", func(r *ghttp.Request) {
r.Response.Write("edit")
})
})
g.Group("/hook", func(g *ghttp.RouterGroup) {
g.Hook("/*", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
r.Response.Write("hook any")
})
g.Hook("/:name", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
r.Response.Write("hook name")
})
})
})
s.SetPort(8199)
s.Run()
}
```
## Multi ports & domains
```go
package main
import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp"
)
func Hello1(r *ghttp.Request) {
r.Response.Write("127.0.0.1: Hello1!")
}
func Hello2(r *ghttp.Request) {
r.Response.Write("localhost: Hello2!")
}
func main() {
s := g.Server()
s.Domain("127.0.0.1").BindHandler("/", Hello1)
s.Domain("localhost").BindHandler("/", Hello2)
s.SetPort(8100, 8200, 8300)
s.Run()
}
```
## Template Engine
```go
package main
import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp"
)
func main() {
s := g.Server()
s.BindHandler("/template", func(r *ghttp.Request) {
r.Response.WriteTpl("index.tpl", g.Map{
"id": 123,
"name": "john",
})
})
s.SetPort(8199)
s.Run()
}
```
## File Uploading
```go
func Upload(r *ghttp.Request) {
if f, h, e := r.FormFile("upload-file"); e == nil {
defer f.Close()
name := gfile.Basename(h.Filename)
buffer := make([]byte, h.Size)
f.Read(buffer)
gfile.PutBytes("/tmp/" + name, buffer)
r.Response.Write(name + " uploaded successly")
} else {
r.Response.Write(e.Error())
}
}
```
## ORM Operations
### 1. Retrieving instance
```go
db := g.DB()
db := g.DB("user-center")
```
### 2. Chaining Operations
`Where + string`
```go
// SELECT * FROM user WHERE uid>1 LIMIT 0,10
r, err := db.Table("user").Where("uid > ?", 1).Limit(0, 10).Select()
// SELECT uid,name FROM user WHERE uid>1 LIMIT 0,10
r, err := db.Table("user").Fileds("uid,name").Where("uid > ?", 1).Limit(0, 10).Select()
// SELECT * FROM user WHERE uid=1
r, err := db.Table("user").Where("u.uid=1",).One()
r, err := db.Table("user").Where("u.uid", 1).One()
r, err := db.Table("user").Where("u.uid=?", 1).One()
// SELECT * FROM user WHERE (uid=1) AND (name='john')
r, err := db.Table("user").Where("uid", 1).Where("name", "john").One()
r, err := db.Table("user").Where("uid=?", 1).And("name=?", "john").One()
// SELECT * FROM user WHERE (uid=1) OR (name='john')
r, err := db.Table("user").Where("uid=?", 1).Or("name=?", "john").One()
```
`Where + map`
```go
// SELECT * FROM user WHERE uid=1 AND name='john'
r, err := db.Table("user").Where(g.Map{"uid" : 1, "name" : "john"}).One()
// SELECT * FROM user WHERE uid=1 AND age>18
r, err := db.Table("user").Where(g.Map{"uid" : 1, "age>" : 18}).One()
```
`Where + struct/*struct`
```go
type User struct {
Id int `json:"uid"`
UserName string `gconv:"name"`
}
// SELECT * FROM user WHERE uid =1 AND name='john'
r, err := db.Table("user").Where(User{ Id : 1, UserName : "john"}).One()
// SELECT * FROM user WHERE uid =1
r, err := db.Table("user").Where(&User{ Id : 1}).One()
```
### 3. Update & Delete
```go
// UPDATE user SET name='john guo' WHERE name='john'
r, err := db.Table("user").Data(gdb.Map{"name" : "john guo"}).Where("name=?", "john").Update()
r, err := db.Table("user").Data("name='john guo'").Where("name=?", "john").Update()
// UPDATE user SET status=1 ORDER BY login_time asc LIMIT 10
r, err := db.Table("user").Data("status", 1).OrderBy("login_time asc").Limit(10).Update
// DELETE FROM user WHERE uid=10
r, err := db.Table("user").Where("uid=?", 10).Delete()
// DELETE FROM user ORDER BY login_time asc LIMIT 10
r, err := db.Table("user").OrderBy("login_time asc").Limit(10).Delete()
```
### 4. Insert & Replace & Save
```go
r, err := db.Table("user").Data(g.Map{"name": "john"}).Insert()
r, err := db.Table("user").Data(g.Map{"uid": 10000, "name": "john"}).Replace()
r, err := db.Table("user").Data(g.Map{"uid": 10001, "name": "john"}).Save()
```
### 5. Transaction
```go
if tx, err := db.Begin(); err == nil {
r, err := tx.Save("user", g.Map{
"uid" : 1,
"name" : "john",
})
tx.Commit()
}
```
### 6. Error Handling
```go
func GetOrderInfo(id int) (order *Order, err error) {
err = g.DB().Table("order").Where("id", id).Struct(&order)
if err != nil && err == sql.ErrNoRows {
err = nil
}
return
}
```
[More Features...](https://goframe.org/start/index)

View File

@ -1,6 +1,6 @@
# `v1.9.0`
# `v1.9.3`
该版本实际为`v2.0.0`的大版本发布,为避免`go module`机制严格要求`v2`版本以上需要修改`import`并加上`v2`后缀,因此使用了`v1.9.0`进行发布。
该版本实际为`v2.0`的大版本发布,为避免`go module`机制严格要求`v2`版本以上需要修改`import`并加上`v2`后缀,因此使用了`v1.9`版本进行发布。
## 新特性
@ -29,6 +29,7 @@
1. `ghttp`
- 改进`Request`参数解析方式https://goframe.org/net/ghttp/request
- 改进跨域请求功能,新增`Origin`设置及校验功能https://goframe.org/net/ghttp/cors
- `Cookie`及`Session`的`TTL`配置数据类型修改为`time.Duration`;
- 新增允许同时通过`Header/Cookie`传递`SessionId`
- 新增`ConfigFromMap/SetConfigWithMap`方法,支持通过`map`参数设置WebServer

View File

@ -220,11 +220,63 @@ func (v *Var) Map(tags ...string) map[string]interface{} {
return gconv.Map(v.Val(), tags...)
}
// MapStrStr converts <v> to map[string]string.
func (v *Var) MapStrStr(tags ...string) map[string]string {
m := v.Map(tags...)
if len(m) > 0 {
vMap := make(map[string]string)
for k, v := range m {
vMap[k] = gconv.String(v)
}
return vMap
}
return nil
}
// MapStrVar converts <v> to map[string]*Var.
func (v *Var) MapStrVar(tags ...string) map[string]*Var {
m := v.Map(tags...)
if len(m) > 0 {
vMap := make(map[string]*Var)
for k, v := range m {
vMap[k] = New(v)
}
return vMap
}
return nil
}
// MapDeep converts <v> to map[string]interface{} recursively.
func (v *Var) MapDeep(tags ...string) map[string]interface{} {
return gconv.MapDeep(v.Val(), tags...)
}
// MapDeep converts <v> to map[string]string recursively.
func (v *Var) MapStrStrDeep(tags ...string) map[string]string {
m := v.MapDeep(tags...)
if len(m) > 0 {
vMap := make(map[string]string)
for k, v := range m {
vMap[k] = gconv.String(v)
}
return vMap
}
return nil
}
// MapStrVarDeep converts <v> to map[string]*Var recursively.
func (v *Var) MapStrVarDeep(tags ...string) map[string]*Var {
m := v.MapDeep(tags...)
if len(m) > 0 {
vMap := make(map[string]*Var)
for k, v := range m {
vMap[k] = New(v)
}
return vMap
}
return nil
}
// Struct maps value of <v> to <pointer>.
// The parameter <pointer> should be a pointer to a struct instance.
// The parameter <mapping> is used to specify the key-to-attribute mapping rules.

View File

@ -12,7 +12,13 @@ import (
"fmt"
"path/filepath"
"runtime"
"strconv"
"strings"
"github.com/gogf/gf/encoding/ghash"
"github.com/gogf/gf/crypto/gmd5"
"github.com/gogf/gf/os/gfile"
)
const (
@ -21,8 +27,9 @@ const (
)
var (
// goRootForFilter is used for stack filtering purpose.
goRootForFilter = runtime.GOROOT()
goRootForFilter = runtime.GOROOT() // goRootForFilter is used for stack filtering purpose.
binaryVersion = "" // The version of current running binary(uint64 hex).
binaryVersionMd5 = "" // The version of current running binary(MD5).
)
func init() {
@ -31,6 +38,27 @@ func init() {
}
}
// BinVersion returns the version of current running binary.
// It uses ghash.BKDRHash+BASE36 algorithm to calculate the unique version of the binary.
func BinVersion() string {
if binaryVersion == "" {
binaryVersion = strconv.FormatInt(
int64(ghash.BKDRHash(gfile.GetBytes(gfile.SelfPath()))),
36,
)
}
return binaryVersion
}
// BinVersionMd5 returns the version of current running binary.
// It uses MD5 algorithm to calculate the unique version of the binary.
func BinVersionMd5() string {
if binaryVersionMd5 == "" {
binaryVersionMd5, _ = gmd5.EncryptFile(gfile.SelfPath())
}
return binaryVersionMd5
}
// PrintStack prints to standard error the stack trace returned by runtime.Stack.
func PrintStack(skip ...int) {
fmt.Print(Stack(skip...))

View File

@ -25,8 +25,13 @@ func Decode(dst []byte) ([]byte, error) {
return src[:n], err
}
// EncodeString encodes bytes with BASE64 algorithm.
func EncodeString(src []byte) string {
// EncodeString encodes string with BASE64 algorithm.
func EncodeString(src string) string {
return EncodeToString([]byte(src))
}
// EncodeToString encodes bytes to string with BASE64 algorithm.
func EncodeToString(src []byte) string {
return string(Encode(src))
}
@ -34,3 +39,9 @@ func EncodeString(src []byte) string {
func DecodeString(str string) ([]byte, error) {
return Decode([]byte(str))
}
// DecodeString decodes string with BASE64 algorithm.
func DecodeToString(str string) (string, error) {
b, err := DecodeString(str)
return string(b), err
}

View File

@ -12,11 +12,11 @@ import (
"github.com/gogf/gf/test/gtest"
)
type testpair struct {
type testPair struct {
decoded, encoded string
}
var pairs = []testpair{
var pairs = []testPair{
// RFC 3548 examples
{"\x14\xfb\x9c\x03\xd9\x7e", "FPucA9l+"},
{"\x14\xfb\x9c\x03\xd9", "FPucA9k="},
@ -45,15 +45,20 @@ var pairs = []testpair{
func TestBase64(t *testing.T) {
gtest.Case(t, func() {
for k := range pairs {
// []byte
// Encode
gtest.Assert(gbase64.Encode([]byte(pairs[k].decoded)), []byte(pairs[k].encoded))
e1, _ := gbase64.Decode([]byte(pairs[k].encoded))
gtest.Assert(e1, []byte(pairs[k].decoded))
gtest.Assert(gbase64.EncodeToString([]byte(pairs[k].decoded)), pairs[k].encoded)
gtest.Assert(gbase64.EncodeString(pairs[k].decoded), pairs[k].encoded)
// string
gtest.Assert(gbase64.EncodeString([]byte(pairs[k].decoded)), pairs[k].encoded)
e2, _ := gbase64.DecodeString(pairs[k].encoded)
gtest.Assert(e2, []byte(pairs[k].decoded))
// Decode
r1, _ := gbase64.Decode([]byte(pairs[k].encoded))
gtest.Assert(r1, []byte(pairs[k].decoded))
r2, _ := gbase64.DecodeString(pairs[k].encoded)
gtest.Assert(r2, []byte(pairs[k].decoded))
r3, _ := gbase64.DecodeToString(pairs[k].encoded)
gtest.Assert(r3, pairs[k].decoded)
}
})
}

View File

@ -12,6 +12,23 @@ import (
"github.com/gogf/gf/encoding/gjson"
)
var (
jsonStr1 = `[1,2,3]`
jsonStr2 = `{"CallbackCommand":"Group.CallbackAfterSendMsg","From_Account":"61934946","GroupId":"@TGS#2FLGX67FD","MsgBody":[{"MsgContent":{"Text":"是的"},"MsgType":"TIMTextElem"}],"MsgSeq":23,"MsgTime":1567032819,"Operator_Account":"61934946","Random":2804799576,"Type":"Public"}`
)
func Benchmark_Validate1(b *testing.B) {
for i := 0; i < b.N; i++ {
gjson.Valid(jsonStr1)
}
}
func Benchmark_Validate2(b *testing.B) {
for i := 0; i < b.N; i++ {
gjson.Valid(jsonStr2)
}
}
func Benchmark_Set1(b *testing.B) {
for i := 0; i < b.N; i++ {
p := gjson.New(map[string]string{

View File

@ -10,25 +10,24 @@ package gyaml
import (
"encoding/json"
"github.com/gf-third/yaml"
"github.com/gogf/gf/util/gconv"
yaml3 "github.com/gf-third/yaml/v3"
)
func Encode(v interface{}) ([]byte, error) {
return yaml3.Marshal(v)
return yaml.Marshal(v)
}
func Decode(v []byte) (interface{}, error) {
var result map[string]interface{}
if err := yaml3.Unmarshal(v, &result); err != nil {
if err := yaml.Unmarshal(v, &result); err != nil {
return nil, err
}
return gconv.MapDeep(result), nil
}
func DecodeTo(v []byte, result interface{}) error {
return yaml3.Unmarshal(v, result)
return yaml.Unmarshal(v, result)
}
func ToJson(v []byte) ([]byte, error) {

View File

@ -6,9 +6,11 @@
package g
import "github.com/gogf/gf/container/gvar"
import (
"github.com/gogf/gf/container/gvar"
)
// Universal variable type, like generics.
// Var is a universal variable type, like generics.
type Var = gvar.Var
// Frequently-used map type alias.

11
go.mod
View File

@ -6,16 +6,13 @@ require (
github.com/fatih/structs v1.1.0
github.com/fsnotify/fsnotify v1.4.7
github.com/gf-third/mysql v1.4.2
github.com/gf-third/yaml/v3 v3.0.0
github.com/gofrs/flock v0.7.1 // indirect
github.com/gf-third/yaml v1.0.1
github.com/gomodule/redigo v2.0.0+incompatible
github.com/gorilla/websocket v1.4.1
github.com/grokify/html-strip-tags-go v0.0.0-20190916062342-6f856a90d556
github.com/grokify/html-strip-tags-go v0.0.0-20190921062105-daaa06bf1aaf
github.com/mattn/go-runewidth v0.0.4 // indirect
github.com/olekukonko/tablewriter v0.0.1
github.com/theckman/go-flock v0.7.1
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3 // indirect
golang.org/x/sys v0.0.0-20190924092210-98129a5cf4a0 // indirect
golang.org/x/text v0.3.2
google.golang.org/appengine v1.6.2 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
google.golang.org/appengine v1.6.3 // indirect
)

44
go.sum
View File

@ -1,44 +0,0 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/clbanning/mxj v1.8.4 h1:HuhwZtbyvyOw+3Z1AowPkU87JkJUSv751ELWaiTpj8I=
github.com/clbanning/mxj v1.8.4/go.mod h1:BVjHeAH+rl9rs6f+QIpeRl0tfu10SXn1pUSa5PVGJng=
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/gf-third/mysql v1.4.2 h1:f1M5CNFUG3WkE07UOomtu4o0n/KJKeuUUf5Nc9ZFXs4=
github.com/gf-third/mysql v1.4.2/go.mod h1:+dd90V663ppI2fV5uQ6+rHk0u8KCyU6FkG8Um8Cx3ms=
github.com/gf-third/yaml/v3 v3.0.0 h1:IMLH3JWFpNraTz5d6jzaNZFvWaVYAwhP9w1lhk9SFUs=
github.com/gf-third/yaml/v3 v3.0.0/go.mod h1:En6jd9ZtAhuCiVfRnTo8NYVgbiZvg/F26r8Tj+vsUy4=
github.com/gofrs/flock v0.7.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/gomodule/redigo v2.0.0+incompatible h1:K/R+8tc58AaqLkqG2Ol3Qk+DR/TlNuhuh457pBFPtt0=
github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grokify/html-strip-tags-go v0.0.0-20190916062342-6f856a90d556 h1:RS7ewakEriTcISIy+USQV9tE37SpWCblCiPE6QWxagk=
github.com/grokify/html-strip-tags-go v0.0.0-20190916062342-6f856a90d556/go.mod h1:Xk7G0nwBiIloTMbLddk4WWJOqi4i/JLhadLd0HUXO30=
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/olekukonko/tablewriter v0.0.1 h1:b3iUnf1v+ppJiOfNX4yxxqfWKMQPZR5yoh8urCTFX88=
github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
github.com/theckman/go-flock v0.7.1/go.mod h1:kjuth3y9VJ2aNlkNEO99G/8lp9fMIKaGyBmh84IBheM=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3 h1:7TYNF4UdlohbFwpNH04CoPMp1cHUZgO1Ebq5r2hIjfo=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
google.golang.org/appengine v1.6.2/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

View File

@ -65,7 +65,7 @@ func newRequest(s *Server, r *http.Request, w http.ResponseWriter) *Request {
// 会话处理
request.Cookie = GetCookie(request)
request.Session = s.sessionManager.New(request.GetSessionId())
request.Response.request = request
request.Response.Request = request
request.Middleware = &Middleware{
request: request,
}
@ -74,7 +74,7 @@ func newRequest(s *Server, r *http.Request, w http.ResponseWriter) *Request {
// 获取Web Socket连接对象(如果是非WS请求会失败注意检查返回的error结果)
func (r *Request) WebSocket() (*WebSocket, error) {
if conn, err := wsUpgrader.Upgrade(r.Response.ResponseWriter.ResponseWriter, r.Request, nil); err == nil {
if conn, err := wsUpgrader.Upgrade(r.Response.Writer, r.Request, nil); err == nil {
return &WebSocket{
conn,
}, nil
@ -108,8 +108,8 @@ func (r *Request) GetRawString() string {
}
// 获取原始json请求输入字符串并解析为json对象
func (r *Request) GetJson() *gjson.Json {
return gjson.New(r.GetRaw())
func (r *Request) GetJson() (*gjson.Json, error) {
return gjson.LoadJson(r.GetRaw())
}
func (r *Request) GetString(key string, def ...interface{}) string {

View File

@ -157,6 +157,18 @@ func (r *Request) GetPostMapStrStr(kvMap ...map[string]interface{}) map[string]s
return nil
}
func (r *Request) GetPostMapStrVar(kvMap ...map[string]interface{}) map[string]*gvar.Var {
postMap := r.GetPostMap(kvMap...)
if len(postMap) > 0 {
m := make(map[string]*gvar.Var)
for k, v := range postMap {
m[k] = gvar.New(v)
}
return m
}
return nil
}
// 将所有的request参数映射到struct属性上参数object应当为一个struct对象的指针, mapping为非必需参数自定义参数与属性的映射关系
func (r *Request) GetPostToStruct(pointer interface{}, mapping ...map[string]string) error {
r.initPost()

View File

@ -129,6 +129,18 @@ func (r *Request) GetQueryMapStrStr(kvMap ...map[string]interface{}) map[string]
return nil
}
func (r *Request) GetQueryMapStrVar(kvMap ...map[string]interface{}) map[string]*gvar.Var {
queryMap := r.GetQueryMap(kvMap...)
if len(queryMap) > 0 {
m := make(map[string]*gvar.Var)
for k, v := range queryMap {
m[k] = gvar.New(v)
}
return m
}
return nil
}
// 将所有的get参数映射到struct属性上参数object应当为一个struct对象的指针, mapping为非必需参数自定义参数与属性的映射关系
func (r *Request) GetQueryToStruct(pointer interface{}, mapping ...map[string]string) error {
r.initGet()

View File

@ -130,6 +130,18 @@ func (r *Request) GetRequestMapStrStr(kvMap ...map[string]interface{}) map[strin
return nil
}
func (r *Request) GetRequestMapStrVar(kvMap ...map[string]interface{}) map[string]*gvar.Var {
requestMap := r.GetRequestMap(kvMap...)
if len(requestMap) > 0 {
m := make(map[string]*gvar.Var)
for k, v := range requestMap {
m[k] = gvar.New(v)
}
return m
}
return nil
}
// 将所有的request参数映射到struct属性上参数object应当为一个struct对象的指针, mapping为非必需参数自定义参数与属性的映射关系
func (r *Request) GetRequestToStruct(pointer interface{}, mapping ...map[string]string) error {
tagMap := structs.TagMapName(pointer, paramTagPriority, true)

View File

@ -23,22 +23,22 @@ import (
// 服务端请求返回对象。
// 注意该对象并没有实现http.ResponseWriter接口而是依靠ghttp.ResponseWriter实现。
type Response struct {
ResponseWriter
Server *Server // 所属Web Server
Writer *ResponseWriter // ResponseWriter的别名
request *Request // 关联的Request请求对象
*ResponseWriter // Underlying ResponseWriter.
Server *Server // Parent server.
Writer *ResponseWriter // Alias of ResponseWriter.
Request *Request // According request.
}
// 创建一个ghttp.Response对象指针
func newResponse(s *Server, w http.ResponseWriter) *Response {
r := &Response{
Server: s,
ResponseWriter: ResponseWriter{
ResponseWriter: w,
buffer: bytes.NewBuffer(nil),
ResponseWriter: &ResponseWriter{
writer: w,
buffer: bytes.NewBuffer(nil),
},
}
r.Writer = &r.ResponseWriter
r.Writer = r.ResponseWriter
return r
}
@ -47,7 +47,7 @@ func (r *Response) Write(content ...interface{}) {
if len(content) == 0 {
return
}
if r.Status == 0 && r.request.hasServeHandler {
if r.Status == 0 && r.Request.hasServeHandler {
r.Status = http.StatusOK
}
for _, v := range content {
@ -99,7 +99,7 @@ func (r *Response) WriteJsonP(content interface{}) error {
return err
} else {
//r.Header().Set("Content-Type", "application/json")
if callback := r.request.GetString("callback"); callback != "" {
if callback := r.Request.GetString("callback"); callback != "" {
buffer := []byte(callback)
buffer = append(buffer, byte('('))
buffer = append(buffer, b...)
@ -128,9 +128,9 @@ func (r *Response) WriteStatus(status int, content ...interface{}) {
if r.buffer.Len() == 0 {
// 状态码注册回调函数处理
if status != http.StatusOK {
if f := r.request.Server.getStatusHandler(status, r.request); f != nil {
if f := r.Request.Server.getStatusHandler(status, r.Request); f != nil {
niceCallFunc(func() {
f(r.request)
f(r.Request)
})
// 防止多次设置(http: multiple response.WriteHeader calls)
if r.Status == 0 {
@ -168,7 +168,7 @@ func (r *Response) ServeFile(path string, allowIndex ...bool) {
}
serveFile = &staticServeFile{path: path}
}
r.Server.serveFile(r.request, serveFile, allowIndex...)
r.Server.serveFile(r.Request, serveFile, allowIndex...)
}
// 静态文件下载处理
@ -200,7 +200,7 @@ func (r *Response) ServeFileDownload(path string, name ...string) {
r.Header().Set("Content-Type", "application/force-download")
r.Header().Set("Accept-Ranges", "bytes")
r.Header().Set("Content-Disposition", fmt.Sprintf(`attachment;filename="%s"`, downloadName))
r.Server.serveFile(r.request, serveFile)
r.Server.serveFile(r.Request, serveFile)
}
// 返回location标识引导客户端跳转。
@ -208,12 +208,12 @@ func (r *Response) ServeFileDownload(path string, name ...string) {
func (r *Response) RedirectTo(location string) {
r.Header().Set("Location", location)
r.WriteHeader(http.StatusFound)
r.request.Exit()
r.Request.Exit()
}
// 返回location标识引导客户端跳转到来源页面
func (r *Response) RedirectBack() {
r.RedirectTo(r.request.GetReferer())
r.RedirectTo(r.Request.GetReferer())
}
// 获取当前缓冲区中的数据
@ -221,6 +221,11 @@ func (r *Response) Buffer() []byte {
return r.buffer.Bytes()
}
// 获取当前缓冲区中的数据(string)
func (r *Response) BufferString() string {
return r.buffer.String()
}
// 获取当前缓冲区中的数据大小
func (r *Response) BufferLength() int {
return r.buffer.Len()

View File

@ -8,22 +8,25 @@
package ghttp
import (
"net/url"
"github.com/gogf/gf/text/gstr"
"github.com/gogf/gf/util/gconv"
)
// See https://www.w3.org/TR/cors/ .
// 服务端允许跨域请求选项
type CORSOptions struct {
AllowOrigin string // Access-Control-Allow-Origin
AllowCredentials string // Access-Control-Allow-Credentials
ExposeHeaders string // Access-Control-Expose-Headers
MaxAge int // Access-Control-Max-Age
AllowMethods string // Access-Control-Allow-Methods
AllowHeaders string // Access-Control-Allow-Headers
AllowDomain []string // Used for allowing requests from custom domains
AllowOrigin string // Access-Control-Allow-Origin
AllowCredentials string // Access-Control-Allow-Credentials
ExposeHeaders string // Access-Control-Expose-Headers
MaxAge int // Access-Control-Max-Age
AllowMethods string // Access-Control-Allow-Methods
AllowHeaders string // Access-Control-Allow-Headers
}
// 默认的CORS配置
// DefaultCORSOptions returns the default CORS options,
// which allows any cross-domain request.
func (r *Response) DefaultCORSOptions() CORSOptions {
options := CORSOptions{
AllowOrigin: "*",
@ -32,7 +35,9 @@ func (r *Response) DefaultCORSOptions() CORSOptions {
AllowHeaders: "Origin, X-Requested-With, Content-Type, Accept, Key",
MaxAge: 3628800,
}
if referer := r.request.Referer(); referer != "" {
if origin := r.Header().Get("Origin"); origin != "" {
options.AllowOrigin = origin
} else if referer := r.Request.Referer(); referer != "" {
if p := gstr.PosR(referer, "/", 6); p != -1 {
options.AllowOrigin = referer[:p]
} else {
@ -42,10 +47,10 @@ func (r *Response) DefaultCORSOptions() CORSOptions {
return options
}
// CORS sets custom CORS options.
// See https://www.w3.org/TR/cors/ .
// 允许请求跨域访问.
func (r *Response) CORS(options CORSOptions) {
if options.AllowOrigin != "" {
if r.CORSAllowedOrigin(options) {
r.Header().Set("Access-Control-Allow-Origin", options.AllowOrigin)
}
if options.AllowCredentials != "" {
@ -65,7 +70,29 @@ func (r *Response) CORS(options CORSOptions) {
}
}
// 允许请求跨域访问(使用默认配置).
// CORSAllowed checks whether the current request origin is allowed CORS.
func (r *Response) CORSAllowedOrigin(options CORSOptions) bool {
if options.AllowDomain == nil {
return true
}
origin := r.Request.Header.Get("Origin")
if origin == "" {
return true
}
parsed, err := url.Parse(origin)
if err != nil {
return false
}
for _, v := range options.AllowDomain {
if gstr.IsSubDomain(parsed.Host, v) {
return true
}
}
return false
}
// CORSDefault sets CORS with default CORS options,
// which allows any cross-domain request.
func (r *Response) CORSDefault() {
r.CORS(r.DefaultCORSOptions())
}

View File

@ -67,9 +67,9 @@ func (r *Response) buildInVars(params ...map[string]interface{}) map[string]inte
if c := gins.Config(); c.FilePath() != "" {
vars["Config"] = c.GetMap(".")
}
vars["Cookie"] = r.request.Cookie.Map()
vars["Session"] = r.request.Session.Map()
vars["Get"] = r.request.GetQueryMap()
vars["Post"] = r.request.GetPostMap()
vars["Get"] = r.Request.GetQueryMap()
vars["Post"] = r.Request.GetPostMap()
vars["Cookie"] = r.Request.Cookie.Map()
vars["Session"] = r.Request.Session.Map()
return vars
}

View File

@ -8,35 +8,47 @@
package ghttp
import (
"bufio"
"bytes"
"net"
"net/http"
)
// 自定义的ResponseWriter,用于写入流的控制
// Custom ResponseWriter, which is used for controlling the output buffer.
type ResponseWriter struct {
http.ResponseWriter
Status int // http status
buffer *bytes.Buffer // 缓冲区内容
Status int // HTTP status.
writer http.ResponseWriter // The underlying ResponseWriter.
buffer *bytes.Buffer // The output buffer.
}
// 覆盖父级的WriteHeader方法
// Header implements the interface function of http.ResponseWriter.Header.
func (w *ResponseWriter) Header() http.Header {
return w.writer.Header()
}
// Write implements the interface function of http.ResponseWriter.Write.
func (w *ResponseWriter) Write(data []byte) (int, error) {
w.buffer.Write(data)
return len(data), nil
}
// 覆盖父级的WriteHeader方法, 这里只会记录Status做缓冲处理, 并不会立即输出到HEADER。
// WriteHeader implements the interface of http.ResponseWriter.WriteHeader.
func (w *ResponseWriter) WriteHeader(status int) {
w.Status = status
}
// 输出buffer数据到客户端.
// Hijack implements the interface function of http.Hijacker.Hijack.
func (w *ResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return w.writer.(http.Hijacker).Hijack()
}
// OutputBuffer outputs the buffer to client.
func (w *ResponseWriter) OutputBuffer() {
if w.Status != 0 {
w.ResponseWriter.WriteHeader(w.Status)
w.writer.WriteHeader(w.Status)
}
if w.buffer.Len() > 0 {
w.ResponseWriter.Write(w.buffer.Bytes())
w.writer.Write(w.buffer.Bytes())
w.buffer.Reset()
}
}

View File

@ -26,10 +26,10 @@ import (
const (
gDEFAULT_HTTP_ADDR = ":80" // 默认HTTP监听地址
gDEFAULT_HTTPS_ADDR = ":443" // 默认HTTPS监听地址
NAME_TO_URI_TYPE_DEFAULT = 0 // 服务注册时对象和方法名称转换为URI时全部转为小写单词以'-'连接符号连接
NAME_TO_URI_TYPE_FULLNAME = 1 // 不处理名称以原有名称构建成URI
NAME_TO_URI_TYPE_ALLLOWER = 2 // 仅转为小写,单词间不使用连接符号
NAME_TO_URI_TYPE_CAMEL = 3 // 采用驼峰命名方式
URI_TYPE_DEFAULT = 0 // 服务注册时对象和方法名称转换为URI时全部转为小写单词以'-'连接符号连接
URI_TYPE_FULLNAME = 1 // 不处理名称以原有名称构建成URI
URI_TYPE_ALLLOWER = 2 // 仅转为小写,单词间不使用连接符号
URI_TYPE_CAMEL = 3 // 采用驼峰命名方式
gCHANGE_CONFIG_WHILE_RUNNING_ERROR = "server's configuration cannot be changed while running"
)
@ -151,12 +151,12 @@ func (s *Server) SetConfigWithMap(m map[string]interface{}) {
}
// 设置http server参数 - Addr
func (s *Server) SetAddr(itemFunc string) {
func (s *Server) SetAddr(address string) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.Addr = itemFunc
s.config.Addr = address
}
// 设置http server参数 - Port
@ -176,12 +176,12 @@ func (s *Server) SetPort(port ...int) {
}
// 设置http server参数 - HTTPS Addr
func (s *Server) SetHTTPSAddr(itemFunc string) {
func (s *Server) SetHTTPSAddr(address string) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.HTTPSAddr = itemFunc
s.config.HTTPSAddr = address
}
// 设置http server参数 - HTTPS Port

View File

@ -77,13 +77,13 @@ func (s *Server) mergeBuildInNameToPattern(pattern string, structName, methodNam
// 规则3: 采用驼峰命名方式
func (s *Server) nameToUrlPart(name string) string {
switch s.config.NameToUriType {
case NAME_TO_URI_TYPE_FULLNAME:
case URI_TYPE_FULLNAME:
return name
case NAME_TO_URI_TYPE_ALLLOWER:
case URI_TYPE_ALLLOWER:
return strings.ToLower(name)
case NAME_TO_URI_TYPE_CAMEL:
case URI_TYPE_CAMEL:
part := bytes.NewBuffer(nil)
if gstr.IsLetterUpper(name[0]) {
part.WriteByte(name[0] + 32)
@ -93,7 +93,7 @@ func (s *Server) nameToUrlPart(name string) string {
part.WriteString(name[1:])
return part.String()
case NAME_TO_URI_TYPE_DEFAULT:
case URI_TYPE_DEFAULT:
fallthrough
default:
part := bytes.NewBuffer(nil)

View File

@ -133,7 +133,12 @@ func Test_Params_Basic(t *testing.T) {
r.Response.Write(r.GetRaw())
})
s.BindHandler("/json", func(r *ghttp.Request) {
r.Response.Write(r.GetJson().Get("name"))
j, err := r.GetJson()
if err != nil {
r.Response.Write(err)
return
}
r.Response.Write(j.Get("name"))
})
s.BindHandler("/struct", func(r *ghttp.Request) {
if m := r.GetQueryMap(); len(m) > 0 {

View File

@ -25,7 +25,7 @@ func (o *NamesObject) ShowName(r *ghttp.Request) {
func Test_NameToUri_FullName(t *testing.T) {
p := ports.PopRand()
s := g.Server(p)
s.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_FULLNAME)
s.SetNameToUriType(ghttp.URI_TYPE_FULLNAME)
s.BindObject("/{.struct}/{.method}", new(NamesObject))
s.SetPort(p)
s.SetDumpRouteMap(false)
@ -47,7 +47,7 @@ func Test_NameToUri_FullName(t *testing.T) {
func Test_NameToUri_AllLower(t *testing.T) {
p := ports.PopRand()
s := g.Server(p)
s.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_ALLLOWER)
s.SetNameToUriType(ghttp.URI_TYPE_ALLLOWER)
s.BindObject("/{.struct}/{.method}", new(NamesObject))
s.SetPort(p)
s.SetDumpRouteMap(false)
@ -69,7 +69,7 @@ func Test_NameToUri_AllLower(t *testing.T) {
func Test_NameToUri_Camel(t *testing.T) {
p := ports.PopRand()
s := g.Server(p)
s.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_CAMEL)
s.SetNameToUriType(ghttp.URI_TYPE_CAMEL)
s.BindObject("/{.struct}/{.method}", new(NamesObject))
s.SetPort(p)
s.SetDumpRouteMap(false)
@ -91,7 +91,7 @@ func Test_NameToUri_Camel(t *testing.T) {
func Test_NameToUri_Default(t *testing.T) {
p := ports.PopRand()
s := g.Server(p)
s.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_DEFAULT)
s.SetNameToUriType(ghttp.URI_TYPE_DEFAULT)
s.BindObject("/{.struct}/{.method}", new(NamesObject))
s.SetPort(p)
s.SetDumpRouteMap(false)

View File

@ -444,7 +444,17 @@ func IsEmpty(path string) bool {
//
// Note: the result contains symbol '.'.
func Ext(path string) string {
return filepath.Ext(path)
ext := filepath.Ext(path)
if p := strings.IndexByte(ext, '?'); p != -1 {
ext = ext[0:p]
}
return ext
}
// ExtName is like function Ext, which returns the file name extension used by path,
// but the result does not contains symbol '.'.
func ExtName(path string) string {
return strings.TrimLeft(Ext(path), ".")
}
// Home returns absolute path of current user's home directory.

View File

@ -593,7 +593,6 @@ func Test_Dir(t *testing.T) {
})
}
// 获取文件名
func Test_Ext(t *testing.T) {
gtest.Case(t, func() {
var (
@ -608,7 +607,22 @@ func Test_Ext(t *testing.T) {
gtest.Assert(gfile.Ext(testpath()+paths1), ".txt")
gtest.Assert(gfile.Ext(testpath()+dirpath1), "")
})
gtest.Case(t, func() {
gtest.Assert(gfile.Ext("/var/www/test.js"), ".js")
gtest.Assert(gfile.Ext("/var/www/test.min.js"), ".js")
gtest.Assert(gfile.Ext("/var/www/test.js?1"), ".js")
gtest.Assert(gfile.Ext("/var/www/test.min.js?v1"), ".js")
})
}
func Test_ExtName(t *testing.T) {
gtest.Case(t, func() {
gtest.Assert(gfile.ExtName("/var/www/test.js"), "js")
gtest.Assert(gfile.ExtName("/var/www/test.min.js"), "js")
gtest.Assert(gfile.ExtName("/var/www/test.js?v=1"), "js")
gtest.Assert(gfile.ExtName("/var/www/test.min.js?v=1"), "js")
})
}

View File

@ -1,117 +0,0 @@
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// Package gflock implements a concurrent-safe sync.Locker interface for file locking.
package gflock
import (
"github.com/gogf/gf/os/gfile"
flock "github.com/theckman/go-flock"
)
// File locker.
type Locker struct {
flock *flock.Flock // Underlying file locker.
}
// New creates and returns a new file locker with given <file>.
// The parameter <file> usually is a absolute file path.
func New(file string) *Locker {
dir := gfile.TempDir() + gfile.Separator + "gflock"
if !gfile.Exists(dir) {
_ = gfile.Mkdir(dir)
}
path := dir + gfile.Separator + file
lock := flock.NewFlock(path)
return &Locker{
flock: lock,
}
}
// Path returns the file path of the locker.
func (l *Locker) Path() string {
return l.flock.Path()
}
// IsLocked returns whether the locker is locked.
func (l *Locker) IsLocked() bool {
return l.flock.Locked()
}
// IsRLocked returns whether the locker is rlocked.
func (l *Locker) IsRLocked() bool {
return l.flock.RLocked()
}
// TryLock tries get the writing lock of the locker.
// It returns true if success, or else returns false immediately.
func (l *Locker) TryLock() bool {
ok, _ := l.flock.TryLock()
return ok
}
// TryRLock tries get the reading lock of the locker.
// It returns true if success, or else returns false immediately.
func (l *Locker) TryRLock() bool {
ok, _ := l.flock.TryRLock()
return ok
}
// Lock is a blocking call to try and take an exclusive file lock. It will wait
// until it is able to obtain the exclusive file lock. It's recommended that
// TryLock() be used over this function. This function may block the ability to
// query the current Locked() or RLocked() status due to a RW-mutex lock.
//
// If we are already exclusive-locked, this function short-circuits and returns
// immediately assuming it can take the mutex lock.
//
// If the *Flock has a shared lock (RLock), this may transparently replace the
// shared lock with an exclusive lock on some UNIX-like operating systems. Be
// careful when using exclusive locks in conjunction with shared locks
// (RLock()), because calling Unlock() may accidentally release the exclusive
// lock that was once a shared lock.
func (l *Locker) Lock() (err error) {
return l.flock.Lock()
}
// Unlock is a function to unlock the file. This file takes a RW-mutex lock, so
// while it is running the Locked() and RLocked() functions will be blocked.
//
// This function short-circuits if we are unlocked already. If not, it calls
// syscall.LOCK_UN on the file and closes the file descriptor. It does not
// remove the file from disk. It's up to your application to do.
//
// Please note, if your shared lock became an exclusive lock this may
// unintentionally drop the exclusive lock if called by the consumer that
// believes they have a shared lock. Please see Lock() for more details.
func (l *Locker) Unlock() (err error) {
return l.flock.Unlock()
}
// RLock is a blocking call to try and take a ahred file lock. It will wait
// until it is able to obtain the shared file lock. It's recommended that
// TryRLock() be used over this function. This function may block the ability to
// query the current Locked() or RLocked() status due to a RW-mutex lock.
//
// If we are already shared-locked, this function short-circuits and returns
// immediately assuming it can take the mutex lock.
func (l *Locker) RLock() (err error) {
return l.flock.RLock()
}
// Unlock is a function to unlock the file. This file takes a RW-mutex lock, so
// while it is running the Locked() and RLocked() functions will be blocked.
//
// This function short-circuits if we are unlocked already. If not, it calls
// syscall.LOCK_UN on the file and closes the file descriptor. It does not
// remove the file from disk. It's up to your application to do.
//
// Please note, if your shared lock became an exclusive lock this may
// unintentionally drop the exclusive lock if called by the consumer that
// believes they have a shared lock. Please see Lock() for more details.
func (l *Locker) RUnlock() (err error) {
return l.flock.Unlock()
}

View File

@ -1,180 +0,0 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gflock_test
import (
"testing"
"time"
"github.com/gogf/gf/container/garray"
"github.com/gogf/gf/os/gfile"
"github.com/gogf/gf/os/gflock"
"github.com/gogf/gf/test/gtest"
)
func Test_GFlock_Base(t *testing.T) {
gtest.Case(t, func() {
fileName := "test"
lock := gflock.New(fileName)
gtest.Assert(lock.Path(), gfile.TempDir()+gfile.Separator+"gflock"+gfile.Separator+fileName)
gtest.Assert(lock.IsLocked(), false)
lock.Lock()
gtest.Assert(lock.IsLocked(), true)
lock.Unlock()
gtest.Assert(lock.IsLocked(), false)
})
gtest.Case(t, func() {
fileName := "test"
lock := gflock.New(fileName)
gtest.Assert(lock.Path(), gfile.TempDir()+gfile.Separator+"gflock"+gfile.Separator+fileName)
gtest.Assert(lock.IsRLocked(), false)
lock.RLock()
gtest.Assert(lock.IsRLocked(), true)
lock.RUnlock()
gtest.Assert(lock.IsRLocked(), false)
})
}
func Test_GFlock_Lock(t *testing.T) {
gtest.Case(t, func() {
fileName := "testLock"
array := garray.New(true)
lock := gflock.New(fileName)
lock2 := gflock.New(fileName)
go func() {
lock.Lock()
array.Append(1)
time.Sleep(300 * time.Millisecond)
lock.Unlock()
}()
go func() {
time.Sleep(100 * time.Millisecond)
lock2.Lock()
array.Append(1)
lock2.Unlock()
}()
time.Sleep(100 * time.Millisecond)
gtest.Assert(array.Len(), 1)
time.Sleep(100 * time.Millisecond)
gtest.Assert(array.Len(), 1)
time.Sleep(200 * time.Millisecond)
gtest.Assert(array.Len(), 2)
})
}
func Test_GFlock_RLock(t *testing.T) {
gtest.Case(t, func() {
fileName := "testRLock"
array := garray.New(true)
lock := gflock.New(fileName)
lock2 := gflock.New(fileName)
go func() {
lock.RLock()
array.Append(1)
time.Sleep(400 * time.Millisecond)
lock.RUnlock()
}()
go func() {
time.Sleep(200 * time.Millisecond)
lock2.RLock()
array.Append(1)
lock2.RUnlock()
}()
time.Sleep(100 * time.Millisecond)
gtest.Assert(array.Len(), 1)
time.Sleep(200 * time.Millisecond)
gtest.Assert(array.Len(), 2)
})
}
func Test_GFlock_TryLock(t *testing.T) {
gtest.Case(t, func() {
fileName := "testTryLock"
array := garray.New(true)
lock := gflock.New(fileName)
lock2 := gflock.New(fileName)
go func() {
lock.TryLock()
array.Append(1)
time.Sleep(200 * time.Millisecond)
lock.Unlock()
}()
go func() {
time.Sleep(100 * time.Millisecond)
if lock2.TryLock() {
array.Append(1)
lock2.Unlock()
}
}()
go func() {
time.Sleep(300 * time.Millisecond)
if lock2.TryLock() {
array.Append(1)
lock2.Unlock()
}
}()
time.Sleep(100 * time.Millisecond)
gtest.Assert(array.Len(), 1)
time.Sleep(100 * time.Millisecond)
gtest.Assert(array.Len(), 1)
time.Sleep(200 * time.Millisecond)
gtest.Assert(array.Len(), 2)
})
}
func Test_GFlock_TryRLock(t *testing.T) {
gtest.Case(t, func() {
fileName := "testTryRLock"
array := garray.New(true)
lock := gflock.New(fileName)
lock2 := gflock.New(fileName)
go func() {
lock.TryRLock()
array.Append(1)
time.Sleep(300 * time.Millisecond)
lock.Unlock()
}()
go func() {
time.Sleep(200 * time.Millisecond)
if lock2.TryRLock() {
array.Append(1)
lock2.Unlock()
}
}()
go func() {
time.Sleep(200 * time.Millisecond)
if lock2.TryRLock() {
array.Append(1)
lock2.Unlock()
}
}()
go func() {
time.Sleep(200 * time.Millisecond)
if lock2.TryRLock() {
array.Append(1)
lock2.Unlock()
}
}()
time.Sleep(100 * time.Millisecond)
gtest.Assert(array.Len(), 1)
time.Sleep(300 * time.Millisecond)
gtest.Assert(array.Len(), 4)
})
}

View File

@ -27,12 +27,11 @@ type Storage interface {
// RemoveAll deletes all key-value pairs from storage.
RemoveAll() error
// GetSession returns the session data bytes for given session id.
// GetSession returns the session data map for given session id.
// The parameter specifies the TTL for this session.
// It returns nil if the TTL is exceeded.
GetSession(id string, ttl time.Duration) map[string]interface{}
// SetSession updates the content for session id.
// Note that the parameter <content> is the serialized bytes for session map.
// SetSession updates the data map for specified session id.
SetSession(id string, data map[string]interface{}) error
// UpdateTTL updates the TTL for specified session id.

44
text/gstr/gstr_domain.go Normal file
View File

@ -0,0 +1,44 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gstr
import "strings"
// IsSubDomain checks whether <subDomain> is sub-domain of mainDomain.
// It supports '*' in <mainDomain>.
func IsSubDomain(subDomain string, mainDomain string) bool {
if p := strings.IndexByte(subDomain, ':'); p != -1 {
subDomain = subDomain[0:p]
}
if p := strings.IndexByte(mainDomain, ':'); p != -1 {
mainDomain = mainDomain[0:p]
}
subArray := strings.Split(subDomain, ".")
mainArray := strings.Split(mainDomain, ".")
subLength := len(subArray)
mainLength := len(mainArray)
// Eg:
// "s.s.goframe.org" is not sub-domain of "*.goframe.org"
// but
// "s.s.goframe.org" is not sub-domain of "goframe.org"
if mainLength > 2 && subLength > mainLength {
return false
}
minLength := subLength
if mainLength < minLength {
minLength = mainLength
}
for i := minLength; i > 0; i-- {
if mainArray[mainLength-i] == "*" {
continue
}
if mainArray[mainLength-i] != subArray[subLength-i] {
return false
}
}
return true
}

View File

@ -0,0 +1,61 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// go test *.go -bench=".*"
package gstr_test
import (
"testing"
"github.com/gogf/gf/test/gtest"
"github.com/gogf/gf/text/gstr"
)
func Test_IsSubDomain(t *testing.T) {
gtest.Case(t, func() {
main := "goframe.org"
gtest.Assert(gstr.IsSubDomain("goframe.org", main), true)
gtest.Assert(gstr.IsSubDomain("s.goframe.org", main), true)
gtest.Assert(gstr.IsSubDomain("s.s.goframe.org", main), true)
gtest.Assert(gstr.IsSubDomain("s.s.goframe.org:8080", main), true)
gtest.Assert(gstr.IsSubDomain("johng.cn", main), false)
gtest.Assert(gstr.IsSubDomain("s.johng.cn", main), false)
gtest.Assert(gstr.IsSubDomain("s.s.johng.cn", main), false)
})
gtest.Case(t, func() {
main := "*.goframe.org"
gtest.Assert(gstr.IsSubDomain("goframe.org", main), true)
gtest.Assert(gstr.IsSubDomain("s.goframe.org", main), true)
gtest.Assert(gstr.IsSubDomain("s.goframe.org:80", main), true)
gtest.Assert(gstr.IsSubDomain("s.s.goframe.org", main), false)
gtest.Assert(gstr.IsSubDomain("johng.cn", main), false)
gtest.Assert(gstr.IsSubDomain("s.johng.cn", main), false)
gtest.Assert(gstr.IsSubDomain("s.s.johng.cn", main), false)
})
gtest.Case(t, func() {
main := "*.*.goframe.org"
gtest.Assert(gstr.IsSubDomain("goframe.org", main), true)
gtest.Assert(gstr.IsSubDomain("s.goframe.org", main), true)
gtest.Assert(gstr.IsSubDomain("s.s.goframe.org", main), true)
gtest.Assert(gstr.IsSubDomain("s.s.goframe.org:8000", main), true)
gtest.Assert(gstr.IsSubDomain("s.s.s.goframe.org", main), false)
gtest.Assert(gstr.IsSubDomain("johng.cn", main), false)
gtest.Assert(gstr.IsSubDomain("s.johng.cn", main), false)
gtest.Assert(gstr.IsSubDomain("s.s.johng.cn", main), false)
})
gtest.Case(t, func() {
main := "*.*.goframe.org:8080"
gtest.Assert(gstr.IsSubDomain("goframe.org", main), true)
gtest.Assert(gstr.IsSubDomain("s.goframe.org", main), true)
gtest.Assert(gstr.IsSubDomain("s.s.goframe.org", main), true)
gtest.Assert(gstr.IsSubDomain("s.s.goframe.org:8000", main), true)
gtest.Assert(gstr.IsSubDomain("s.s.s.goframe.org", main), false)
gtest.Assert(gstr.IsSubDomain("johng.cn", main), false)
gtest.Assert(gstr.IsSubDomain("s.johng.cn", main), false)
gtest.Assert(gstr.IsSubDomain("s.s.johng.cn", main), false)
})
}

View File

@ -58,9 +58,13 @@ func init() {
// Intn returns a int number which is between 0 and max - [0, max).
//
// Note:
// 1. The result is greater than or equal to 0, but less than <max>;
// 2. The result number is 32bit and less than math.MaxUint32.
// 1. The <max> can only be geater than 0, or else it return <max> directly;
// 2. The result is greater than or equal to 0, but less than <max>;
// 3. The result number is 32bit and less than math.MaxUint32.
func Intn(max int) int {
if max <= 0 {
return max
}
n := int(<-bufferChan) % max
if (max > 0 && n < 0) || (max < 0 && n > 0) {
return -n

View File

@ -25,7 +25,7 @@ func Test_Intn(t *testing.T) {
for i := 0; i < 1000000; i++ {
n := grand.Intn(-100)
gtest.AssertLE(n, 0)
gtest.AssertGT(n, -100)
gtest.Assert(n, -100)
}
})
}

View File

@ -1,4 +1,4 @@
package gf
const VERSION = "v1.9.1"
const VERSION = "v1.9.3"
const AUTHORS = "john<john@goframe.org>"