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"
|
2018-01-26 17:17:38 +08:00
|
|
|
"log"
|
2017-06-02 16:08:14 +08:00
|
|
|
"flag"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
"encoding/json"
|
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-06-14 09:23:29 +08:00
|
|
|
"github.com/lifei6671/mindoc/commands/migrate"
|
|
|
|
"github.com/lifei6671/mindoc/conf"
|
|
|
|
"github.com/lifei6671/mindoc/models"
|
|
|
|
"github.com/lifei6671/mindoc/utils"
|
2017-06-02 16:08:14 +08:00
|
|
|
)
|
2017-05-27 17:53:35 +08:00
|
|
|
|
2017-04-21 18:20:35 +08:00
|
|
|
// RegisterDataBase 注册数据库
|
2017-05-11 13:39:34 +08:00
|
|
|
func RegisterDataBase() {
|
2017-05-20 09:52:18 +08:00
|
|
|
adapter := beego.AppConfig.String("db_adapter")
|
2017-04-21 18:20:35 +08:00
|
|
|
|
2017-05-20 09:52:18 +08:00
|
|
|
if adapter == "mysql" {
|
|
|
|
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")
|
2017-04-21 18:20:35 +08:00
|
|
|
|
2017-05-20 09:52:18 +08:00
|
|
|
port := beego.AppConfig.String("db_port")
|
2017-04-21 18:20:35 +08:00
|
|
|
|
2017-05-20 09:52:18 +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
|
|
|
|
2017-05-20 09:52:18 +08:00
|
|
|
orm.RegisterDataBase("default", "mysql", dataSource)
|
|
|
|
|
|
|
|
location, err := time.LoadLocation(timezone)
|
|
|
|
if err == nil {
|
|
|
|
orm.DefaultTimeLoc = location
|
|
|
|
} else {
|
2017-06-02 16:08:14 +08:00
|
|
|
log.Fatalln(err)
|
2017-05-20 09:52:18 +08:00
|
|
|
}
|
2017-06-02 16:08:14 +08:00
|
|
|
} else if adapter == "sqlite3" {
|
2017-05-20 09:52:18 +08:00
|
|
|
database := beego.AppConfig.String("db_database")
|
2018-01-26 17:17:38 +08:00
|
|
|
if strings.HasPrefix(database, "./") {
|
|
|
|
database = filepath.Join(conf.WorkingDirectory, string(database[1:]))
|
2017-06-05 12:23:01 +08:00
|
|
|
}
|
2017-06-05 13:55:07 +08:00
|
|
|
|
2017-06-02 09:27:13 +08:00
|
|
|
dbPath := filepath.Dir(database)
|
2017-06-02 16:08:14 +08:00
|
|
|
os.MkdirAll(dbPath, 0777)
|
2017-04-21 18:20:35 +08:00
|
|
|
|
2017-05-20 09:52:18 +08:00
|
|
|
orm.RegisterDataBase("default", "sqlite3", database)
|
|
|
|
}
|
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),
|
|
|
|
new(models.Option),
|
|
|
|
new(models.Document),
|
2017-04-24 18:25:17 +08:00
|
|
|
new(models.Attachment),
|
|
|
|
new(models.Logger),
|
2017-05-03 14:22:05 +08:00
|
|
|
new(models.MemberToken),
|
2017-05-19 17:20:33 +08:00
|
|
|
new(models.DocumentHistory),
|
2017-05-27 17:53:35 +08:00
|
|
|
new(models.Migration),
|
2017-07-07 16:20:55 +08:00
|
|
|
new(models.Label),
|
2017-04-21 18:20:35 +08:00
|
|
|
)
|
2017-05-27 17:53:35 +08:00
|
|
|
migrate.RegisterMigration()
|
2017-04-21 18:20:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterLogger 注册日志
|
2017-06-02 16:08:14 +08:00
|
|
|
func RegisterLogger(log string) {
|
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-06-02 16:08:14 +08:00
|
|
|
logPath := filepath.Join(log, "log.log")
|
|
|
|
|
|
|
|
if _, err := os.Stat(logPath); os.IsNotExist(err) {
|
2017-05-04 10:35:56 +08:00
|
|
|
|
2017-06-02 16:08:14 +08:00
|
|
|
os.MkdirAll(log, 0777)
|
|
|
|
|
|
|
|
if f, err := os.Create(logPath); err == nil {
|
2017-05-01 16:17:26 +08:00
|
|
|
f.Close()
|
2018-01-26 17:17:38 +08:00
|
|
|
config := make(map[string]interface{}, 1)
|
2017-06-02 16:29:31 +08:00
|
|
|
|
|
|
|
config["filename"] = logPath
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
b, _ := json.Marshal(config)
|
2017-06-02 16:29:31 +08:00
|
|
|
|
|
|
|
beego.SetLogger("file", string(b))
|
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-06-02 16:08:14 +08:00
|
|
|
if len(os.Args) >= 2 && os.Args[1] == "install" {
|
|
|
|
ResolveCommand(os.Args[2:])
|
|
|
|
Install()
|
|
|
|
} else if len(os.Args) >= 2 && os.Args[1] == "version" {
|
|
|
|
ResolveCommand(os.Args[2:])
|
|
|
|
CheckUpdate()
|
|
|
|
} else if len(os.Args) >= 2 && os.Args[1] == "migrate" {
|
|
|
|
ResolveCommand(os.Args[2:])
|
|
|
|
migrate.RunMigration()
|
|
|
|
}
|
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 {
|
2017-06-02 16:08:14 +08:00
|
|
|
cdn := beego.AppConfig.DefaultString("cdn", "")
|
2018-01-26 17:17:38 +08:00
|
|
|
if strings.HasPrefix(p, "http://") || strings.HasPrefix(p, "https://") {
|
2018-01-18 19:54:05 +08:00
|
|
|
return p
|
|
|
|
}
|
2017-06-02 16:08:14 +08:00
|
|
|
if strings.HasPrefix(p, "/") && strings.HasSuffix(cdn, "/") {
|
2017-05-12 10:45:40 +08:00
|
|
|
return cdn + string(p[1:])
|
|
|
|
}
|
2017-06-02 16:08:14 +08:00
|
|
|
if !strings.HasPrefix(p, "/") && !strings.HasSuffix(cdn, "/") {
|
2017-05-12 10:45:40 +08:00
|
|
|
return cdn + "/" + p
|
|
|
|
}
|
|
|
|
return cdn + p
|
2017-06-02 16:08:14 +08:00
|
|
|
})
|
2017-05-12 10:45:40 +08:00
|
|
|
|
|
|
|
beego.AddFuncMap("cdnjs", func(p string) string {
|
2017-06-02 16:08:14 +08:00
|
|
|
cdn := beego.AppConfig.DefaultString("cdnjs", "")
|
2018-01-26 17:17:38 +08:00
|
|
|
if strings.HasPrefix(p, "http://") || strings.HasPrefix(p, "https://") {
|
2018-01-18 19:54:05 +08:00
|
|
|
return p
|
|
|
|
}
|
2017-06-02 16:08:14 +08:00
|
|
|
if strings.HasPrefix(p, "/") && strings.HasSuffix(cdn, "/") {
|
2017-05-12 10:45:40 +08:00
|
|
|
return cdn + string(p[1:])
|
|
|
|
}
|
2017-06-02 16:08:14 +08:00
|
|
|
if !strings.HasPrefix(p, "/") && !strings.HasSuffix(cdn, "/") {
|
2017-05-12 10:45:40 +08:00
|
|
|
return cdn + "/" + p
|
|
|
|
}
|
|
|
|
return cdn + p
|
2017-06-02 16:08:14 +08:00
|
|
|
})
|
|
|
|
beego.AddFuncMap("cdncss", func(p string) string {
|
|
|
|
cdn := beego.AppConfig.DefaultString("cdncss", "")
|
2018-01-26 17:17:38 +08:00
|
|
|
if strings.HasPrefix(p, "http://") || strings.HasPrefix(p, "https://") {
|
2018-01-18 19:54:05 +08:00
|
|
|
return p
|
|
|
|
}
|
2017-06-02 16:08:14 +08:00
|
|
|
if strings.HasPrefix(p, "/") && strings.HasSuffix(cdn, "/") {
|
2017-05-12 10:45:40 +08:00
|
|
|
return cdn + string(p[1:])
|
|
|
|
}
|
2017-06-02 16:08:14 +08:00
|
|
|
if !strings.HasPrefix(p, "/") && !strings.HasSuffix(cdn, "/") {
|
2017-05-12 10:45:40 +08:00
|
|
|
return cdn + "/" + p
|
|
|
|
}
|
|
|
|
return cdn + p
|
2017-06-02 16:08:14 +08:00
|
|
|
})
|
2017-05-12 10:45:40 +08:00
|
|
|
beego.AddFuncMap("cdnimg", func(p string) string {
|
2018-01-26 17:17:38 +08:00
|
|
|
if strings.HasPrefix(p, "http://") || strings.HasPrefix(p, "https://") {
|
2018-01-18 19:54:05 +08:00
|
|
|
return p
|
|
|
|
}
|
2017-06-02 16:08:14 +08:00
|
|
|
cdn := beego.AppConfig.DefaultString("cdnimg", "")
|
|
|
|
if strings.HasPrefix(p, "/") && strings.HasSuffix(cdn, "/") {
|
2017-05-12 10:45:40 +08:00
|
|
|
return cdn + string(p[1:])
|
|
|
|
}
|
2017-06-02 16:08:14 +08:00
|
|
|
if !strings.HasPrefix(p, "/") && !strings.HasSuffix(cdn, "/") {
|
2017-05-12 10:45:40 +08:00
|
|
|
return cdn + "/" + p
|
|
|
|
}
|
|
|
|
return cdn + p
|
2017-06-02 16:08:14 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func ResolveCommand(args []string) {
|
|
|
|
flagSet := flag.NewFlagSet("MinDoc command: ", flag.ExitOnError)
|
2018-01-26 17:17:38 +08:00
|
|
|
flagSet.StringVar(&conf.ConfigurationFile, "config", "", "MinDoc configuration file.")
|
|
|
|
flagSet.StringVar(&conf.WorkingDirectory, "dir", "", "MinDoc working directory.")
|
|
|
|
flagSet.StringVar(&conf.LogFile, "log", "", "MinDoc log file path.")
|
2017-06-02 16:08:14 +08:00
|
|
|
|
|
|
|
flagSet.Parse(args)
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
if conf.WorkingDirectory == "" {
|
2017-06-02 16:08:14 +08:00
|
|
|
if p, err := filepath.Abs(os.Args[0]); err == nil {
|
2018-01-26 17:17:38 +08:00
|
|
|
conf.WorkingDirectory = filepath.Dir(p)
|
2017-06-02 16:08:14 +08:00
|
|
|
}
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
if conf.LogFile == "" {
|
|
|
|
conf.LogFile = filepath.Join(conf.WorkingDirectory, "logs")
|
2017-06-02 16:08:14 +08:00
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
if conf.ConfigurationFile == "" {
|
|
|
|
conf.ConfigurationFile = filepath.Join(conf.WorkingDirectory, "conf", "app.conf")
|
|
|
|
config := filepath.Join(conf.WorkingDirectory, "conf", "app.conf.example")
|
|
|
|
if !utils.FileExists(conf.ConfigurationFile) && utils.FileExists(config) {
|
|
|
|
utils.CopyFile(conf.ConfigurationFile, config)
|
2017-06-02 16:08:14 +08:00
|
|
|
}
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
gocaptcha.ReadFonts(filepath.Join(conf.WorkingDirectory, "static", "fonts"), ".ttf")
|
2017-06-02 16:08:14 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
err := beego.LoadAppConfig("ini", conf.ConfigurationFile)
|
2017-06-02 16:08:14 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println("An error occurred:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
uploads := filepath.Join(conf.WorkingDirectory, "uploads")
|
2017-06-02 16:08:14 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
os.MkdirAll(uploads, 0666)
|
2017-06-02 16:08:14 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
beego.BConfig.WebConfig.StaticDir["/static"] = filepath.Join(conf.WorkingDirectory, "static")
|
2017-06-02 16:08:14 +08:00
|
|
|
beego.BConfig.WebConfig.StaticDir["/uploads"] = uploads
|
2018-01-26 17:17:38 +08:00
|
|
|
beego.BConfig.WebConfig.ViewsPath = filepath.Join(conf.WorkingDirectory, "views")
|
2017-06-02 16:08:14 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
fonts := filepath.Join(conf.WorkingDirectory, "static", "fonts")
|
2017-06-02 16:08:14 +08:00
|
|
|
|
|
|
|
if !utils.FileExists(fonts) {
|
|
|
|
log.Fatal("Font path not exist.")
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
gocaptcha.ReadFonts(filepath.Join(conf.WorkingDirectory, "static", "fonts"), ".ttf")
|
2017-06-02 16:08:14 +08:00
|
|
|
|
|
|
|
RegisterDataBase()
|
|
|
|
RegisterModel()
|
2018-01-26 17:17:38 +08:00
|
|
|
RegisterLogger(conf.LogFile)
|
2017-04-30 22:13:12 +08:00
|
|
|
}
|
|
|
|
|
2017-05-11 13:39:34 +08:00
|
|
|
func init() {
|
2017-06-02 16:08:14 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
if configPath ,err := filepath.Abs(conf.ConfigurationFile); err == nil {
|
|
|
|
conf.ConfigurationFile = configPath
|
|
|
|
}
|
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-06-02 16:08:14 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
if p, err := filepath.Abs(os.Args[0]); err == nil {
|
|
|
|
conf.WorkingDirectory = filepath.Dir(p)
|
2017-06-02 16:08:14 +08:00
|
|
|
}
|
2017-05-11 13:39:34 +08:00
|
|
|
}
|