2017-04-21 18:20:35 +08:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2017-05-11 13:39:34 +08:00
|
|
|
"encoding/gob"
|
2017-04-21 18:20:35 +08:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2017-04-30 22:13:12 +08:00
|
|
|
"os"
|
2017-05-11 13:39:34 +08:00
|
|
|
"time"
|
2017-04-21 18:20:35 +08:00
|
|
|
|
|
|
|
"github.com/astaxie/beego"
|
|
|
|
"github.com/astaxie/beego/logs"
|
2017-05-11 13:39:34 +08:00
|
|
|
"github.com/astaxie/beego/orm"
|
2017-05-01 12:15:55 +08:00
|
|
|
"github.com/lifei6671/gocaptcha"
|
2017-05-11 13:39:34 +08:00
|
|
|
"github.com/lifei6671/godoc/conf"
|
|
|
|
"github.com/lifei6671/godoc/models"
|
2017-05-12 10:45:40 +08:00
|
|
|
"strings"
|
2017-04-21 18:20:35 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// RegisterDataBase 注册数据库
|
2017-05-11 13:39:34 +08:00
|
|
|
func RegisterDataBase() {
|
2017-04-21 18:20:35 +08:00
|
|
|
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-11 13:39:34 +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)
|
|
|
|
|
2017-05-11 13:39:34 +08:00
|
|
|
location, err := time.LoadLocation(timezone)
|
2017-05-09 18:17:08 +08:00
|
|
|
if err == nil {
|
|
|
|
orm.DefaultTimeLoc = location
|
2017-05-11 13:39:34 +08:00
|
|
|
} else {
|
2017-05-09 18:17:08 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2017-04-21 18:20:35 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterModel 注册Model
|
2017-05-11 13:39:34 +08:00
|
|
|
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),
|
2017-05-04 13:07:01 +08:00
|
|
|
//new(models.Comment),
|
2017-04-21 18:20:35 +08:00
|
|
|
new(models.Option),
|
|
|
|
new(models.Document),
|
2017-04-24 18:25:17 +08:00
|
|
|
new(models.Attachment),
|
|
|
|
new(models.Logger),
|
2017-05-04 13:07:01 +08:00
|
|
|
//new(models.CommentVote),
|
2017-05-03 14:22:05 +08:00
|
|
|
new(models.MemberToken),
|
2017-04-21 18:20:35 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterLogger 注册日志
|
2017-05-11 13:39:34 +08:00
|
|
|
func RegisterLogger() {
|
2017-04-21 18:20:35 +08:00
|
|
|
|
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-11 13:39:34 +08:00
|
|
|
if _, err := os.Stat("logs/log.log"); os.IsNotExist(err) {
|
|
|
|
os.MkdirAll("./logs", 0777)
|
2017-05-04 10:35:56 +08:00
|
|
|
|
2017-05-11 13:39:34 +08:00
|
|
|
if f, err := os.Create("logs/log.log"); err == nil {
|
2017-05-01 16:17:26 +08:00
|
|
|
f.Close()
|
2017-05-11 13:39:34 +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() {
|
|
|
|
|
2017-05-11 13:39:34 +08:00
|
|
|
Install()
|
|
|
|
Update()
|
2017-05-05 18:02:52 +08:00
|
|
|
CheckUpdate()
|
2017-04-21 18:20:35 +08:00
|
|
|
}
|
|
|
|
|
2017-05-11 13:39:34 +08:00
|
|
|
func RegisterFunction() {
|
|
|
|
beego.AddFuncMap("config", models.GetOptionValue)
|
2017-05-12 10:45:40 +08:00
|
|
|
|
|
|
|
beego.AddFuncMap("cdn", func(p string) string {
|
|
|
|
cdn := beego.AppConfig.DefaultString("cdn","")
|
|
|
|
if strings.HasPrefix(p,"/") && strings.HasSuffix(cdn,"/"){
|
|
|
|
return cdn + string(p[1:])
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(p,"/") && !strings.HasSuffix(cdn,"/"){
|
|
|
|
return cdn + "/" + p
|
|
|
|
}
|
|
|
|
return cdn + p
|
|
|
|
});
|
|
|
|
|
|
|
|
beego.AddFuncMap("cdnjs", func(p string) string {
|
|
|
|
cdn := beego.AppConfig.DefaultString("cdnjs","")
|
|
|
|
if strings.HasPrefix(p,"/") && strings.HasSuffix(cdn,"/"){
|
|
|
|
return cdn + string(p[1:])
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(p,"/") && !strings.HasSuffix(cdn,"/"){
|
|
|
|
return cdn + "/" + p
|
|
|
|
}
|
|
|
|
return cdn + p
|
|
|
|
});
|
|
|
|
beego.AddFuncMap("cdncss", func(p string) string {
|
|
|
|
cdn := beego.AppConfig.DefaultString("cdncss","")
|
|
|
|
if strings.HasPrefix(p,"/") && strings.HasSuffix(cdn,"/"){
|
|
|
|
return cdn + string(p[1:])
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(p,"/") && !strings.HasSuffix(cdn,"/"){
|
|
|
|
return cdn + "/" + p
|
|
|
|
}
|
|
|
|
return cdn + p
|
|
|
|
});
|
|
|
|
beego.AddFuncMap("cdnimg", func(p string) string {
|
|
|
|
cdn := beego.AppConfig.DefaultString("cdnimg","")
|
|
|
|
if strings.HasPrefix(p,"/") && strings.HasSuffix(cdn,"/"){
|
|
|
|
return cdn + string(p[1:])
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(p,"/") && !strings.HasSuffix(cdn,"/"){
|
|
|
|
return cdn + "/" + p
|
|
|
|
}
|
|
|
|
return cdn + p
|
|
|
|
});
|
2017-04-30 22:13:12 +08:00
|
|
|
}
|
|
|
|
|
2017-05-11 13:39:34 +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-05-11 13:39:34 +08:00
|
|
|
}
|