Skip to content

Commit

Permalink
Merge pull request #70 from sunmi-OS/docs
Browse files Browse the repository at this point in the history
feat: update
  • Loading branch information
2276282419 authored Nov 24, 2021
2 parents f4bce6c + f5642be commit 3be25f3
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 169 deletions.
49 changes: 28 additions & 21 deletions conf/nacos/nacos.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const (

_NacosScheme = "NACOS_SCHEME"
_NacosContextPath = "NACOS_CONTEXT_PATH"
_NacosIp = "NACOS_IP"
_NacosHost = "NACOS_HOST"
_NacosPort = "NACOS_PORT"
)

Expand All @@ -53,12 +53,31 @@ var nacosHarder = &nacos{
// NewNacosEnv 注入Nacos配置文件
// 兼容endpoint 和 service 两种方式
func NewNacosEnv() {
// 获取nacos鉴权、地区、命名空间
namespaceId := utils.Either(os.Getenv(_NacosNamespaceId), os.Getenv(_NamespaceId))
accessKey := utils.Either(os.Getenv(_NacosAccessKey), os.Getenv(_AccessKey))
secretKey := utils.Either(os.Getenv(_NacosSecretKey), os.Getenv(_SecretKey))
regionID := utils.Either(os.Getenv(_NacosRegionId), os.Getenv(_RegionId))
if namespaceId == "" || accessKey == "" || secretKey == "" {
panic("The configuration file cannot be empty.")
}
if regionID == "" {
regionID = _DefaultRegionId
}
ccConfig := &constant.ClientConfig{
NamespaceId: namespaceId,
AccessKey: accessKey,
SecretKey: secretKey,
RegionId: regionID,
LogLevel: LogError,
}

// 读取service地址,如果有service优先使用service连接方式
nacosIp := os.Getenv(_NacosIp)
nacosHost := os.Getenv(_NacosHost)
nacosPort := os.Getenv(_NacosPort)
if nacosIp != "" && nacosPort != "" {
err := NewNacos(nil, constant.ServerConfig{
IpAddr: nacosIp,
if nacosHost != "" && nacosPort != "" {
err := NewNacos(ccConfig, constant.ServerConfig{
IpAddr: nacosHost,
Port: cast.ToUint64(nacosPort),
Scheme: os.Getenv(_NacosScheme),
ContextPath: os.Getenv(_NacosContextPath),
Expand All @@ -69,25 +88,13 @@ func NewNacosEnv() {
return
}

namespaceId := utils.Either(os.Getenv(_NacosNamespaceId), os.Getenv(_NamespaceId))
// 如果未使用service检查是否配置了endpoint兼容acm
endpoint := utils.Either(os.Getenv(_NacosEndpoint), os.Getenv(_Endpoint))
accessKey := utils.Either(os.Getenv(_NacosAccessKey), os.Getenv(_AccessKey))
secretKey := utils.Either(os.Getenv(_NacosSecretKey), os.Getenv(_SecretKey))
regionID := utils.Either(os.Getenv(_NacosRegionId), os.Getenv(_RegionId))
if endpoint == "" || namespaceId == "" || accessKey == "" || secretKey == "" {
if endpoint == "" {
panic("The configuration file cannot be empty.")
}
if regionID == "" {
regionID = _DefaultRegionId
}
err := NewNacos(&constant.ClientConfig{
Endpoint: endpoint,
NamespaceId: namespaceId,
AccessKey: accessKey,
SecretKey: secretKey,
RegionId: regionID,
LogLevel: LogError,
})
ccConfig.Endpoint = endpoint
err := NewNacos(ccConfig)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion tools/gocore/conf/gocore.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func GetGocoreConfig() *GoCore {
Handle: []Handle{
{
Name: "GetUserInfo",
Method: "Any",
Method: "POST",
Comment: "获取用户信息",
RequestParams: []Param{
{
Expand Down
1 change: 1 addition & 0 deletions tools/gocore/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/urfave/cli/v2"
)

// @TODO 命名不允许用关键字
func main() {
// 打印banner
utils.PrintBanner(conf.PROJECT_NAME)
Expand Down
9 changes: 8 additions & 1 deletion tools/gocore/template/cmd_init.got
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%: func FromCmdInit(name, pkgs, dbUpdate, initDb,initCache string, buffer *bytes.Buffer) %>
<%: func FromCmdInit(name, pkgs, dbUpdate, initDb,initCache,dbUpdateRedis string, buffer *bytes.Buffer) %>
package cmd

import (
Expand Down Expand Up @@ -26,9 +26,16 @@ func initConf() {
vt.SetBaseConfig(conf.BaseConfig)
vt.SetDataIds(conf.ProjectName, "config" <% if len(goCoreConfig.Config.CMysql) > 0 {%>, "mysql" <% }%><% if len(goCoreConfig.Config.CRedis) > 0 {%>, "redis"<% } %><%if goCoreConfig.Config.CRocketMQConfig {%>, "rocketmq"<% } %>)
// 注册配置更新回调
<% if len(goCoreConfig.Config.CMysql) > 0 {%>
vt.SetCallBackFunc(conf.ProjectName, "mysql", func(namespace, group, dataId, data string) {
<%== dbUpdate %>
})
<% } %>
<% if len(goCoreConfig.Config.CRedis) > 0 {%>
vt.SetCallBackFunc(conf.ProjectName, "redis", func(namespace, group, dataId, data string) {
<%== dbUpdateRedis %>
})
<% } %>
vt.NacosToViper()
<%}else{%>
viper.MergeConfigToToml(conf.BaseConfig)
Expand Down
21 changes: 18 additions & 3 deletions tools/gocore/template/cmd_init.got.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion tools/gocore/template/conf_base.got
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<%: func FromConfBase(buffer *bytes.Buffer) %>
<%: func FromConfBase(baseConf string,buffer *bytes.Buffer) %>
package conf

var BaseConfig = `
[network]
ApiServiceHost = "<%== goCoreConfig.HttpApis.Host %>"
ApiServicePort = "<%== goCoreConfig.HttpApis.Port %>"

<%== baseConf %>
`
6 changes: 5 additions & 1 deletion tools/gocore/template/conf_base.got.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion tools/gocore/template/conf_local.got
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<%: func FromConfLocal(env,content string,buffer *bytes.Buffer) %>
package conf

var <%== env %> = `<%== content %>`
var <%== env %> = `<%== content %>
`

3 changes: 2 additions & 1 deletion tools/gocore/template/conf_local.got.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions tools/gocore/template/model.got
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (

"<%== goCoreConfig.Service.ProjectName%>/conf"
"gorm.io/gorm"
g "github.com/sunmi-OS/gocore/v2/db/orm"
"github.com/sunmi-OS/gocore/v2/db/orm"
"github.com/sunmi-OS/gocore/v2/conf/viper"
"github.com/sunmi-OS/gocore/v2/utils"
)

func Orm() *gorm.DB {
db := g.GetORM(conf.DB<%== strings.Title(dbName) %>)
if utils.GetRunTime() != "onl" {
db := orm.GetORM(conf.DB<%== strings.Title(dbName) %>)
if viper.C.GetBool("base.debug") {
db = db.Debug()
}
return db
Expand Down
7 changes: 4 additions & 3 deletions tools/gocore/template/model.got.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 2 additions & 47 deletions tools/gocore/template/model_table.got
Original file line number Diff line number Diff line change
@@ -1,58 +1,13 @@
<%: func FromModelTable(dbName, tableStruct, tableName, fields string, buffer *bytes.Buffer) %>
package <%== dbName %>

import (
gormx "gorm.io/gorm"
)

var <%== tableStruct %>Handler = &<%== tableStruct %>{}

type <%== tableStruct %> struct {
<%== fields %>
}

func (* <%== tableStruct %>) Insert(db *gormx.DB, data * <%== tableStruct %>) error {
if db == nil {
db = Orm()
}
return db.Create(data).Error
}

func (* <%== tableStruct %>) ButchInsert(db *gormx.DB, data []* <%== tableStruct %>) error {
if db == nil {
db = Orm()
}
return db.Create(&data).Error
}

func (* <%== tableStruct %>) GetOne(where string, args ...interface{}) (* <%== tableStruct %>, error) {
var obj <%== tableStruct %>
return &obj, Orm().Where(where, args...).Take(&obj).Error
}

func (* <%== tableStruct %>) GetList(where string, args ...interface{}) ([]* <%== tableStruct %>, error) {
var list []*<%== tableStruct %>
db := Orm()
return list, db.Where(where, args...).Find(&list).Error
}

func (* <%== tableStruct %>) GetCount(where string, args ...interface{}) (int64, error) {
var number int64
err := Orm().Model(&<%== tableStruct %>{}).Where(where, args...).Count(&number).Error
return number, err
}

func (* <%== tableStruct %>) Delete(db *gormx.DB, where string, args ...interface{}) error {
if db == nil {
db = Orm()
}
return db.Where(where, args...).Delete(&<%== tableStruct %>{}).Error
func (* <%== tableStruct %>) TableName() string {
return "<%== tableName %>"
}

func (*<%== tableStruct %>) Update(db *gormx.DB, data map[string]interface{}, where string, args ...interface{}) (int64, error) {
if db == nil {
db = Orm()
}
db = db.Model(&<%== tableStruct %>{}).Where(where, args...).Updates(data)
return db.RowsAffected, db.Error
}
Loading

0 comments on commit 3be25f3

Please sign in to comment.