mirror of
https://gitee.com/johng/gf.git
synced 2024-12-04 05:07:44 +08:00
83 lines
2.2 KiB
Go
83 lines
2.2 KiB
Go
// Copyright 2018 gf Author(https://gitee.com/johng/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 this file,
|
||
// You can obtain one at https://gitee.com/johng/gf.
|
||
|
||
package g
|
||
|
||
import (
|
||
"gitee.com/johng/gf/g/util/gconv"
|
||
"gitee.com/johng/gf/g/database/gdb"
|
||
"gitee.com/johng/gf/g/database/gredis"
|
||
"gitee.com/johng/gf/g/frame/gins"
|
||
"gitee.com/johng/gf/g/net/ghttp"
|
||
"gitee.com/johng/gf/g/net/gtcp"
|
||
"gitee.com/johng/gf/g/net/gudp"
|
||
"gitee.com/johng/gf/g/os/gview"
|
||
"gitee.com/johng/gf/g/os/gcfg"
|
||
"gitee.com/johng/gf/g/util/gregex"
|
||
)
|
||
|
||
// HTTPServer单例对象
|
||
func Server(name...interface{}) *ghttp.Server {
|
||
return ghttp.GetServer(name...)
|
||
}
|
||
|
||
// TCPServer单例对象
|
||
func TcpServer(name...interface{}) *gtcp.Server {
|
||
return gtcp.GetServer(name...)
|
||
}
|
||
|
||
// UDPServer单例对象
|
||
func UdpServer(name...interface{}) *gudp.Server {
|
||
return gudp.GetServer(name...)
|
||
}
|
||
|
||
// 核心对象:View
|
||
func View() *gview.View {
|
||
return gins.View()
|
||
}
|
||
|
||
// Config配置管理对象
|
||
// 配置文件目录查找依次为:启动参数cfgpath、当前程序运行目录
|
||
func Config() *gcfg.Config {
|
||
return gins.Config()
|
||
}
|
||
|
||
// 数据库操作对象,使用了连接池
|
||
func Database(name...string) *gdb.Db {
|
||
return gins.Database(name...)
|
||
}
|
||
|
||
// (别名)Database
|
||
func DB(name...string) *gdb.Db {
|
||
return gins.Database(name...)
|
||
}
|
||
|
||
// Redis操作对象,使用了连接池
|
||
func Redis(name...string) *gredis.Redis {
|
||
group := "default"
|
||
if len(name) > 0 {
|
||
group = name[0]
|
||
}
|
||
config := gins.Config()
|
||
if config == nil {
|
||
return nil
|
||
}
|
||
if m := config.GetMap("redis"); m != nil {
|
||
// host:port[,db[,pass]]
|
||
if v, ok := m[group]; ok {
|
||
array, err := gregex.MatchString(`(.+):(\d+),{0,1}(\d*),{0,1}(.*)`, gconv.String(v))
|
||
if err == nil {
|
||
return gredis.New(gredis.Config{
|
||
Host : array[1],
|
||
Port : gconv.Int(array[2]),
|
||
Db : gconv.Int(array[3]),
|
||
Pass : array[4],
|
||
})
|
||
}
|
||
}
|
||
}
|
||
return nil
|
||
} |