-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
1,027 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
bin | ||
pkg | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// @Title agentaction.go | ||
// @Description 测试数据库连接 | ||
// @Author amberhu 20210624 | ||
// @Update | ||
package agentaction | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"github.com/gin-gonic/gin" | ||
_ "github.com/go-sql-driver/mysql" //init | ||
"gopkg.in/gcfg.v1" | ||
"mygin/application/models" | ||
"net/http" | ||
) | ||
|
||
var db *sql.DB | ||
|
||
type mysqlsetting struct { | ||
//Section struct{ | ||
// Enabled bool | ||
// Path string | ||
//} | ||
Mysql struct { | ||
Host string | ||
Dbname string | ||
Username string | ||
Password string | ||
Port string | ||
} | ||
} | ||
|
||
func returnMysqlSetting() *mysqlsetting { | ||
mysql := mysqlsetting{} | ||
err := gcfg.ReadFileInto(&mysql, "src/conf/systeminfo.ini") | ||
if err != nil { | ||
fmt.Println("Failed to parse config file: %s", err) | ||
} | ||
return &mysql | ||
} | ||
func ReturnMsqlDb() *sql.DB { | ||
mysql := returnMysqlSetting() | ||
// init mysql db | ||
if err := initMySQL(mysql); err != nil { | ||
fmt.Printf("try connecting fail,err:%v\n", err) | ||
} | ||
return db | ||
} | ||
|
||
// @title initMySQL | ||
// @description 初始化数据库连接函数 | ||
// @auth amberhu 20210624 15:35 | ||
// @param mysql mysqlsetting mysql设置参数 | ||
// @return none-db sql.DB 为全局参数赋值 | ||
// @return err error 报错 | ||
func initMySQL(mysql *mysqlsetting) (err error) { | ||
dsn := mysql.Mysql.Username + ":" + mysql.Mysql.Password + "@tcp(" + mysql.Mysql.Host + ":" + mysql.Mysql.Port + ")/" + mysql.Mysql.Dbname | ||
db, err = sql.Open("mysql", dsn) | ||
if err != nil { | ||
panic(err) | ||
} | ||
err = db.Ping() | ||
if err != nil { | ||
fmt.Printf("try connecting fail,err:%v\n", err) | ||
return | ||
} | ||
//db.SetConnMaxLifetime(time.Second * 10) | ||
db.SetMaxOpenConns(200) | ||
db.SetMaxIdleConns(10) | ||
return | ||
} | ||
|
||
// @title sendinfo | ||
// @description 访问链接测试 | ||
// @auth amberhu 20210624 15:35 | ||
// @param | ||
// @return err error 报错 | ||
func Sendinfo(c *gin.Context) { | ||
// init mysql setting | ||
mysql := returnMysqlSetting() | ||
|
||
// init mysql db | ||
if err := initMySQL(mysql); err != nil { | ||
fmt.Printf("try connecting fail,err:%v\n", err) | ||
} | ||
// test query | ||
//queryRowDemo() | ||
queryMulRowDemo() | ||
|
||
//final db close | ||
defer db.Close() | ||
fmt.Println("try connecting") | ||
c.JSON(http.StatusOK, gin.H{ | ||
"message": "Hello sendinfoagent!", | ||
}) | ||
} | ||
|
||
// @title queryRowDemo | ||
// @description 单行搜索测试 | ||
// @auth amberhu 20210624 15:35 | ||
// @return err error 报错 | ||
func queryRowDemo() { | ||
|
||
sqlStr := "select id,name,age from user where id=?" | ||
var u myginuser.User | ||
err := db.QueryRow(sqlStr, 1).Scan(&u.Id, &u.Name, &u.Age) | ||
if err != nil { | ||
fmt.Printf("scan failed err:%v\n", err) | ||
return | ||
} | ||
fmt.Printf("id:%d name:%s age:%d\n", u.Id, u.Name, u.Age) | ||
} | ||
|
||
func queryMulRowDemo() { | ||
sqlStr := "select * from user" | ||
rows, err := db.Query(sqlStr) | ||
if err != nil { | ||
fmt.Printf("query faild,err:%v\n", err) | ||
return | ||
} | ||
|
||
defer rows.Close() | ||
|
||
for rows.Next() { | ||
var u myginuser.User | ||
err := rows.Scan(&u.Id, &u.Name, &u.Age) | ||
if err != nil { | ||
fmt.Printf("scan faild,err:%v\n", err) | ||
return | ||
} | ||
fmt.Printf("id:%d name:%s age:%d\n", u.Id, u.Name, u.Age) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package agentaction | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"github.com/gin-gonic/gin" | ||
_ "github.com/go-sql-driver/mysql" | ||
"mygin/application/models" | ||
"net/http" | ||
) | ||
|
||
// @title sendinfo | ||
// @description 访问链接测试 | ||
// @auth amberhu 20210624 15:35 | ||
// @param | ||
// @return err error 报错 | ||
func Sendsqlx(c *gin.Context) { | ||
//获取数据库初始化的连接对象 | ||
db_sqx := ReturnMsqlDb() | ||
queryRowDemo2(db_sqx) | ||
//fmt.Println(mysqlset) | ||
fmt.Println("try connecting") | ||
c.JSON(http.StatusOK, gin.H{ | ||
"message": "Hello sendsendsqlx!", | ||
}) | ||
} | ||
|
||
func queryRowDemo2(db *sql.DB) { | ||
|
||
sqlStr := "select id,name,age from user where id=?" | ||
var u myginuser.User | ||
err := db.QueryRow(sqlStr, 1).Scan(&u.Id, &u.Name, &u.Age) | ||
if err != nil { | ||
fmt.Printf("scan failed err:%v\n", err) | ||
return | ||
} | ||
fmt.Printf("id:%d name:%s age:%d\n", u.Id, u.Name, u.Age) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* 测试协程 | ||
* | ||
*/ | ||
package test | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
settings2 "mygin/settings" | ||
"net/http" | ||
) | ||
|
||
func Sendgo(c *gin.Context) { | ||
//go say() | ||
println("hello world ") | ||
println(settings2.SettingGlb.Redis.Host) | ||
|
||
c.JSON(http.StatusOK, gin.H{ | ||
"message": "Hello sendinfo!", | ||
}) | ||
} | ||
|
||
func say() { | ||
println("hello world via channel") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package test | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"github.com/gin-gonic/gin" | ||
"github.com/go-redis/redis" | ||
"github.com/gohouse/gorose" | ||
myginuser2 "mygin/application/models" | ||
mysql2 "mygin/dao/mysql" | ||
redis3 "mygin/dao/redis" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
func Sendredis(c *gin.Context) { | ||
|
||
//测试redis | ||
rdb := redis3.ReturnRedisDb() | ||
defer rdb.Close() | ||
//测试watch | ||
key := "watch_count" | ||
errw := rdb.Watch(func(tx *redis.Tx) error { | ||
n, err := tx.Get(key).Int() | ||
if err != nil && err != redis.Nil { | ||
fmt.Printf("try connecting fail,err:%v\n", err) | ||
return err | ||
} | ||
println(n) | ||
time.Sleep(time.Second * 10) | ||
pipe := tx.Pipeline() | ||
pipe.Set(key, n+1, 0) | ||
_, err = pipe.Exec() | ||
if err != nil { | ||
fmt.Printf("try connecting fail,err:%v\n", err) | ||
return err | ||
} | ||
|
||
println("over") | ||
return err | ||
}, key) | ||
if errw != nil { | ||
fmt.Printf("try connecting fail,err:%v\n", errw) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{ | ||
"message": "Hello sendinfo!", | ||
}) | ||
} | ||
|
||
func Testq(c *gin.Context) { | ||
queryMultiRowDemo(mysql2.ReturnMsqlDb()) | ||
queryGoroseMultiRowDemo(mysql2.ReturnMsqlGoroseConnection()) | ||
c.JSON(http.StatusOK, gin.H{ | ||
"message": "Hello sendinfo!", | ||
}) | ||
} | ||
func queryMultiRowDemo(db *sql.DB) { | ||
sqlStr := "select id, name, age from user where id > ?" | ||
rows, err := db.Query(sqlStr, 0) | ||
if err != nil { | ||
fmt.Printf("query failed, err:%v\n", err) | ||
return | ||
} | ||
// 非常重要:关闭rows释放持有的数据库链接 | ||
defer rows.Close() | ||
|
||
// 循环读取结果集中的数据 | ||
for rows.Next() { | ||
var u myginuser2.User | ||
err := rows.Scan(&u.Id, &u.Name, &u.Age) | ||
if err != nil { | ||
fmt.Printf("scan failed, err:%v\n", err) | ||
return | ||
} | ||
fmt.Printf("id:%d name:%s age:%d\n", u.Id, u.Name, u.Age) | ||
} | ||
} | ||
|
||
func queryGoroseMultiRowDemo(connection *gorose.Connection) { | ||
db := connection.NewSession() | ||
var user myginuser2.User | ||
var users []myginuser2.User | ||
err2 := db.Table(&user).Select() | ||
err2 = db.Table(&users).Limit(10).Select() | ||
if err2 != nil { | ||
fmt.Println(err2) | ||
return | ||
} | ||
fmt.Println(db.LastSql) | ||
fmt.Println(user) | ||
fmt.Println(users) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package user | ||
|
||
import ( | ||
"fmt" | ||
"github.com/gin-gonic/gin" | ||
"github.com/go-redis/redis" | ||
"math/rand" | ||
redis3 "mygin/dao/redis" | ||
"mygin/src/tools" | ||
"net/http" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
type configuration struct { | ||
Enabled bool | ||
Path string | ||
Username string | ||
Passwd string | ||
} | ||
|
||
type configuration2 struct { | ||
Section struct { | ||
Enabled bool | ||
Path string | ||
} | ||
} | ||
|
||
func Sendinfo(c *gin.Context) { | ||
//json config | ||
//file, _ := os.Open("src/conf/systeminfo.json") | ||
//defer file.Close() | ||
//decoder := json.NewDecoder(file) | ||
//conf := configuration{} | ||
//err := decoder.Decode(&conf) | ||
//if err != nil { | ||
// fmt.Println("Error:", err) | ||
//} | ||
//fmt.Println(conf) | ||
|
||
//config := configuration2{} | ||
//err := gcfg.ReadFileInto(&config, "src/conf/systeminfo.ini") | ||
//if err != nil { | ||
// fmt.Println("Failed to parse config file: %s", err) | ||
//} | ||
//fmt.Println(config.Section.Path) | ||
|
||
test := false | ||
if test { | ||
//测试二维码生成 | ||
randfinal := rand.New(rand.NewSource(time.Now().UnixNano())) | ||
randname := randfinal.Intn(1000) | ||
var url = tools.CreateQrcode(200, 200, "testinfo", strconv.Itoa(randname)) | ||
println(url) | ||
} | ||
|
||
//测试redis | ||
rdb := redis3.ReturnRedisDb() | ||
defer rdb.Close() | ||
//测试watch | ||
key := "watch_count" | ||
errw := rdb.Watch(func(tx *redis.Tx) error { | ||
n, err := tx.Get(key).Int() | ||
if err != nil && err != redis.Nil { | ||
fmt.Printf("try connecting fail,err:%v\n", err) | ||
return err | ||
} | ||
println(n) | ||
time.Sleep(time.Second * 10) | ||
pipe := tx.Pipeline() | ||
pipe.Set(key, n+1, 0) | ||
_, err = pipe.Exec() | ||
if err != nil { | ||
fmt.Printf("try connecting fail,err:%v\n", err) | ||
return err | ||
} | ||
|
||
println("over") | ||
return err | ||
}, key) | ||
if errw != nil { | ||
fmt.Printf("try connecting fail,err:%v\n", errw) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{ | ||
"message": "Hello sendinfo!", | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package myginuser | ||
|
||
type User struct { | ||
Id int | ||
Name string | ||
Age int | ||
} | ||
|
||
func (u User) TableName() string { | ||
return "user" | ||
} |
Oops, something went wrong.