mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
新增环境变量管理工具genv,完善gmvc配置管理功能
This commit is contained in:
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -31,7 +31,7 @@ func (c *Config) file(files []string) string {
|
||||
if len(files) > 0 {
|
||||
file = files[0]
|
||||
}
|
||||
return file
|
||||
return file + ".json"
|
||||
}
|
||||
|
||||
// 添加配置文件到配置管理器中,第二个参数为非必须,如果不输入表示添加进入默认的配置名称中
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
// 单例对象管理工具
|
||||
package ginstance
|
||||
|
||||
import "gitee.com/johng/gf/g/container/gmap"
|
||||
|
||||
@ -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())
|
||||
|
||||
@ -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{}),
|
||||
}
|
||||
}
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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{}
|
||||
|
||||
19
g/os/genv/genv.go
Normal file
19
g/os/genv/genv.go
Normal file
@ -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)
|
||||
}
|
||||
@ -1,5 +1,4 @@
|
||||
{
|
||||
"configpath" : "",
|
||||
"viewpath" : "/home/john/Workspace/Go/GOPATH/src/gitee.com/johng/gf/geg/frame/mvc/view",
|
||||
"database" : {
|
||||
"default" : [
|
||||
|
||||
@ -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)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user