diff --git a/g/frame/gbase/gbase.go b/g/frame/gbase/gbase.go index ee2fc723e..3445aed1e 100644 --- a/g/frame/gbase/gbase.go +++ b/g/frame/gbase/gbase.go @@ -6,13 +6,14 @@ import ( "gitee.com/johng/gf/g/database/gdb" "gitee.com/johng/gf/g/frame/gconfig" "gitee.com/johng/gf/g/frame/ginstance" + "gitee.com/johng/gf/g/os/gconsole" ) const ( gDEFAULT_CONFIG_FILE = "config.json" // 默认读取的配置文件名称 ) -// 框架基类 +// 框架基类,所有的基于gf框架的类对象都继承于此,以便使用框架的一些封装的核心组件 type Base struct { Db gdb.Link Config *gconfig.Config @@ -20,16 +21,18 @@ type Base struct { // 基类初始化,如若需要自定义初始化内置核心对象组件,可在继承子类中覆盖此方法 func (b *Base) Init() { - // 默认配置目录为当前程序运行目录 + // 配置文件目录查找依次为:启动参数cfgpath、当前程序运行目录 if b.Config == nil { - path := gfile.SelfDir() + path := gconsole.Option.Get("cfgpath") + if path == "" { + path = gfile.SelfDir() + } ckey := "gf_config_with_path_" + path result := ginstance.Get(ckey) if result != nil { b.Config = result.(*gconfig.Config) } else { b.Config = gconfig.New(path) - b.Config.Add(gDEFAULT_CONFIG_FILE) ginstance.Set(ckey, b.Config) } } @@ -49,10 +52,10 @@ func (b *Base) Init() { } } } - } - if link, err := gdb.Instance(); err != nil { - b.Db = link - ginstance.Set(ckey, b.Db) + if link, err := gdb.Instance(); err != nil { + b.Db = link + ginstance.Set(ckey, b.Db) + } } } } diff --git a/g/frame/gconfig/gconfig.go b/g/frame/gconfig/gconfig.go index afcb7234f..c014d144a 100644 --- a/g/frame/gconfig/gconfig.go +++ b/g/frame/gconfig/gconfig.go @@ -31,7 +31,7 @@ func (c *Config) file(files []string) string { if len(files) > 0 { file = files[0] } - return file + return file + ".json" } // 添加配置文件到配置管理器中,第二个参数为非必须,如果不输入表示添加进入默认的配置名称中 diff --git a/g/frame/ginstance/ginstance.go b/g/frame/ginstance/ginstance.go index 35e7b228c..413a4ee75 100644 --- a/g/frame/ginstance/ginstance.go +++ b/g/frame/ginstance/ginstance.go @@ -1,3 +1,4 @@ +// 单例对象管理工具 package ginstance import "gitee.com/johng/gf/g/container/gmap" diff --git a/g/frame/gmvc/controller.go b/g/frame/gmvc/controller.go index c93334d66..f684b7321 100644 --- a/g/frame/gmvc/controller.go +++ b/g/frame/gmvc/controller.go @@ -22,7 +22,7 @@ type Controller struct { View *View // 视图对象 } -// 控制器初始化 +// 控制器初始化接口方法 func (c *Controller) Init(s *ghttp.Server, r *ghttp.ClientRequest, w *ghttp.ServerResponse) { c.Base.Init() c.Server = s @@ -37,7 +37,7 @@ func (c *Controller) Init(s *ghttp.Server, r *ghttp.ClientRequest, w *ghttp.Serv } } -// 控制器结束请求 +// 控制器结束请求接口方法 func (c *Controller) Shut() { if c.Cookie.Get(gDEFAULT_SESSION_ID_NAME) == "" { c.Cookie.Set(gDEFAULT_SESSION_ID_NAME, c.Session.Id()) diff --git a/g/frame/gmvc/view.go b/g/frame/gmvc/view.go index 92aebdb88..20d3300ae 100644 --- a/g/frame/gmvc/view.go +++ b/g/frame/gmvc/view.go @@ -6,6 +6,7 @@ import ( "gitee.com/johng/gf/g/os/gview" "gitee.com/johng/gf/g/frame/gbase" "gitee.com/johng/gf/g/os/gfile" + "gitee.com/johng/gf/g/os/gconsole" ) // 视图对象(一个请求一个视图对象,用完即销毁) @@ -19,13 +20,17 @@ type View struct { // 创建一个MVC请求中使用的视图对象 func NewView(c *Controller) *View { - viewpath := gfile.SelfDir() + // 视图目录路径查找优先级:配置文件参数viewpath、启动参数viewpath、当前程序运行目录 + path := gconsole.Option.Get("viewpath") + if path == "" { + path = gfile.SelfDir() + } if r := c.Config.Get("viewpath"); r != nil { - viewpath = r.(string) + path = r.(string) } return &View{ ctl : c, - view : gview.GetView(viewpath), + view : gview.GetView(path), data : make(map[string]interface{}), } } diff --git a/g/net/ghttp/http_server.go b/g/net/ghttp/http_server.go index 06b6528fe..ee85eec7a 100644 --- a/g/net/ghttp/http_server.go +++ b/g/net/ghttp/http_server.go @@ -41,7 +41,8 @@ type HandlerFunc struct { // Server表,用以存储和检索名称与Server对象之间的关联关系 var serverMapping = gmap.NewStringInterfaceMap() -// 创建一个默认配置的HTTP Server(默认监听端口是80) +// 获取/创建一个默认配置的HTTP Server(默认监听端口是80) +// 单例模式,请保证name的唯一性 func GetServer(name string) (*Server) { if s := serverMapping.Get(name); s != nil { return s.(*Server) diff --git a/g/net/ghttp/http_server_handler.go b/g/net/ghttp/http_server_handler.go index d64c9c840..a9d66fe08 100644 --- a/g/net/ghttp/http_server_handler.go +++ b/g/net/ghttp/http_server_handler.go @@ -19,6 +19,9 @@ func (s *Server)defaultHttpHandle(w http.ResponseWriter, r *http.Request) { } // 执行处理HTTP请求 +// 首先,查找是否有对应域名的处理接口配置; +// 其次,如果没有对应的自定义处理接口配置,那么走默认的域名处理接口配置; +// 最后,如果以上都没有找到处理接口,那么进行文件处理; func (s *Server)handleRequest(w http.ResponseWriter, r *http.Request) { request := &ClientRequest{} response := &ServerResponse{} diff --git a/g/os/genv/genv.go b/g/os/genv/genv.go new file mode 100644 index 000000000..08ee1e8cf --- /dev/null +++ b/g/os/genv/genv.go @@ -0,0 +1,19 @@ +package genv + +import "os" + +func All() []string { + return os.Environ() +} + +func Get(k string) string { + return os.Getenv(k) +} + +func Set(k, v string) error { + return os.Setenv(k, v) +} + +func Remove(k string) error { + return os.Unsetenv(k) +} \ No newline at end of file diff --git a/geg/frame/config.json b/geg/frame/config.json index 1962b43b0..c83051631 100644 --- a/geg/frame/config.json +++ b/geg/frame/config.json @@ -1,5 +1,4 @@ { - "configpath" : "", "viewpath" : "/home/john/Workspace/Go/GOPATH/src/gitee.com/johng/gf/geg/frame/mvc/view", "database" : { "default" : [ diff --git a/geg/other/test.go b/geg/other/test.go index 6a4c48873..221b6082c 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -1,10 +1,7 @@ package main import ( "fmt" - "regexp" - "strings" - "strconv" - "gitee.com/johng/gf/g/database/gdb" + "os" ) func Add(path string, name ... string) { @@ -12,33 +9,10 @@ func Add(path string, name ... string) { } func main() { - nodestr := "账号@地址:端口 ,密码 , 数据库名称, 数据库类型, 集群角色 , 字符编码, 负载均衡优先级 , 12345" - reg, _ := regexp.Compile(`(.+)@(.+):([^,]+),([^,]+),([^,]+),([^,]+)`) - match := reg.FindStringSubmatch(nodestr) - if match != nil { - node := gdb.ConfigNode{ - User : strings.TrimSpace(match[1]), - Host : strings.TrimSpace(match[2]), - Port : strings.TrimSpace(match[3]), - Pass : strings.TrimSpace(match[4]), - Name : strings.TrimSpace(match[5]), - Type : strings.TrimSpace(match[6]), - } - extra := strings.Split(nodestr[len(match[0]) + 1:], ",") - if len(extra) > 0 { - node.Role = strings.TrimSpace(extra[0]) - } - if len(extra) > 1 { - node.Charset = strings.TrimSpace(extra[1]) - } - if len(extra) > 2 { - node.Priority, _ = strconv.Atoi(strings.TrimSpace(extra[2])) - } - if len(extra) > 3 { - index := len(extra[0]) + len(extra[1]) + len(extra[2]) + 3 - node.Linkinfo = strings.TrimSpace(nodestr[len(match[0]) + 1 + index:]) - } - fmt.Println(node) + for _, e := range os.Environ() { + + fmt.Println(e) + } } \ No newline at end of file