diff --git a/.gitignore b/.gitignore index 3551ef0..c3a88a4 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ # Output of the go coverage tool, specifically when used with LiteIDE *.out *.DS_Store +conf/database.conf .idea lastupdate.tmp diff --git a/conf/app.conf b/conf/app.conf index 4b3800c..c7929d0 100644 --- a/conf/app.conf +++ b/conf/app.conf @@ -1,3 +1,5 @@ appname = liteblog httpport = 8080 runmode = dev + +include "database.conf" \ No newline at end of file diff --git a/conf/database.conf.example b/conf/database.conf.example new file mode 100644 index 0000000..36fecb6 --- /dev/null +++ b/conf/database.conf.example @@ -0,0 +1,13 @@ +mysqluser = "root" +mysqlpass = "" +mysqlurls = "127.0.0.1" +mysqlport = "3306" + +[dev] +mysqldb = "lite_blog_dev" + +[pro] +mysqldb = "lite_blog_pro" + +[test] +mysqldb = "lite_blog_test" diff --git a/main.go b/main.go index 2bde032..260ae81 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,9 @@ package main import ( - _ "liteblog/routers" "github.com/astaxie/beego" + _ "liteblog/routers" + _ "liteblog/models" "strings" ) @@ -19,3 +20,5 @@ func initTemplate() { }) } + + diff --git a/models/core.go b/models/core.go new file mode 100644 index 0000000..bbed548 --- /dev/null +++ b/models/core.go @@ -0,0 +1,59 @@ +package models + +import ( + "fmt" + "github.com/astaxie/beego" + "github.com/jinzhu/gorm" + _ "github.com/jinzhu/gorm" + _ "github.com/go-sql-driver/mysql" + _ "github.com/jinzhu/gorm/dialects/mysql" +) + +func init() { + db := initDbConnect() + //SetMaxOpenConns用于设置最大打开的连接数 + //SetMaxIdleConns用于设置闲置的连接数 + db.DB().SetMaxIdleConns(10) + db.DB().SetMaxOpenConns(100) + + // 启用Logger,显示详细日志 + db.LogMode(true) + + // 自动迁移模式 + db.AutoMigrate(&User{}, + //&Model.UserDetailModel{}, + //&Model.UserAuthsModel{}, + ) + + var count int + if err := db.Model(&User{}).Count(&count).Error; err == nil && count == 0 { + //新增 + db.Create(&User{Name: "admin", + //邮箱 + Email: "admin@qq.com", + //密码 + Pwd: "123123", + //头像地址 + Avatar: "/static/images/info-img.png", + //角色 管理员 + Role: 0, + }) + } +} + +func initDbConnect() *gorm.DB { + config := beego.AppConfig + openStr := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", + config.String("mysqluser"), + config.String("mysqlpass"), + config.String("mysqlurls"), + config.String("mysqlport"), + config.String("mysqldb")) + + db, err := gorm.Open("mysql", openStr) + //存在错误,则程序退出,panic是类似于java的RuntimeException错误 + if err != nil { + panic("failed to connect database") + } + return db +} diff --git a/models/user.go b/models/user.go new file mode 100644 index 0000000..352e567 --- /dev/null +++ b/models/user.go @@ -0,0 +1,12 @@ +package models + +import "github.com/jinzhu/gorm" + +type User struct { + gorm.Model + Name string `gorm:"unique_index"` + Email string `gorm:"unique_index"` + Avatar string + Pwd string + Role int `gorm:"default:1"` // 0 管理员 1正常用户 +} \ No newline at end of file diff --git a/views/about.html b/views/about.html index 146bbf9..d740ca5 100755 --- a/views/about.html +++ b/views/about.html @@ -4,84 +4,31 @@