master
Could not load branches
Nothing to show
Could not load tags
Nothing to show
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
1
branch
0
tags
Code
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
This branch is up to date with mydevc/go-gin-mvc:master.
Latest commit
Git stats
Files
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
// 建立连接池
RedisPool = &redis.Pool{
MaxIdle: max_idle,
MaxActive: max_active,
IdleTimeout: idle_timeout,
Wait: true,
Dial: func() (redis.Conn, error) {
con, err := redis.Dial("tcp", host,
//redis.DialPassword(conf["Password"].(string)),
redis.DialDatabase(database),
redis.DialConnectTimeout(timeout),
redis.DialReadTimeout(timeout),
redis.DialWriteTimeout(timeout))
if err != nil {
return nil, err
}
return con, nil
},
}
//Redis测试
func RedisSetAction(ctx *gin.Context) {
rds := utils.RedisPool.Get();
count, _ := redis.Int(rds.Do("GET", "count"))
count++
rds.Do("SET", "count", count)
ctx.JSON(200, gin.H{
"message": count,
})
}
//不用连接池
//store,err:=redis.NewStore(10,"tcp","rs1.baidu.com:6379","",[]byte("asfajfa;lskjr2"))
//使用连接池
store, err := redis.NewStoreWithPool(utils.RedisPool, []byte("as&8(0fajfa;lskjr2"))
store, err := redis.NewStoreWithPool(utils.RedisPool, []byte("as&8(0fajfa;lskjr2"))
store.Options(sessions.Options{
"/",
domain,
maxage,
false, //https 时使用
true, //true:JS脚本无法获取cookie信息
})
if err != nil {
// Handle the error. Probably bail out if we can't connect.
fmt.Println("redis.NewStore error")
}
Router.Use(sessions.Sessions(session_name, store))
//Sesssion测试
func SessionAction(ctx *gin.Context) {
session := sessions.Default(ctx)
var count int
v := session.Get("count")
if v == nil {
count = 0
} else {
count = v.(int)
count += 1
}
session.Set("count", count)
session.Save()
ctx.JSON(200, gin.H{"count": count})
}
//从库添加
slaves := utils.Config.Section("mysql_slave").Keys()
for _, s_dsn := range slaves {
_dbs, err := xorm.NewEngine("mysql", s_dsn.String())
_dbs.SetMaxIdleConns(10)
_dbs.SetMaxOpenConns(200)
_dbs.ShowSQL(true)
_dbs.ShowExecTime(true)
if err!=nil {
fmt.Println(err)
}else{
dbs = append(dbs, _dbs)
}
}
subscriber := new(jobs.Subscribe)
forever := make(chan bool)
q := queue.NewQueue()
//队列执行的任务需要注册方可执行
q.PushJob("Dosome",jobs.HandlerFunc(subscriber.Dosome))
//q.PushJob("Fusome",jobs.HandlerFunc(subscriber.Fusome))
//提前规划好队列,可按延时时间来划分。可多个任务由一个队列来执行,也可以一个任务一个队列,一个队列可启动多个消费者
go q.NewShareQueue("SomeQueue")
//go q.NewShareQueue("SomeQueue")
//go q.NewShareQueue("SomeQueue")
defer q.Close()
<-forever
forever := make(chan bool)
go func() {
for i := 0; i < 1000000; i++ {
queue.NewSender("SomeQueue", "Dosome", jobs.Subscribe{Name: "We are doing..." + strconv.Itoa(i)}).Send()
}
}()
defer queue.SendConn.Close()
<-forever
//队列生产者测试
func QueueAction(ctx *gin.Context) {
queue.NewSender("SomeQueue", "Dosome", jobs.Subscribe{Name: "We are doing..."}).Send()
}
Router.Use(csrf.Middleware(csrf.Options{
Secret: csrfscret,
ErrorFunc: func(c *gin.Context) {
c.String(400, "CSRF token mismatch")
c.Abort()
},
}))
fn := c.HandlerName();
fn = fn[strings.LastIndex(fn, "/"):]
for _, action := range IgnoreAction {
if (strings.Contains(fn, action)) {
fmt.Println(action)
c.Next()
return
}
}
//参数检验
rules := govalidator.MapData{
"name": []string{"required", "between:3,8"},
"age": []string{"digits:11"},
}
messages := govalidator.MapData{
"name": []string{"required:用户名不能为空", "between:3到8位"},
"age": []string{"digits:手机号码为11位数字"},
}
opts := govalidator.Options{
Request: ctx.Request, // request object
Rules: rules, // rules map
Messages: messages, // custom message map (Optional)
RequiredDefault: false, // all the field to be pass the rules
}
v := govalidator.New(opts)
e := v.Validate()
//校验结果判断
if len(e)>0 {
ctx.JSON(200, e)
return
}
RootPath="/Users/baidu/data/golang/gopath/src/go-gin-mvc"
var err error
Config, err = ini.Load(RootPath+"/conf/config.ini");
if err != nil {
fmt.Printf("Fail to read file: %v", err)
os.Exit(1)
}
//从库添加
slaves := utils.Config.Section("mysql_slave").Keys()
for _, s_dsn := range slaves {
_dbs, err := xorm.NewEngine("mysql", s_dsn.String())
...
}
//定时程序启动
c := cron.New()
//数据库状态检查
c.AddFunc("*/600 * * * * *", models.DbCheck)
c.Start()
go get github.com/go-xorm/cmd/xorm
cloud.google.com/go/civil
golang.org/x/crypto/md4
cloud.google.com/go/civil
golang.org/x/crypto/md4
go build
xorm reverse mysql "root:12345678@tcp(dbm1.baidu.com:3306)/test?charset=utf8" .