mirror of
https://gitee.com/johng/gf
synced 2026-06-21 07:57:36 +08:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a8caf4ad21 | |||
| 51d9fe5253 | |||
| 3218c89f17 |
@ -7,7 +7,7 @@ import (
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_FULLNAME)
|
||||
s.SetNameToUriType(ghttp.URI_TYPE_FULLNAME)
|
||||
s.EnableAdmin()
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.Write("hello world")
|
||||
|
||||
25
.example/net/ghttp/server/cors/cors1.go
Normal file
25
.example/net/ghttp/server/cors/cors1.go
Normal file
@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func MiddlewareCORS(r *ghttp.Request) {
|
||||
r.Response.CORSDefault()
|
||||
r.Middleware.Next()
|
||||
}
|
||||
|
||||
func Order(r *ghttp.Request) {
|
||||
r.Response.Write("GET")
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/api.v1", func(g *ghttp.RouterGroup) {
|
||||
g.Middleware(MiddlewareCORS)
|
||||
g.GET("/order", Order)
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
27
.example/net/ghttp/server/cors/cors2.go
Normal file
27
.example/net/ghttp/server/cors/cors2.go
Normal file
@ -0,0 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func MiddlewareCORS(r *ghttp.Request) {
|
||||
corsOptions := r.Response.DefaultCORSOptions()
|
||||
corsOptions.AllowDomain = []string{"goframe.org", "baidu.com"}
|
||||
r.Response.CORS(corsOptions)
|
||||
r.Middleware.Next()
|
||||
}
|
||||
|
||||
func Order(r *ghttp.Request) {
|
||||
r.Response.Write("GET")
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/api.v1", func(g *ghttp.RouterGroup) {
|
||||
g.Middleware(MiddlewareCORS)
|
||||
g.GET("/order", Order)
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
33
.example/net/ghttp/server/cors/cors3.go
Normal file
33
.example/net/ghttp/server/cors/cors3.go
Normal file
@ -0,0 +1,33 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func MiddlewareCORS(r *ghttp.Request) {
|
||||
corsOptions := r.Response.DefaultCORSOptions()
|
||||
corsOptions.AllowDomain = []string{"goframe.org"}
|
||||
if !r.Response.CORSAllowedOrigin(corsOptions) {
|
||||
r.Response.WriteStatus(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
r.Response.CORS(corsOptions)
|
||||
r.Middleware.Next()
|
||||
}
|
||||
|
||||
func Order(r *ghttp.Request) {
|
||||
r.Response.Write("GET")
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/api.v1", func(g *ghttp.RouterGroup) {
|
||||
g.Middleware(MiddlewareCORS)
|
||||
g.GET("/order", Order)
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
@ -17,10 +17,10 @@ func main() {
|
||||
s3 := g.Server(3)
|
||||
s4 := g.Server(4)
|
||||
|
||||
s1.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_DEFAULT)
|
||||
s2.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_FULLNAME)
|
||||
s3.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_ALLLOWER)
|
||||
s4.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_CAMEL)
|
||||
s1.SetNameToUriType(ghttp.URI_TYPE_DEFAULT)
|
||||
s2.SetNameToUriType(ghttp.URI_TYPE_FULLNAME)
|
||||
s3.SetNameToUriType(ghttp.URI_TYPE_ALLLOWER)
|
||||
s4.SetNameToUriType(ghttp.URI_TYPE_CAMEL)
|
||||
|
||||
s1.BindObject("/{.struct}/{.method}", new(User))
|
||||
s2.BindObject("/{.struct}/{.method}", new(User))
|
||||
|
||||
@ -9,7 +9,7 @@ import (
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.SetServerRoot("public")
|
||||
s.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_ALLLOWER)
|
||||
s.SetNameToUriType(ghttp.URI_TYPE_ALLLOWER)
|
||||
s.SetErrorLogEnabled(true)
|
||||
s.SetAccessLogEnabled(true)
|
||||
s.SetPort(2333)
|
||||
|
||||
@ -1,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)
|
||||
}
|
||||
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@ -14,3 +14,4 @@ bin/
|
||||
cbuild
|
||||
**/.DS_Store
|
||||
.vscode/
|
||||
go.sum
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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...))
|
||||
|
||||
@ -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{
|
||||
|
||||
@ -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.
|
||||
|
||||
44
go.sum
44
go.sum
@ -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=
|
||||
@ -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
|
||||
|
||||
@ -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()
|
||||
|
||||
@ -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()
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -221,6 +221,11 @@ func (r *Response) Buffer() []byte {
|
||||
return r.buffer.Bytes()
|
||||
}
|
||||
|
||||
// 获取当前缓冲区中的数据(string)
|
||||
func (r *Response) BufferString() string {
|
||||
return r.buffer.String()
|
||||
}
|
||||
|
||||
// 获取当前缓冲区中的数据大小
|
||||
func (r *Response) BufferLength() int {
|
||||
return r.buffer.Len()
|
||||
|
||||
@ -8,22 +8,25 @@
|
||||
package ghttp
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
)
|
||||
|
||||
// See https://www.w3.org/TR/cors/ .
|
||||
// 服务端允许跨域请求选项
|
||||
type CORSOptions struct {
|
||||
AllowOrigin string // Access-Control-Allow-Origin
|
||||
AllowCredentials string // Access-Control-Allow-Credentials
|
||||
ExposeHeaders string // Access-Control-Expose-Headers
|
||||
MaxAge int // Access-Control-Max-Age
|
||||
AllowMethods string // Access-Control-Allow-Methods
|
||||
AllowHeaders string // Access-Control-Allow-Headers
|
||||
AllowDomain []string // Used for allowing requests from custom domains
|
||||
AllowOrigin string // Access-Control-Allow-Origin
|
||||
AllowCredentials string // Access-Control-Allow-Credentials
|
||||
ExposeHeaders string // Access-Control-Expose-Headers
|
||||
MaxAge int // Access-Control-Max-Age
|
||||
AllowMethods string // Access-Control-Allow-Methods
|
||||
AllowHeaders string // Access-Control-Allow-Headers
|
||||
}
|
||||
|
||||
// 默认的CORS配置
|
||||
// DefaultCORSOptions returns the default CORS options,
|
||||
// which allows any cross-domain request.
|
||||
func (r *Response) DefaultCORSOptions() CORSOptions {
|
||||
options := CORSOptions{
|
||||
AllowOrigin: "*",
|
||||
@ -32,7 +35,9 @@ func (r *Response) DefaultCORSOptions() CORSOptions {
|
||||
AllowHeaders: "Origin, X-Requested-With, Content-Type, Accept, Key",
|
||||
MaxAge: 3628800,
|
||||
}
|
||||
if referer := r.request.Referer(); referer != "" {
|
||||
if origin := r.Header().Get("Origin"); origin != "" {
|
||||
options.AllowOrigin = origin
|
||||
} else if referer := r.request.Referer(); referer != "" {
|
||||
if p := gstr.PosR(referer, "/", 6); p != -1 {
|
||||
options.AllowOrigin = referer[:p]
|
||||
} else {
|
||||
@ -42,10 +47,10 @@ func (r *Response) DefaultCORSOptions() CORSOptions {
|
||||
return options
|
||||
}
|
||||
|
||||
// CORS sets custom CORS options.
|
||||
// See https://www.w3.org/TR/cors/ .
|
||||
// 允许请求跨域访问.
|
||||
func (r *Response) CORS(options CORSOptions) {
|
||||
if options.AllowOrigin != "" {
|
||||
if r.CORSAllowedOrigin(options) {
|
||||
r.Header().Set("Access-Control-Allow-Origin", options.AllowOrigin)
|
||||
}
|
||||
if options.AllowCredentials != "" {
|
||||
@ -65,7 +70,29 @@ func (r *Response) CORS(options CORSOptions) {
|
||||
}
|
||||
}
|
||||
|
||||
// 允许请求跨域访问(使用默认配置).
|
||||
// CORSAllowed checks whether the current request origin is allowed CORS.
|
||||
func (r *Response) CORSAllowedOrigin(options CORSOptions) bool {
|
||||
if options.AllowDomain == nil {
|
||||
return true
|
||||
}
|
||||
origin := r.request.Header.Get("Origin")
|
||||
if origin == "" {
|
||||
return false
|
||||
}
|
||||
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())
|
||||
}
|
||||
|
||||
@ -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"
|
||||
)
|
||||
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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")
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@ -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
44
text/gstr/gstr_domain.go
Normal 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
|
||||
}
|
||||
61
text/gstr/gstr_z_unit_domain_test.go
Normal file
61
text/gstr/gstr_z_unit_domain_test.go
Normal 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)
|
||||
})
|
||||
}
|
||||
@ -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
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
package gf
|
||||
|
||||
const VERSION = "v1.9.1"
|
||||
const VERSION = "v1.9.3"
|
||||
const AUTHORS = "john<john@goframe.org>"
|
||||
|
||||
Reference in New Issue
Block a user