mirror of
https://gitee.com/johng/gf
synced 2026-07-07 14:25:17 +08:00
improve configuration feature for glog/ghttp.Server
This commit is contained in:
@ -234,7 +234,9 @@ func (r *Response) ClearBuffer() {
|
||||
|
||||
// Output outputs the buffer content to the client.
|
||||
func (r *Response) Output() {
|
||||
r.Header().Set("Server", r.Server.config.ServerAgent)
|
||||
if r.Server.config.ServerAgent != "" {
|
||||
r.Header().Set("Server", r.Server.config.ServerAgent)
|
||||
}
|
||||
//r.handleGzip()
|
||||
r.Writer.OutputBuffer()
|
||||
}
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
package ghttp
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/gins"
|
||||
"github.com/gogf/gf/os/gcfg"
|
||||
"github.com/gogf/gf/os/gview"
|
||||
"github.com/gogf/gf/util/gmode"
|
||||
)
|
||||
@ -57,14 +57,14 @@ func (r *Response) ParseTplContent(content string, params ...gview.Params) (stri
|
||||
|
||||
// 内置变量/对象
|
||||
func (r *Response) buildInVars(params ...map[string]interface{}) map[string]interface{} {
|
||||
vars := map[string]interface{}(nil)
|
||||
var vars map[string]interface{}
|
||||
if len(params) > 0 && params[0] != nil {
|
||||
vars = params[0]
|
||||
} else {
|
||||
vars = make(map[string]interface{})
|
||||
}
|
||||
// 当配置文件不存在时就不赋值该模板变量,不然会报错
|
||||
if c := gins.Config(); c.FilePath() != "" {
|
||||
if c := gcfg.Instance(); c.FilePath() != "" {
|
||||
vars["Config"] = c.GetMap(".")
|
||||
}
|
||||
vars["Get"] = r.Request.GetQueryMap()
|
||||
|
||||
@ -227,7 +227,9 @@ func GetServer(name ...interface{}) *Server {
|
||||
logger: glog.New(),
|
||||
}
|
||||
// 初始化时使用默认配置
|
||||
s.SetConfig(c)
|
||||
if err := s.SetConfig(c); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// 记录到全局ServerMap中
|
||||
serverMapping.Set(serverName, s)
|
||||
return s
|
||||
@ -368,7 +370,7 @@ func (s *Server) GetRouteMap() string {
|
||||
m[item.domain].Add(item)
|
||||
}
|
||||
}
|
||||
itemFunc := s.config.Addr
|
||||
itemFunc := s.config.Address
|
||||
if s.config.HTTPSAddr != "" {
|
||||
if len(itemFunc) > 0 {
|
||||
itemFunc += ","
|
||||
@ -424,9 +426,9 @@ func (s *Server) startServer(fdMap listenerFdMap) {
|
||||
// HTTPS
|
||||
// ================
|
||||
if len(s.config.HTTPSAddr) == 0 {
|
||||
if len(s.config.Addr) > 0 {
|
||||
s.config.HTTPSAddr = s.config.Addr
|
||||
s.config.Addr = ""
|
||||
if len(s.config.Address) > 0 {
|
||||
s.config.HTTPSAddr = s.config.Address
|
||||
s.config.Address = ""
|
||||
} else {
|
||||
s.config.HTTPSAddr = gDEFAULT_HTTPS_ADDR
|
||||
}
|
||||
@ -464,14 +466,14 @@ func (s *Server) startServer(fdMap listenerFdMap) {
|
||||
// HTTP
|
||||
// ================
|
||||
// 当HTTPS服务未启用时,默认HTTP地址才会生效
|
||||
if !httpsEnabled && len(s.config.Addr) == 0 {
|
||||
s.config.Addr = gDEFAULT_HTTP_ADDR
|
||||
if !httpsEnabled && len(s.config.Address) == 0 {
|
||||
s.config.Address = gDEFAULT_HTTP_ADDR
|
||||
}
|
||||
var array []string
|
||||
if v, ok := fdMap["http"]; ok && len(v) > 0 {
|
||||
array = strings.Split(v, ",")
|
||||
} else {
|
||||
array = strings.Split(s.config.Addr, ",")
|
||||
array = strings.Split(s.config.Address, ",")
|
||||
}
|
||||
for _, v := range array {
|
||||
if len(v) == 0 {
|
||||
|
||||
@ -37,7 +37,7 @@ type LogHandler func(r *Request, err ...error)
|
||||
|
||||
// HTTP Server 设置结构体,静态配置
|
||||
type ServerConfig struct {
|
||||
Addr string // 监听IP和端口,监听本地所有IP使用":端口"(支持多个地址,使用","号分隔)
|
||||
Address string // Server listening address like ":port", multiple addresses separated using ','
|
||||
HTTPSAddr string // HTTPS服务监听地址(支持多个地址,使用","号分隔)
|
||||
HTTPSCertPath string // HTTPS证书文件路径
|
||||
HTTPSKeyPath string // HTTPS签名文件路径
|
||||
@ -81,7 +81,7 @@ type ServerConfig struct {
|
||||
|
||||
// 默认HTTP Server配置
|
||||
var defaultServerConfig = ServerConfig{
|
||||
Addr: "",
|
||||
Address: "",
|
||||
HTTPSAddr: "",
|
||||
Handler: nil,
|
||||
ReadTimeout: 60 * time.Second,
|
||||
@ -116,12 +116,12 @@ func Config() ServerConfig {
|
||||
}
|
||||
|
||||
// 通过Map创建Config配置对象,Map没有传递的属性将会使用模块的默认值
|
||||
func ConfigFromMap(m map[string]interface{}) ServerConfig {
|
||||
func ConfigFromMap(m map[string]interface{}) (ServerConfig, error) {
|
||||
config := defaultServerConfig
|
||||
if err := gconv.Struct(m, &config); err != nil {
|
||||
panic(err)
|
||||
return config, err
|
||||
}
|
||||
return config
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// http server setting设置。
|
||||
@ -140,24 +140,28 @@ func (s *Server) SetConfig(c ServerConfig) error {
|
||||
|
||||
// 通过map设置http server setting。
|
||||
// 注意使用该方法进行http server配置时,需要配置所有的配置项,否则没有配置的属性将会默认变量为空
|
||||
func (s *Server) SetConfigWithMap(m map[string]interface{}) {
|
||||
s.SetConfig(ConfigFromMap(m))
|
||||
func (s *Server) SetConfigWithMap(m map[string]interface{}) error {
|
||||
config, err := ConfigFromMap(m)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.SetConfig(config)
|
||||
}
|
||||
|
||||
// 设置http server参数 - Addr
|
||||
func (s *Server) SetAddr(address string) {
|
||||
s.config.Addr = address
|
||||
s.config.Address = address
|
||||
}
|
||||
|
||||
// 设置http server参数 - Port
|
||||
func (s *Server) SetPort(port ...int) {
|
||||
if len(port) > 0 {
|
||||
s.config.Addr = ""
|
||||
s.config.Address = ""
|
||||
for _, v := range port {
|
||||
if len(s.config.Addr) > 0 {
|
||||
s.config.Addr += ","
|
||||
if len(s.config.Address) > 0 {
|
||||
s.config.Address += ","
|
||||
}
|
||||
s.config.Addr += ":" + strconv.Itoa(v)
|
||||
s.config.Address += ":" + strconv.Itoa(v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -21,19 +21,35 @@ import (
|
||||
func Test_ConfigFromMap(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
m := g.Map{
|
||||
"addr": ":8199",
|
||||
"address": ":8199",
|
||||
"readTimeout": "60s",
|
||||
"indexFiles": g.Slice{"index.php", "main.php"},
|
||||
"errorLogEnabled": true,
|
||||
"cookieMaxAge": "1y",
|
||||
}
|
||||
config := ghttp.ConfigFromMap(m)
|
||||
config, err := ghttp.ConfigFromMap(m)
|
||||
gtest.Assert(err, nil)
|
||||
d1, _ := time.ParseDuration(gconv.String(m["readTimeout"]))
|
||||
d2, _ := time.ParseDuration(gconv.String(m["cookieMaxAge"]))
|
||||
gtest.Assert(config.Addr, m["addr"])
|
||||
gtest.Assert(config.Address, m["address"])
|
||||
gtest.Assert(config.ReadTimeout, d1)
|
||||
gtest.Assert(config.CookieMaxAge, d2)
|
||||
gtest.Assert(config.IndexFiles, m["indexFiles"])
|
||||
gtest.Assert(config.ErrorLogEnabled, m["errorLogEnabled"])
|
||||
})
|
||||
}
|
||||
|
||||
func Test_SetConfigWithMap(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
m := g.Map{
|
||||
"address": ":8199",
|
||||
"readTimeout": "60s",
|
||||
"indexFiles": g.Slice{"index.php", "main.php"},
|
||||
"errorLogEnabled": true,
|
||||
"cookieMaxAge": "1y",
|
||||
}
|
||||
s := g.Server()
|
||||
err := s.SetConfigWithMap(m)
|
||||
gtest.Assert(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user