2017-04-21 18:20:35 +08:00
|
|
|
|
package commands
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/url"
|
|
|
|
|
"time"
|
2017-04-30 22:13:12 +08:00
|
|
|
|
"os"
|
|
|
|
|
"encoding/gob"
|
2017-04-21 18:20:35 +08:00
|
|
|
|
|
|
|
|
|
"github.com/lifei6671/godoc/models"
|
|
|
|
|
"github.com/astaxie/beego"
|
|
|
|
|
"github.com/astaxie/beego/orm"
|
|
|
|
|
"github.com/astaxie/beego/logs"
|
2017-04-24 18:25:17 +08:00
|
|
|
|
"github.com/lifei6671/godoc/conf"
|
2017-04-30 22:13:12 +08:00
|
|
|
|
|
2017-05-01 12:15:55 +08:00
|
|
|
|
"github.com/lifei6671/gocaptcha"
|
2017-04-21 18:20:35 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// RegisterDataBase 注册数据库
|
|
|
|
|
func RegisterDataBase() {
|
|
|
|
|
host := beego.AppConfig.String("db_host")
|
|
|
|
|
database := beego.AppConfig.String("db_database")
|
|
|
|
|
username := beego.AppConfig.String("db_username")
|
|
|
|
|
password := beego.AppConfig.String("db_password")
|
|
|
|
|
timezone := beego.AppConfig.String("timezone")
|
|
|
|
|
|
|
|
|
|
port := beego.AppConfig.String("db_port")
|
|
|
|
|
|
2017-05-03 14:22:05 +08:00
|
|
|
|
dataSource := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true&loc=%s",username,password,host,port,database,url.QueryEscape(timezone))
|
2017-04-21 18:20:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
orm.RegisterDataBase("default", "mysql", dataSource)
|
|
|
|
|
|
|
|
|
|
orm.DefaultTimeLoc, _ = time.LoadLocation(timezone)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RegisterModel 注册Model
|
|
|
|
|
func RegisterModel() {
|
2017-04-24 18:25:17 +08:00
|
|
|
|
orm.RegisterModelWithPrefix(conf.GetDatabasePrefix(),
|
2017-04-21 18:20:35 +08:00
|
|
|
|
new(models.Member),
|
|
|
|
|
new(models.Book),
|
|
|
|
|
new(models.Relationship),
|
|
|
|
|
new(models.Comment),
|
|
|
|
|
new(models.Option),
|
|
|
|
|
new(models.Document),
|
2017-04-24 18:25:17 +08:00
|
|
|
|
new(models.Attachment),
|
|
|
|
|
new(models.Logger),
|
2017-05-01 20:24:43 +08:00
|
|
|
|
new(models.CommentVote),
|
2017-05-03 14:22:05 +08:00
|
|
|
|
new(models.MemberToken),
|
2017-04-21 18:20:35 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-04 10:35:56 +08:00
|
|
|
|
//初始化数据
|
2017-04-21 18:20:35 +08:00
|
|
|
|
func Initialization() {
|
|
|
|
|
|
2017-05-01 20:08:03 +08:00
|
|
|
|
options := []models.Option {
|
|
|
|
|
{ OptionName: "ENABLED_CAPTCHA", OptionValue: "false", OptionTitle:"是否启用验证码"},
|
|
|
|
|
{ OptionName: "ENABLED_REGISTER",OptionValue:"false",OptionTitle:"是否启用注册"},
|
|
|
|
|
{ OptionName: "ENABLE_ANONYMOUS" , OptionValue:"false", OptionTitle:"启用匿名访问"},
|
|
|
|
|
{ OptionName: "SITE_NAME", OptionValue:"MinDoc", OptionTitle: "站点名称"},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-05-03 15:08:35 +08:00
|
|
|
|
if err := models.NewOption().InsertMulti(options...);err != nil {
|
2017-05-04 11:40:49 +08:00
|
|
|
|
panic("Option.InsertMulti => " + err.Error())
|
2017-05-03 15:36:19 +08:00
|
|
|
|
os.Exit(1)
|
2017-05-03 15:08:35 +08:00
|
|
|
|
}
|
2017-04-21 18:20:35 +08:00
|
|
|
|
|
|
|
|
|
member := models.NewMember()
|
|
|
|
|
member.Account = "admin"
|
|
|
|
|
member.Avatar = "/static/images/headimgurl.jpg"
|
|
|
|
|
member.Password = "123456"
|
|
|
|
|
member.Role = 0
|
2017-05-03 15:36:19 +08:00
|
|
|
|
member.Email = "admin@iminho.me"
|
2017-04-21 18:20:35 +08:00
|
|
|
|
|
2017-05-03 15:08:35 +08:00
|
|
|
|
if err := member.Add();err != nil {
|
2017-05-04 11:40:49 +08:00
|
|
|
|
panic("Member.Add => " + err.Error())
|
2017-05-03 16:03:51 +08:00
|
|
|
|
os.Exit(0)
|
2017-05-03 15:08:35 +08:00
|
|
|
|
}
|
2017-05-03 14:54:03 +08:00
|
|
|
|
|
|
|
|
|
book := models.NewBook()
|
|
|
|
|
|
|
|
|
|
book.MemberId = member.MemberId
|
|
|
|
|
book.BookName = "MinDoc演示项目"
|
|
|
|
|
book.Status = 0
|
|
|
|
|
book.Description = "这是一个MinDoc演示项目,该项目是由系统初始化时自动创建。"
|
|
|
|
|
book.CommentCount = 0
|
|
|
|
|
book.PrivatelyOwned = 0
|
|
|
|
|
book.CommentStatus = "closed"
|
|
|
|
|
book.Identify = "mindoc"
|
|
|
|
|
book.DocCount = 0
|
|
|
|
|
book.CommentCount = 0
|
|
|
|
|
book.Version = time.Now().Unix()
|
|
|
|
|
book.Cover = conf.GetDefaultCover()
|
|
|
|
|
book.Editor = "markdown"
|
|
|
|
|
book.Theme = "default"
|
2017-05-03 15:08:35 +08:00
|
|
|
|
|
|
|
|
|
if err := book.Insert(); err != nil {
|
2017-05-04 11:40:49 +08:00
|
|
|
|
panic("Book.Insert => " + err.Error())
|
2017-05-03 16:03:51 +08:00
|
|
|
|
os.Exit(0)
|
2017-05-03 15:08:35 +08:00
|
|
|
|
}
|
2017-04-21 18:20:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RegisterLogger 注册日志
|
|
|
|
|
func RegisterLogger() {
|
|
|
|
|
|
2017-04-29 21:28:09 +08:00
|
|
|
|
logs.SetLogFuncCall(true)
|
2017-04-21 18:20:35 +08:00
|
|
|
|
logs.SetLogger("console")
|
|
|
|
|
logs.EnableFuncCallDepth(true)
|
|
|
|
|
logs.Async()
|
2017-04-29 21:28:09 +08:00
|
|
|
|
|
2017-05-01 16:17:26 +08:00
|
|
|
|
if _,err := os.Stat("logs/log.log"); os.IsNotExist(err) {
|
2017-05-04 10:35:56 +08:00
|
|
|
|
os.MkdirAll("./logs",0777)
|
|
|
|
|
|
2017-05-01 16:17:26 +08:00
|
|
|
|
if f,err := os.Create("logs/log.log");err == nil {
|
|
|
|
|
f.Close()
|
2017-05-01 17:17:04 +08:00
|
|
|
|
beego.SetLogger("file",`{"filename":"logs/log.log"}`)
|
2017-05-01 16:17:26 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-01 17:17:04 +08:00
|
|
|
|
|
2017-04-29 21:28:09 +08:00
|
|
|
|
beego.SetLogFuncCall(true)
|
|
|
|
|
beego.BeeLogger.Async()
|
2017-04-21 18:20:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RunCommand 注册orm命令行工具
|
|
|
|
|
func RegisterCommand() {
|
|
|
|
|
|
|
|
|
|
if _,err := os.Stat("install.lock"); os.IsNotExist(err){
|
2017-04-29 21:28:09 +08:00
|
|
|
|
err = orm.RunSyncdb("default",true,false)
|
|
|
|
|
if err == nil {
|
|
|
|
|
Initialization()
|
|
|
|
|
f, _ := os.Create("install.lock")
|
|
|
|
|
defer f.Close()
|
|
|
|
|
}else{
|
2017-05-03 16:03:51 +08:00
|
|
|
|
panic(err.Error())
|
|
|
|
|
os.Exit(0)
|
2017-04-29 21:28:09 +08:00
|
|
|
|
}
|
2017-04-21 18:20:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func RegisterFunction() {
|
|
|
|
|
beego.AddFuncMap("config",models.GetOptionValue)
|
2017-04-30 22:13:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func init() {
|
2017-05-01 12:15:55 +08:00
|
|
|
|
gocaptcha.ReadFonts("./static/fonts", ".ttf")
|
2017-04-30 22:13:12 +08:00
|
|
|
|
gob.Register(models.Member{})
|
2017-04-21 18:20:35 +08:00
|
|
|
|
}
|