improving gdb.Model/gmap

This commit is contained in:
John
2019-09-26 20:01:48 +08:00
parent ef4e128af7
commit c85162a8a0
19 changed files with 229 additions and 241 deletions

View File

@ -1,8 +1,6 @@
package main
import (
"fmt"
"github.com/gogf/gf/frame/g"
)
@ -10,11 +8,20 @@ func main() {
db := g.DB()
db.SetDebug(true)
r, e := db.Table("test").Where("id IN (?)", []interface{}{1, 2}).All()
//type User struct {
// Uid int
// Name *gvar.Var
//}
//user := new(User)
////user.Name = g.NewVar("john")
//g.Dump(gconv.Map(user))
_, e := db.Table("test").Data(g.Map{
"name": nil,
}).Update()
if e != nil {
panic(e)
}
if r != nil {
fmt.Println(r.ToList())
}
}

View File

@ -1,48 +1,18 @@
package main
import (
"fmt"
"github.com/gogf/gf/os/gfile"
"github.com/gogf/gf/text/gregex"
"github.com/gogf/gf/debug/gdebug"
"github.com/gogf/gf/container/gvar"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/util/gconv"
)
func main() {
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)
type User struct {
Uid int
Name *gvar.Var
}
func main() {
user := new(User)
user.Name = g.NewVar("john")
g.Dump(gconv.Map(user))
}

View File

@ -9,6 +9,8 @@ package gmap
import (
"encoding/json"
"github.com/gogf/gf/internal/empty"
"github.com/gogf/gf/util/gconv"
"github.com/gogf/gf/container/gvar"
@ -68,6 +70,12 @@ func (m *AnyAnyMap) Map() map[interface{}]interface{} {
return data
}
// Data returns the underlying data map.
// Note that it's not concurrent safe using this function to access the data.
func (m *AnyAnyMap) Data() map[interface{}]interface{} {
return m.data
}
// MapStrAny returns a copy of the data of the map as map[string]interface{}.
func (m *AnyAnyMap) MapStrAny() map[string]interface{} {
m.mu.RLock()
@ -79,6 +87,17 @@ func (m *AnyAnyMap) MapStrAny() map[string]interface{} {
return data
}
// FilterEmpty deletes all key-value pair of which the value is empty.
func (m *AnyAnyMap) FilterEmpty() {
m.mu.Lock()
for k, v := range m.data {
if empty.IsEmpty(v) {
delete(m.data, k)
}
}
m.mu.Unlock()
}
// Set sets key-value to the hash map.
func (m *AnyAnyMap) Set(key interface{}, val interface{}) {
m.mu.Lock()

View File

@ -57,6 +57,12 @@ func (m *IntAnyMap) Clone() *IntAnyMap {
return NewIntAnyMapFrom(m.Map(), !m.mu.IsSafe())
}
// Data returns the underlying data map.
// Note that it's not concurrent safe using this function to access the data.
func (m *IntAnyMap) Data() map[int]interface{} {
return m.data
}
// Map returns a copy of the data of the hash map.
func (m *IntAnyMap) Map() map[int]interface{} {
m.mu.RLock()

View File

@ -54,6 +54,12 @@ func (m *IntIntMap) Clone() *IntIntMap {
return NewIntIntMapFrom(m.Map(), !m.mu.IsSafe())
}
// Data returns the underlying data map.
// Note that it's not concurrent safe using this function to access the data.
func (m *IntIntMap) Data() map[int]int {
return m.data
}
// Map returns a copy of the data of the hash map.
func (m *IntIntMap) Map() map[int]int {
m.mu.RLock()

View File

@ -55,6 +55,12 @@ func (m *IntStrMap) Clone() *IntStrMap {
return NewIntStrMapFrom(m.Map(), !m.mu.IsSafe())
}
// Data returns the underlying data map.
// Note that it's not concurrent safe using this function to access the data.
func (m *IntStrMap) Data() map[int]string {
return m.data
}
// Map returns a copy of the data of the hash map.
func (m *IntStrMap) Map() map[int]string {
m.mu.RLock()

View File

@ -57,6 +57,12 @@ func (m *StrAnyMap) Clone() *StrAnyMap {
return NewStrAnyMapFrom(m.Map(), !m.mu.IsSafe())
}
// Data returns the underlying data map.
// Note that it's not concurrent safe using this function to access the data.
func (m *StrAnyMap) Data() map[string]interface{} {
return m.data
}
// Map returns a copy of the data of the hash map.
func (m *StrAnyMap) Map() map[string]interface{} {
m.mu.RLock()

View File

@ -56,6 +56,12 @@ func (m *StrIntMap) Clone() *StrIntMap {
return NewStrIntMapFrom(m.Map(), !m.mu.IsSafe())
}
// Data returns the underlying data map.
// Note that it's not concurrent safe using this function to access the data.
func (m *StrIntMap) Data() map[string]int {
return m.data
}
// Map returns a copy of the data of the hash map.
func (m *StrIntMap) Map() map[string]int {
m.mu.RLock()

View File

@ -55,6 +55,12 @@ func (m *StrStrMap) Clone() *StrStrMap {
return NewStrStrMapFrom(m.Map(), !m.mu.IsSafe())
}
// Data returns the underlying data map.
// Note that it's not concurrent safe using this function to access the data.
func (m *StrStrMap) Data() map[string]string {
return m.data
}
// Map returns a copy of the data of the hash map.
func (m *StrStrMap) Map() map[string]string {
m.mu.RLock()

View File

@ -0,0 +1,133 @@
// Copyright 2017-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 gm file,
// You can obtain one at https://github.com/gogf/gf.
package gmap_test
import (
"testing"
"github.com/gogf/gf/container/gmap"
"github.com/gogf/gf/test/gtest"
)
func anyAnyCallBack(int, interface{}) bool {
return true
}
func Test_AnyAnyMap_Basic(t *testing.T) {
gtest.Case(t, func() {
m := gmap.NewAnyAnyMap()
m.Set(1, 1)
gtest.Assert(m.Get(1), 1)
gtest.Assert(m.Size(), 1)
gtest.Assert(m.IsEmpty(), false)
gtest.Assert(m.GetOrSet(2, "2"), "2")
gtest.Assert(m.SetIfNotExist(2, "2"), false)
gtest.Assert(m.SetIfNotExist(3, 3), true)
gtest.Assert(m.Remove(2), "2")
gtest.Assert(m.Contains(2), false)
gtest.AssertIN(3, m.Keys())
gtest.AssertIN(1, m.Keys())
gtest.AssertIN(3, m.Values())
gtest.AssertIN(1, m.Values())
m.Flip()
gtest.Assert(m.Map(), map[interface{}]int{1: 1, 3: 3})
m.Clear()
gtest.Assert(m.Size(), 0)
gtest.Assert(m.IsEmpty(), true)
m2 := gmap.NewAnyAnyMapFrom(map[interface{}]interface{}{1: 1, 2: "2"})
gtest.Assert(m2.Map(), map[interface{}]interface{}{1: 1, 2: "2"})
})
}
func Test_AnyAnyMap_Set_Fun(t *testing.T) {
m := gmap.NewAnyAnyMap()
m.GetOrSetFunc(1, getAny)
m.GetOrSetFuncLock(2, getAny)
gtest.Assert(m.Get(1), 123)
gtest.Assert(m.Get(2), 123)
gtest.Assert(m.SetIfNotExistFunc(1, getAny), false)
gtest.Assert(m.SetIfNotExistFunc(3, getAny), true)
gtest.Assert(m.SetIfNotExistFuncLock(2, getAny), false)
gtest.Assert(m.SetIfNotExistFuncLock(4, getAny), true)
}
func Test_AnyAnyMap_Batch(t *testing.T) {
m := gmap.NewAnyAnyMap()
m.Sets(map[interface{}]interface{}{1: 1, 2: "2", 3: 3})
gtest.Assert(m.Map(), map[interface{}]interface{}{1: 1, 2: "2", 3: 3})
m.Removes([]interface{}{1, 2})
gtest.Assert(m.Map(), map[interface{}]interface{}{3: 3})
}
func Test_AnyAnyMap_Iterator(t *testing.T) {
expect := map[interface{}]interface{}{1: 1, 2: "2"}
m := gmap.NewAnyAnyMapFrom(expect)
m.Iterator(func(k interface{}, v interface{}) bool {
gtest.Assert(expect[k], v)
return true
})
// 断言返回值对遍历控制
i := 0
j := 0
m.Iterator(func(k interface{}, v interface{}) bool {
i++
return true
})
m.Iterator(func(k interface{}, v interface{}) bool {
j++
return false
})
gtest.Assert(i, "2")
gtest.Assert(j, 1)
}
func Test_AnyAnyMap_Lock(t *testing.T) {
expect := map[interface{}]interface{}{1: 1, 2: "2"}
m := gmap.NewAnyAnyMapFrom(expect)
m.LockFunc(func(m map[interface{}]interface{}) {
gtest.Assert(m, expect)
})
m.RLockFunc(func(m map[interface{}]interface{}) {
gtest.Assert(m, expect)
})
}
func Test_AnyAnyMap_Clone(t *testing.T) {
//clone 方法是深克隆
m := gmap.NewAnyAnyMapFrom(map[interface{}]interface{}{1: 1, 2: "2"})
m_clone := m.Clone()
m.Remove(1)
//修改原 map,clone 后的 map 不影响
gtest.AssertIN(1, m_clone.Keys())
m_clone.Remove(2)
//修改clone map,原 map 不影响
gtest.AssertIN(2, m.Keys())
}
func Test_AnyAnyMap_Merge(t *testing.T) {
m1 := gmap.NewAnyAnyMap()
m2 := gmap.NewAnyAnyMap()
m1.Set(1, 1)
m2.Set(2, "2")
m1.Merge(m2)
gtest.Assert(m1.Map(), map[interface{}]interface{}{1: 1, 2: "2"})
}

View File

@ -56,6 +56,9 @@ func (v *Var) Set(value interface{}) (old interface{}) {
// Val returns the current value of <v>.
func (v *Var) Val() interface{} {
if v == nil {
return nil
}
if v.safe {
return v.value.(*gtype.Interface).Val()
}

View File

@ -43,6 +43,8 @@ type Model struct {
const (
gLINK_TYPE_MASTER = 1 // 主节点类型
gLINK_TYPE_SLAVE = 2 // 从节点类型
OPTION_OMITEMPTY = 1 << iota
OPTION_ALLOWEMPTY
)
// 链式操作,数据表字段,可支持多个表,以半角逗号连接

View File

@ -24,13 +24,12 @@ import (
)
const (
gDEFAULT_HTTP_ADDR = ":80" // 默认HTTP监听地址
gDEFAULT_HTTPS_ADDR = ":443" // 默认HTTPS监听地址
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"
gDEFAULT_HTTP_ADDR = ":80" // 默认HTTP监听地址
gDEFAULT_HTTPS_ADDR = ":443" // 默认HTTPS监听地址
URI_TYPE_DEFAULT = 0 // 服务注册时对象和方法名称转换为URI时全部转为小写单词以'-'连接符号连接
URI_TYPE_FULLNAME = 1 // 不处理名称以原有名称构建成URI
URI_TYPE_ALLLOWER = 2 // 仅转为小写,单词间不使用连接符号
URI_TYPE_CAMEL = 3 // 采用驼峰命名方式
)
// 自定义日志处理方法类型
@ -125,45 +124,31 @@ func ConfigFromMap(m map[string]interface{}) ServerConfig {
// http server setting设置。
// 注意使用该方法进行http server配置时需要配置所有的配置项否则没有配置的属性将会默认变量为空
func (s *Server) SetConfig(c ServerConfig) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
func (s *Server) SetConfig(c ServerConfig) error {
if c.Handler == nil {
c.Handler = http.HandlerFunc(s.defaultHttpHandle)
}
s.config = c
if c.LogPath != "" {
s.logger.SetPath(c.LogPath)
return s.logger.SetPath(c.LogPath)
}
return nil
}
// 通过map设置http server setting。
// 注意使用该方法进行http server配置时需要配置所有的配置项否则没有配置的属性将会默认变量为空
func (s *Server) SetConfigWithMap(m map[string]interface{}) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.SetConfig(ConfigFromMap(m))
}
// 设置http server参数 - Addr
func (s *Server) SetAddr(address string) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.Addr = address
}
// 设置http server参数 - Port
func (s *Server) SetPort(port ...int) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error("config cannot be changed while running")
}
if len(port) > 0 {
s.config.Addr = ""
for _, v := range port {
@ -177,19 +162,11 @@ func (s *Server) SetPort(port ...int) {
// 设置http server参数 - HTTPS Addr
func (s *Server) SetHTTPSAddr(address string) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.HTTPSAddr = address
}
// 设置http server参数 - HTTPS Port
func (s *Server) SetHTTPSPort(port ...int) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
if len(port) > 0 {
s.config.HTTPSAddr = ""
for _, v := range port {
@ -203,10 +180,6 @@ func (s *Server) SetHTTPSPort(port ...int) {
// 开启HTTPS支持但是必须提供Cert和Key文件tlsConfig为可选项
func (s *Server) EnableHTTPS(certFile, keyFile string, tlsConfig ...tls.Config) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
certFileRealPath := gfile.RealPath(certFile)
if certFileRealPath == "" {
certFileRealPath = gfile.RealPath(gfile.Pwd() + gfile.Separator + certFile)
@ -236,74 +209,41 @@ func (s *Server) EnableHTTPS(certFile, keyFile string, tlsConfig ...tls.Config)
// 设置TLS配置对象
func (s *Server) SetTLSConfig(tlsConfig tls.Config) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.TLSConfig = tlsConfig
}
// 设置http server参数 - ReadTimeout
func (s *Server) SetReadTimeout(t time.Duration) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.ReadTimeout = t
}
// 设置http server参数 - WriteTimeout
func (s *Server) SetWriteTimeout(t time.Duration) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.WriteTimeout = t
}
// 设置http server参数 - IdleTimeout
func (s *Server) SetIdleTimeout(t time.Duration) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.IdleTimeout = t
}
// 设置http server参数 - MaxHeaderBytes
func (s *Server) SetMaxHeaderBytes(b int) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.MaxHeaderBytes = b
}
// 设置http server参数 - ServerAgent
func (s *Server) SetServerAgent(agent string) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.ServerAgent = agent
}
// 设置KeepAlive
func (s *Server) SetKeepAlive(enabled bool) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.KeepAlive = enabled
}
// 设置模板引擎对象
func (s *Server) SetView(view *gview.View) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.View = view
}

View File

@ -8,34 +8,20 @@ package ghttp
import (
"time"
"github.com/gogf/gf/os/glog"
)
// 设置http server参数 - CookieMaxAge
func (s *Server) SetCookieMaxAge(ttl time.Duration) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.CookieMaxAge = ttl
}
// 设置http server参数 - CookiePath
func (s *Server) SetCookiePath(path string) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.CookiePath = path
}
// 设置http server参数 - CookieDomain
func (s *Server) SetCookieDomain(domain string) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.CookieDomain = domain
}

View File

@ -6,69 +6,43 @@
package ghttp
import (
"github.com/gogf/gf/os/glog"
)
// 设置日志目录,只有在设置了日志目录的情况下才会输出日志到日志文件中。
// 日志文件路径格式为:
// 1. 请求日志: access/YYYY-MM-DD.log
// 2. 错误日志: error/YYYY-MM-DD.log
func (s *Server) SetLogPath(path string) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
if len(path) == 0 {
return
}
s.config.LogPath = path
s.config.ErrorLogEnabled = true
s.config.AccessLogEnabled = true
s.logger.SetPath(path)
}
// 设置日志内容是否输出到终端,默认情况下只有错误日志才会自动输出到终端。
// 如果需要输出请求日志到终端默认情况下使用SetAccessLogEnabled方法开启请求日志特性即可。
func (s *Server) SetLogStdout(enabled bool) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.LogStdout = enabled
}
// 设置是否开启access log日志功能
func (s *Server) SetAccessLogEnabled(enabled bool) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.AccessLogEnabled = enabled
}
// 设置是否开启error log日志功能
func (s *Server) SetErrorLogEnabled(enabled bool) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.ErrorLogEnabled = enabled
}
// 设置是否开启error stack打印功能
func (s *Server) SetErrorStack(enabled bool) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.ErrorStack = enabled
}
// 设置日志写入的回调函数
func (s *Server) SetLogHandler(handler LogHandler) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.LogHandler = handler
}

View File

@ -6,47 +6,25 @@
package ghttp
import "github.com/gogf/gf/os/glog"
func (s *Server) SetGzipContentTypes(types []string) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.GzipContentTypes = types
}
// 服务注册时对象和方法名称转换为URI时的规则
func (s *Server) SetNameToUriType(t int) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.NameToUriType = t
}
// 是否在程序启动时打印路由表信息
func (s *Server) SetDumpRouteMap(enabled bool) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.DumpRouteMap = enabled
}
// 设置路由缓存过期时间(秒)
func (s *Server) SetRouterCacheExpire(expire int) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.RouterCacheExpire = expire
}
func (s *Server) SetFormParsingMemory(maxMemory int64) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.FormParsingMemory = maxMemory
}

View File

@ -6,47 +6,25 @@
package ghttp
import "github.com/gogf/gf/os/glog"
func (s *Server) SetDenyIps(ips []string) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.DenyIps = ips
}
func (s *Server) SetAllowIps(ips []string) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.AllowIps = ips
}
func (s *Server) SetDenyRoutes(routes []string) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.DenyRoutes = routes
}
// 设置URI重写规则
func (s *Server) SetRewrite(uri string, rewrite string) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.Rewrites[uri] = rewrite
}
// 设置URI重写规则批量
func (s *Server) SetRewriteMap(rewrites map[string]string) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
for k, v := range rewrites {
s.config.Rewrites[k] = v
}

View File

@ -10,35 +10,21 @@ import (
"time"
"github.com/gogf/gf/os/gsession"
"github.com/gogf/gf/os/glog"
)
// 设置http server参数 - SessionMaxAge
func (s *Server) SetSessionMaxAge(ttl time.Duration) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.SessionMaxAge = ttl
s.sessionManager.SetTTL(ttl)
}
// 设置http server参数 - SessionIdName
func (s *Server) SetSessionIdName(name string) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.SessionIdName = name
}
// 设置http server参数 - SessionStorage
func (s *Server) SetSessionStorage(storage gsession.Storage) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.SessionStorage = storage
s.sessionManager.SetStorage(storage)
}

View File

@ -28,37 +28,21 @@ type staticPathItem struct {
// 设置http server参数 - IndexFiles默认展示文件index.html, index.htm
func (s *Server) SetIndexFiles(index []string) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.IndexFiles = index
}
// 允许展示访问目录的文件列表
func (s *Server) SetIndexFolder(enabled bool) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.IndexFolder = enabled
}
// 是否开启/关闭静态文件服务,当关闭时仅提供动态接口服务,路由性能会得到一定提升
func (s *Server) SetFileServerEnabled(enabled bool) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.FileServerEnabled = enabled
}
// 设置http server参数 - ServerRoot
func (s *Server) SetServerRoot(root string) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
realPath := root
if !gres.Contains(realPath) {
if p, err := gfile.Search(root); err != nil {
@ -74,10 +58,6 @@ func (s *Server) SetServerRoot(root string) {
// 添加静态文件搜索**目录**,必须给定目录的绝对路径
func (s *Server) AddSearchPath(path string) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
realPath := path
if !gres.Contains(realPath) {
if p, err := gfile.Search(path); err != nil {
@ -92,10 +72,6 @@ func (s *Server) AddSearchPath(path string) {
// 添加URI与静态**目录**的映射
func (s *Server) AddStaticPath(prefix string, path string) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
realPath := path
if !gres.Contains(realPath) {
if p, err := gfile.Search(path); err != nil {