Skip to content

Commit

Permalink
improve configuration feature for glog/ghttp.Server
Browse files Browse the repository at this point in the history
  • Loading branch information
gqcn committed Nov 7, 2019
1 parent 9e32d74 commit 05120b5
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 111 deletions.
39 changes: 5 additions & 34 deletions .example/other/test.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,12 @@
package main

import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/os/glog"
"fmt"
"regexp"
)

func AddAPKCmdTask11(assistantId int, cmd int32, cmdData []byte, FromClientId string, desc string, priority int, status int32, cmdkey string) (int64, error) {
//var res, err = g.DB("test").Insert("assistant_tasks", g.Map{
// "assistant_id": assistantId,
// "cmd": cmd,
// "cmdData": cmdData,
// "status": status,
// "FromClientId": FromClientId,
// "desc": desc,
// "priority": priority,
// "cmdkey": cmdkey,
//})
var res, err = g.DB("test").Table("assistant_tasks").Data(g.Map{
"assistant_id": assistantId,
"cmd": cmd,
"cmdData": cmdData,
"status": status,
"FromClientId": FromClientId,
"desc": desc,
"priority": priority,
"cmdkey": cmdkey,
}).Insert()

if err != nil {
glog.Error("插入手机任务队列报错", err.Error())
return 0, err
}
taskId, err := res.LastInsertId()
return taskId, err
}

func main() {
g.DB().SetDebug(true)
AddAPKCmdTask11(1, 2058, []byte(""), "", "", 60, 0, "")
replaceCharReg, err := regexp.Compile(`[\-\.\_\s]+`)
fmt.Println(err)
fmt.Println(replaceCharReg.ReplaceAllString("s--s.s.a b", ""))
}
2 changes: 1 addition & 1 deletion frame/g/g_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

// Server returns an instance of http server with specified name.
func Server(name ...interface{}) *ghttp.Server {
return ghttp.GetServer(name...)
return gins.Server(name...)
}

// TCPServer returns an instance of tcp server with specified name.
Expand Down
51 changes: 38 additions & 13 deletions frame/gins/gins.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ package gins

import (
"fmt"
"github.com/gogf/gf/internal/intlog"
"github.com/gogf/gf/net/ghttp"

"github.com/gogf/gf/os/gfile"

Expand All @@ -28,6 +30,7 @@ import (
const (
gFRAME_CORE_COMPONENT_NAME_REDIS = "gf.core.component.redis"
gFRAME_CORE_COMPONENT_NAME_LOGGER = "gf.core.component.logger"
gFRAME_CORE_COMPONENT_NAME_SERVER = "gf.core.component.server"
gFRAME_CORE_COMPONENT_NAME_DATABASE = "gf.core.component.database"
)

Expand Down Expand Up @@ -100,17 +103,19 @@ func I18n(name ...string) *gi18n.Manager {
// Log returns an instance of glog.Logger.
// The parameter <name> is the name for the instance.
func Log(name ...string) *glog.Logger {
config := Config()
instanceName := glog.DEFAULT_NAME
if len(name) > 0 && name[0] != "" {
instanceName = name[0]
}
instanceKey := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_LOGGER, instanceName)
return instances.GetOrSetFuncLock(instanceKey, func() interface{} {
logger := glog.Instance(instanceName)
if m := config.GetMap("logging"); m != nil {
if err := logger.SetConfigWithMap(m); err != nil {
glog.Error(err)
// To avoid file no found error while it's not necessary.
if Config().FilePath() != "" {
if m := Config().GetMap("logging"); m != nil {
if err := logger.SetConfigWithMap(m); err != nil {
glog.Panic(err)
}
}
}
return logger
Expand All @@ -130,13 +135,13 @@ func Database(name ...string) gdb.DB {
if gdb.GetConfig(group) != nil {
db, err := gdb.Instance(group)
if err != nil {
glog.Error(err)
glog.Panic(err)
}
return db
}
m := config.GetMap("database")
if m == nil {
glog.Error(`database init failed: "database" node not found, is config file or configuration missing?`)
glog.Panic(`database init failed: "database" node not found, is config file or configuration missing?`)
return nil
}
// Parse <m> as map-slice.
Expand Down Expand Up @@ -173,7 +178,7 @@ func Database(name ...string) gdb.DB {
if db, err := gdb.New(name...); err == nil {
return db
} else {
glog.Error(err)
glog.Panic(err)
}
return nil
})
Expand All @@ -191,7 +196,7 @@ func parseDBConfigNode(value interface{}) *gdb.ConfigNode {
node := &gdb.ConfigNode{}
err := gconv.Struct(nodeMap, node)
if err != nil {
glog.Error(err)
glog.Panic(err)
}
if value, ok := nodeMap["link"]; ok {
node.LinkInfo = gconv.String(value)
Expand All @@ -210,7 +215,7 @@ func parseDBConfigNode(value interface{}) *gdb.ConfigNode {
// Redis returns an instance of redis client with specified configuration group name.
func Redis(name ...string) *gredis.Redis {
config := Config()
group := "default"
group := gredis.DEFAULT_GROUP_NAME
if len(name) > 0 && name[0] != "" {
group = name[0]
}
Expand All @@ -225,16 +230,16 @@ func Redis(name ...string) *gredis.Redis {
if v, ok := m[group]; ok {
redisConfig, err := gredis.ConfigFromStr(gconv.String(v))
if err != nil {
glog.Error(err)
glog.Panic(err)
return nil
}
addConfigMonitor(instanceKey, config)
return gredis.New(redisConfig)
} else {
glog.Errorf(`configuration for redis not found for group "%s"`, group)
glog.Panicf(`configuration for redis not found for group "%s"`, group)
}
} else {
glog.Errorf(`incomplete configuration for redis: "redis" node not found in config file "%s"`, config.FilePath())
glog.Panicf(`incomplete configuration for redis: "redis" node not found in config file "%s"`, config.FilePath())
}
return nil
})
Expand All @@ -244,10 +249,30 @@ func Redis(name ...string) *gredis.Redis {
return nil
}

// Server returns an instance of http server with specified name.
func Server(name ...interface{}) *ghttp.Server {
instanceKey := fmt.Sprintf("%s.%v", gFRAME_CORE_COMPONENT_NAME_SERVER, name)
return instances.GetOrSetFuncLock(instanceKey, func() interface{} {
s := ghttp.GetServer(name...)
// To avoid file no found error while it's not necessary.
if Config().FilePath() != "" {
if m := Config().GetMap("server"); m != nil {
if err := s.SetConfigWithMap(m); err != nil {
panic(err)
}
}
}
return s
}).(*ghttp.Server)
}

func addConfigMonitor(key string, config *gcfg.Config) {
if path := config.FilePath(); path != "" && gfile.Exists(path) {
gfsnotify.Add(path, func(event *gfsnotify.Event) {
_, err := gfsnotify.Add(path, func(event *gfsnotify.Event) {
instances.Remove(key)
})
if err != nil {
intlog.Error(err)
}
}
}
4 changes: 3 additions & 1 deletion net/ghttp/ghttp_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
6 changes: 3 additions & 3 deletions net/ghttp/ghttp_response_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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()
Expand Down
18 changes: 10 additions & 8 deletions net/ghttp/ghttp_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 += ","
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
28 changes: 16 additions & 12 deletions net/ghttp/ghttp_server_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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签名文件路径
Expand Down Expand Up @@ -81,7 +81,7 @@ type ServerConfig struct {

// 默认HTTP Server配置
var defaultServerConfig = ServerConfig{
Addr: "",
Address: "",
HTTPSAddr: "",
Handler: nil,
ReadTimeout: 60 * time.Second,
Expand Down Expand Up @@ -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设置。
Expand All @@ -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)
}
}
}
Expand Down
22 changes: 19 additions & 3 deletions net/ghttp/ghttp_unit_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
Loading

0 comments on commit 05120b5

Please sign in to comment.