Skip to content

Commit

Permalink
Merge pull request #15 from aberic/master
Browse files Browse the repository at this point in the history
SQL support
  • Loading branch information
aberic committed May 13, 2019
2 parents 113198e + e5579b1 commit 0094cba
Show file tree
Hide file tree
Showing 46 changed files with 7,354 additions and 7 deletions.
15 changes: 13 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
PKGSWITHOUTEXAMPLES := $(shell go list ./... | grep -v 'examples/')
PKGSWITHOUTEXAMPLES := $(shell go list ./... | grep -v 'examples/\|utils/')
GO_FILES := $(shell find . -name "*.go" -not -path "./vendor/*" -not -path ".git/*" -print0 | xargs -0)

export SQLName=some-mysql
export DBUrl=localhost:3306
export DBUser=root
export DBPass=my-secret-pw
export DBName=1

checkTravis: start overalls vet lint misspell staticcheck cyclo const veralls test end

checkCircle: start overalls vet lint misspell staticcheck cyclo const test end

checkLocal: start overalls vet lint misspell staticcheck cyclo const end

start: wright consul
start: wright mysql consul

end:
@echo "end"
rm -rf a.txt
docker rm -f ${SQLName}
consul leave

mysql:
@echo "docker run mysql"
docker run --name ${SQLName} -e MYSQL_ROOT_PASSWORD=${DBPass} -d -p 3306:3306 mysql:5.7.26

consul:
@echo "consul"
nohup consul agent -dev &
Expand Down
2 changes: 1 addition & 1 deletion examples/bow1/bow1.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

func main() {
rivet.Initialize(false, true, false)
rivet.UserBow(func(context *gin.Context, result *response.Result) bool {
rivet.UseBow(func(context *gin.Context, result *response.Result) bool {
result.Fail("test fail")
return false
})
Expand Down
2 changes: 1 addition & 1 deletion examples/bow2/bow2.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

func main() {
rivet.Initialize(false, true, false)
rivet.UserBow(func(context *gin.Context, result *response.Result) bool {
rivet.UseBow(func(context *gin.Context, result *response.Result) bool {
return true
})
rivet.Bow().Add(
Expand Down
4 changes: 2 additions & 2 deletions rivet/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ func Initialize(healthCheck bool, serverManager bool, loadBalance bool) {
request.LB = loadBalance
}

// UserBow 开启网关路由
// UseBow 开启网关路由
//
// filter 自定义过滤方案
func UserBow(filter func(context *gin.Context, result *response.Result) bool) {
func UseBow(filter func(context *gin.Context, result *response.Result) bool) {
route = true
f = filter
}
Expand Down
10 changes: 9 additions & 1 deletion utils/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ const (
// DiscoveryURL 当前服务注册的发现服务地址
DiscoveryURL = "DISCOVERY_URL"
// GOPath Go工作路径
GOPath = "GOPATH"
GOPath = "GO_PATH"
// DBUrl 数据库 URL
DBUrl = "DB_URL"
// DBName 数据库名称
DBName = "DB_NAME"
// DBUser 数据库用户名
DBUser = "DB_USER"
// DBPass 数据库用户密码
DBPass = "DB_PASS"
)

// GetEnv 获取环境变量 envName 的值
Expand Down
3 changes: 3 additions & 0 deletions utils/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ var (
Trans, _ = zap.NewDevelopment()
// Scheduled 定时任务日志对象
Scheduled, _ = zap.NewDevelopment()
// SQL 数据库日志对象
SQL, _ = zap.NewDevelopment()
)

const (
Expand Down Expand Up @@ -84,6 +86,7 @@ func GetLogInstance() *Logger {
Shunt = instance.New("./logs/shunt.log", "shunt")
Trans = instance.New("./logs/trans.log", "trans")
Scheduled = instance.New("./logs/scheduled.log", "scheduled")
SQL = instance.New("./logs/sql.log", "sql")
})
return instance
}
Expand Down
126 changes: 126 additions & 0 deletions utils/sql/sql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright (c) 2019. ENNOO - All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package sql

import (
"github.com/ennoo/rivet/utils/env"
"github.com/ennoo/rivet/utils/log"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"github.com/robfig/cron"
"strings"
"sync"
"time"
)

var (
instance *SQL
once sync.Once
)

// SQL sql 连接对象
type SQL struct {
DB *gorm.DB // 数据库任务入口
DBUrl string // dbURL 数据库 URL
DBUser string // dbUser 数据库用户名
DBPass string // dbPass 数据库用户密码
DBName string // dbName 数据库名称
}

// GetSQLInstance 获取 SQL 单例
func GetSQLInstance() *SQL {
once.Do(func() {
instance = &SQL{}
})
return instance
}

// Connect 链接数据库服务
func (s *SQL) Connect(dbURL, dbUser, dbPass, dbName string) error {
if nil == s.DB {
s.DBUrl = env.GetEnvDefault(env.DBUrl, dbURL)
s.DBUser = env.GetEnvDefault(env.DBUser, dbUser)
s.DBPass = env.GetEnvDefault(env.DBPass, dbPass)
s.DBName = env.GetEnvDefault(env.DBName, dbName)
log.SQL.Info("init DB Manager")
dbValue := strings.Join([]string{s.DBUser, ":", s.DBPass, "@tcp(", s.DBUrl, ")/", s.DBName,
"?charset=utf8&parseTime=True&loc=Local"}, "")
log.SQL.Debug("dbValue = " + dbValue)
var err error
s.DB, err = gorm.Open("mysql", dbValue)
if err != nil {
log.SQL.Error("failed to connect database, err = " + err.Error())
return err
}
s.DB.LogMode(false)
// SetMaxIdleConns sets the maximum number of connections in the idle connection pool.
s.DB.DB().SetMaxIdleConns(10)
// SetMaxOpenConns sets the maximum number of open connections to the database.
s.DB.DB().SetMaxOpenConns(100)
// SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
s.DB.DB().SetConnMaxLifetime(time.Hour)
go s.dbKeepAlive(s.DB)
}
return nil
}

func (s *SQL) reConnect() error {
return s.Connect(s.DBUrl, s.DBUser, s.DBPass, s.DBName)
}

// Exec 执行自定义 SQL
func (s *SQL) Exec(f func(db *gorm.DB)) error {
if nil == s.DB {
if err := s.reConnect(); nil == err {
f(s.DB)
} else {
return err
}
}
f(s.DB)
return nil
}

// ExecSQL 执行自定义 SQL 语句,该方法是对 func Exec(f func(db *gorm.DB)) error 的实现
//
// dest 期望通过该过程赋值的对象
//
// sql 即将执行的 SQL 语句,可以包含 "?" 来做通配符
//
// values 上述 SQL 语句中 "?" 通配符所表达的值
//
// eg:在 db_user 表中根据用户编号和年龄查询用户基本信息,如下所示:
//
// ExecSQL(&user, "select id,name,age from db_user where id=? and age=?", 1, 18)
func (s *SQL) ExecSQL(dest interface{}, sql string, values ...interface{}) {
s.DB.Raw(Format(sql), values).Scan(dest)
}

// Format SQL 格式化
func Format(elem ...string) string {
return strings.Join(elem, " ")
}

func (s *SQL) dbKeepAlive(db *gorm.DB) {
c := cron.New()
_ = c.AddFunc("*/10 * * * * ?", func() {
err := db.DB().Ping()
if nil != err {
_ = s.Exec(func(db *gorm.DB) {})
}
}) //每10秒执行一次
c.Start()
select {} //阻塞主线程不退出
}
52 changes: 52 additions & 0 deletions utils/sql/sql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2019. ENNOO - All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package sql

import (
"github.com/ennoo/rivet/utils/log"
"github.com/jinzhu/gorm"
"testing"
)

type User struct {
Host string `gorm:"column:Host"`
User string `gorm:"column:User"`
}

func TestSQL(t *testing.T) {
db := GetSQLInstance()
_ = db.Connect("127.0.0.1:3306", "root", "my-secret-pw", "mysql")

_ = db.Connect("127.0.0.1:3306", "root", "my-secret-pw", "mysql")
log.SQL.Info("dbURL = " + db.DBUrl)
var user User
db.ExecSQL(&user, "select * from user where User=? limit 1", "root")
log.SQL.Info("user Host = " + user.Host + " User = " + user.User)

db.DB = nil
_ = db.reConnect()
db.ExecSQL(&user, "select * from user where User=? limit 1", "mysql.sys")
db.DB = nil
_ = db.Exec(func(db *gorm.DB) {
db.Raw(Format(
"select * from", "user", "where User=? limit 1"), "mysql.session").Scan(&user)
})
_ = db.Exec(func(db *gorm.DB) {
db.Raw(Format(
"select * from", "user", "where User=? limit 1"), "root").Scan(&user)
})
log.SQL.Info("user Host = " + user.Host + " User = " + user.User)
}
21 changes: 21 additions & 0 deletions vendor/github.com/jinzhu/gorm/License

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

40 changes: 40 additions & 0 deletions vendor/github.com/jinzhu/gorm/README.md

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

0 comments on commit 0094cba

Please sign in to comment.