2017-04-21 18:20:35 +08:00
|
|
|
|
package commands
|
|
|
|
|
|
|
|
|
|
import (
|
2017-05-11 13:39:34 +08:00
|
|
|
|
"encoding/gob"
|
2018-03-24 17:24:02 +08:00
|
|
|
|
"flag"
|
2017-04-21 18:20:35 +08:00
|
|
|
|
"fmt"
|
2018-03-24 17:24:02 +08:00
|
|
|
|
"log"
|
2017-04-21 18:20:35 +08:00
|
|
|
|
"net/url"
|
2017-04-30 22:13:12 +08:00
|
|
|
|
"os"
|
2017-06-02 16:08:14 +08:00
|
|
|
|
"path/filepath"
|
2021-08-08 11:15:46 +08:00
|
|
|
|
"strconv"
|
2017-06-02 16:08:14 +08:00
|
|
|
|
"strings"
|
2018-03-24 17:24:02 +08:00
|
|
|
|
"time"
|
2022-05-07 18:06:46 +08:00
|
|
|
|
_ "time/tzdata"
|
2017-06-02 16:08:14 +08:00
|
|
|
|
|
2019-02-27 17:55:18 +08:00
|
|
|
|
"bytes"
|
2018-01-26 17:17:38 +08:00
|
|
|
|
"encoding/json"
|
2021-03-23 15:09:17 +08:00
|
|
|
|
"net/http"
|
|
|
|
|
|
2021-03-23 21:55:50 +08:00
|
|
|
|
beegoCache "github.com/beego/beego/v2/client/cache"
|
|
|
|
|
_ "github.com/beego/beego/v2/client/cache/memcache"
|
|
|
|
|
"github.com/beego/beego/v2/client/cache/redis"
|
2021-03-26 11:34:02 +08:00
|
|
|
|
"github.com/beego/beego/v2/client/orm"
|
2021-03-23 21:55:50 +08:00
|
|
|
|
"github.com/beego/beego/v2/core/logs"
|
|
|
|
|
"github.com/beego/beego/v2/server/web"
|
2021-03-29 18:00:24 +08:00
|
|
|
|
"github.com/beego/i18n"
|
2019-02-27 17:55:18 +08:00
|
|
|
|
"github.com/howeyc/fsnotify"
|
2023-04-14 11:09:53 +08:00
|
|
|
|
_ "github.com/lib/pq"
|
2017-05-01 12:15:55 +08:00
|
|
|
|
"github.com/lifei6671/gocaptcha"
|
2021-03-23 15:09:17 +08:00
|
|
|
|
"github.com/mindoc-org/mindoc/cache"
|
|
|
|
|
"github.com/mindoc-org/mindoc/conf"
|
|
|
|
|
"github.com/mindoc-org/mindoc/models"
|
|
|
|
|
"github.com/mindoc-org/mindoc/utils/filetil"
|
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() {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Info("正在初始化数据库配置.")
|
2021-03-25 11:49:39 +08:00
|
|
|
|
dbadapter, _ := web.AppConfig.String("db_adapter")
|
2018-09-17 17:07:58 +08:00
|
|
|
|
orm.DefaultTimeLoc = time.Local
|
2021-03-16 12:45:12 +08:00
|
|
|
|
orm.DefaultRowsLimit = -1
|
2017-04-21 18:20:35 +08:00
|
|
|
|
|
2021-03-23 21:55:50 +08:00
|
|
|
|
if strings.EqualFold(dbadapter, "mysql") {
|
2021-03-25 11:49:39 +08:00
|
|
|
|
host, _ := web.AppConfig.String("db_host")
|
2021-03-25 10:42:18 +08:00
|
|
|
|
database, _ := web.AppConfig.String("db_database")
|
2021-03-25 11:49:39 +08:00
|
|
|
|
username, _ := web.AppConfig.String("db_username")
|
|
|
|
|
password, _ := web.AppConfig.String("db_password")
|
2018-03-22 16:42:34 +08:00
|
|
|
|
|
2021-03-25 11:49:39 +08:00
|
|
|
|
timezone, _ := web.AppConfig.String("timezone")
|
2018-03-23 17:50:10 +08:00
|
|
|
|
location, err := time.LoadLocation(timezone)
|
|
|
|
|
if err == nil {
|
|
|
|
|
orm.DefaultTimeLoc = location
|
|
|
|
|
} else {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("加载时区配置信息失败,请检查是否存在 ZONEINFO 环境变量->", err)
|
2018-03-23 17:50:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-25 11:49:39 +08:00
|
|
|
|
port, _ := web.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
|
|
|
|
|
2018-03-23 17:50:10 +08:00
|
|
|
|
if err := orm.RegisterDataBase("default", "mysql", dataSource); err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("注册默认数据库失败->", err)
|
2018-02-27 17:20:42 +08:00
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
2018-09-19 11:32:29 +08:00
|
|
|
|
|
2021-03-23 21:55:50 +08:00
|
|
|
|
} else if strings.EqualFold(dbadapter, "sqlite3") {
|
2018-09-17 17:07:58 +08:00
|
|
|
|
|
2021-03-25 11:49:39 +08:00
|
|
|
|
database, _ := web.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
|
|
|
|
}
|
2018-11-05 18:50:01 +08:00
|
|
|
|
if p, err := filepath.Abs(database); err == nil {
|
2018-08-24 15:05:15 +08:00
|
|
|
|
database = p
|
|
|
|
|
}
|
2017-06-05 13:55:07 +08:00
|
|
|
|
|
2017-06-02 09:27:13 +08:00
|
|
|
|
dbPath := filepath.Dir(database)
|
2018-08-24 15:05:15 +08:00
|
|
|
|
|
2018-11-05 18:50:01 +08:00
|
|
|
|
if _, err := os.Stat(dbPath); err != nil && os.IsNotExist(err) {
|
2019-05-20 12:08:14 +08:00
|
|
|
|
_ = os.MkdirAll(dbPath, 0777)
|
2018-08-24 15:05:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-27 17:20:42 +08:00
|
|
|
|
err := orm.RegisterDataBase("default", "sqlite3", database)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("注册默认数据库失败->", err)
|
2018-02-27 17:20:42 +08:00
|
|
|
|
}
|
2023-04-14 11:09:53 +08:00
|
|
|
|
} else if strings.EqualFold(dbadapter, "postgres") {
|
|
|
|
|
host, _ := web.AppConfig.String("db_host")
|
|
|
|
|
database, _ := web.AppConfig.String("db_database")
|
|
|
|
|
username, _ := web.AppConfig.String("db_username")
|
|
|
|
|
password, _ := web.AppConfig.String("db_password")
|
|
|
|
|
sslmode, _ := web.AppConfig.String("db_sslmode")
|
|
|
|
|
|
|
|
|
|
timezone, _ := web.AppConfig.String("timezone")
|
|
|
|
|
location, err := time.LoadLocation(timezone)
|
|
|
|
|
if err == nil {
|
|
|
|
|
orm.DefaultTimeLoc = location
|
|
|
|
|
} else {
|
|
|
|
|
logs.Error("加载时区配置信息失败,请检查是否存在 ZONEINFO 环境变量->", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
port, _ := web.AppConfig.String("db_port")
|
|
|
|
|
|
|
|
|
|
dataSource := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=%s", username, password, host, port, database, sslmode)
|
|
|
|
|
|
|
|
|
|
if err := orm.RegisterDataBase("default", "postgres", dataSource); err != nil {
|
|
|
|
|
logs.Error("注册默认数据库失败->", err)
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-24 17:24:02 +08:00
|
|
|
|
} else {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("不支持的数据库类型.")
|
2018-02-27 17:20:42 +08:00
|
|
|
|
os.Exit(1)
|
2017-05-20 09:52:18 +08:00
|
|
|
|
}
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Info("数据库初始化完成.")
|
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),
|
2018-07-13 19:04:51 +08:00
|
|
|
|
new(models.Blog),
|
2018-08-13 19:05:49 +08:00
|
|
|
|
new(models.Template),
|
2018-11-05 18:50:01 +08:00
|
|
|
|
new(models.Team),
|
|
|
|
|
new(models.TeamMember),
|
|
|
|
|
new(models.TeamRelationship),
|
2018-11-20 20:36:14 +08:00
|
|
|
|
new(models.Itemsets),
|
2023-03-20 14:30:27 +08:00
|
|
|
|
new(models.Comment),
|
|
|
|
|
new(models.WorkWeixinAccount),
|
2023-04-20 13:24:28 +08:00
|
|
|
|
new(models.DingTalkAccount),
|
2017-04-21 18:20:35 +08:00
|
|
|
|
)
|
2018-07-25 14:46:56 +08:00
|
|
|
|
gob.Register(models.Blog{})
|
|
|
|
|
gob.Register(models.Document{})
|
2018-08-13 19:05:49 +08:00
|
|
|
|
gob.Register(models.Template{})
|
2018-02-28 16:29:26 +08:00
|
|
|
|
//migrate.RegisterMigration()
|
2024-07-08 15:41:56 +08:00
|
|
|
|
err := orm.RunSyncdb("default", false, true)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logs.Error("注册Model失败 ->", err)
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
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
|
|
|
|
|
2023-08-23 10:22:02 +08:00
|
|
|
|
logs.Reset()
|
2017-04-29 21:28:09 +08:00
|
|
|
|
logs.SetLogFuncCall(true)
|
2019-05-20 12:08:14 +08:00
|
|
|
|
_ = logs.SetLogger("console")
|
2017-04-21 18:20:35 +08:00
|
|
|
|
logs.EnableFuncCallDepth(true)
|
2017-04-29 21:28:09 +08:00
|
|
|
|
|
2021-03-25 10:42:18 +08:00
|
|
|
|
if web.AppConfig.DefaultBool("log_is_async", true) {
|
2018-07-25 14:46:56 +08:00
|
|
|
|
logs.Async(1e3)
|
|
|
|
|
}
|
|
|
|
|
if log == "" {
|
2021-03-25 10:42:18 +08:00
|
|
|
|
logPath, err := filepath.Abs(web.AppConfig.DefaultString("log_path", conf.WorkingDir("runtime", "logs")))
|
2018-08-13 15:04:52 +08:00
|
|
|
|
if err == nil {
|
|
|
|
|
log = logPath
|
2018-11-05 18:50:01 +08:00
|
|
|
|
} else {
|
|
|
|
|
log = conf.WorkingDir("runtime", "logs")
|
2018-08-13 15:04:52 +08:00
|
|
|
|
}
|
2018-07-25 14:46:56 +08:00
|
|
|
|
}
|
2017-06-02 16:08:14 +08:00
|
|
|
|
|
2018-07-25 14:46:56 +08:00
|
|
|
|
logPath := filepath.Join(log, "log.log")
|
2017-05-04 10:35:56 +08:00
|
|
|
|
|
2018-07-25 14:46:56 +08:00
|
|
|
|
if _, err := os.Stat(log); os.IsNotExist(err) {
|
2019-05-20 12:08:14 +08:00
|
|
|
|
_ = os.MkdirAll(log, 0755)
|
2018-07-25 14:46:56 +08:00
|
|
|
|
}
|
2017-06-02 16:08:14 +08:00
|
|
|
|
|
2018-07-25 14:46:56 +08:00
|
|
|
|
config := make(map[string]interface{}, 1)
|
2017-06-02 16:29:31 +08:00
|
|
|
|
|
2018-07-25 14:46:56 +08:00
|
|
|
|
config["filename"] = logPath
|
|
|
|
|
config["perm"] = "0755"
|
|
|
|
|
config["rotate"] = true
|
2017-06-02 16:29:31 +08:00
|
|
|
|
|
2021-03-25 10:42:18 +08:00
|
|
|
|
if maxLines := web.AppConfig.DefaultInt("log_maxlines", 1000000); maxLines > 0 {
|
2018-07-25 14:46:56 +08:00
|
|
|
|
config["maxLines"] = maxLines
|
|
|
|
|
}
|
2021-03-25 10:42:18 +08:00
|
|
|
|
if maxSize := web.AppConfig.DefaultInt("log_maxsize", 1<<28); maxSize > 0 {
|
2018-07-25 14:46:56 +08:00
|
|
|
|
config["maxsize"] = maxSize
|
|
|
|
|
}
|
2021-03-25 10:42:18 +08:00
|
|
|
|
if !web.AppConfig.DefaultBool("log_daily", true) {
|
2018-07-25 14:46:56 +08:00
|
|
|
|
config["daily"] = false
|
|
|
|
|
}
|
2021-03-25 10:42:18 +08:00
|
|
|
|
if maxDays := web.AppConfig.DefaultInt("log_maxdays", 7); maxDays > 0 {
|
2018-07-25 14:46:56 +08:00
|
|
|
|
config["maxdays"] = maxDays
|
|
|
|
|
}
|
2021-03-25 10:42:18 +08:00
|
|
|
|
if level := web.AppConfig.DefaultString("log_level", "Trace"); level != "" {
|
2018-07-25 14:46:56 +08:00
|
|
|
|
switch level {
|
|
|
|
|
case "Emergency":
|
2021-03-30 16:18:02 +08:00
|
|
|
|
config["level"] = logs.LevelEmergency
|
2018-07-25 14:46:56 +08:00
|
|
|
|
case "Alert":
|
2021-03-30 16:18:02 +08:00
|
|
|
|
config["level"] = logs.LevelAlert
|
2018-07-25 14:46:56 +08:00
|
|
|
|
case "Critical":
|
2021-03-30 16:18:02 +08:00
|
|
|
|
config["level"] = logs.LevelCritical
|
2018-07-25 14:46:56 +08:00
|
|
|
|
case "Error":
|
2021-03-30 16:18:02 +08:00
|
|
|
|
config["level"] = logs.LevelError
|
2018-07-25 14:46:56 +08:00
|
|
|
|
case "Warning":
|
2021-03-30 16:18:02 +08:00
|
|
|
|
config["level"] = logs.LevelWarning
|
2018-07-25 14:46:56 +08:00
|
|
|
|
case "Notice":
|
2021-03-30 16:18:02 +08:00
|
|
|
|
config["level"] = logs.LevelNotice
|
2018-07-25 14:46:56 +08:00
|
|
|
|
case "Informational":
|
2021-03-30 16:18:02 +08:00
|
|
|
|
config["level"] = logs.LevelInformational
|
2018-07-25 14:46:56 +08:00
|
|
|
|
case "Debug":
|
2021-03-30 16:18:02 +08:00
|
|
|
|
config["level"] = logs.LevelDebug
|
2017-05-01 16:17:26 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-20 12:08:14 +08:00
|
|
|
|
b, err := json.Marshal(config)
|
2018-11-05 18:50:01 +08:00
|
|
|
|
if err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("初始化文件日志时出错 ->", err)
|
2021-03-30 16:18:02 +08:00
|
|
|
|
_ = logs.SetLogger("file", `{"filename":"`+logPath+`"}`)
|
2018-11-05 18:50:01 +08:00
|
|
|
|
} else {
|
2021-03-30 16:18:02 +08:00
|
|
|
|
_ = logs.SetLogger(logs.AdapterFile, string(b))
|
2018-07-25 14:46:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 16:18:02 +08:00
|
|
|
|
logs.SetLogFuncCall(true)
|
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" {
|
|
|
|
|
CheckUpdate()
|
2018-02-28 16:29:26 +08:00
|
|
|
|
os.Exit(0)
|
2024-07-11 12:39:15 +08:00
|
|
|
|
} else if len(os.Args) >= 2 && os.Args[1] == "update" {
|
|
|
|
|
Update()
|
|
|
|
|
os.Exit(0)
|
2017-06-02 16:08:14 +08:00
|
|
|
|
}
|
2018-07-11 18:06:02 +08:00
|
|
|
|
|
2017-04-21 18:20:35 +08:00
|
|
|
|
}
|
2018-03-13 14:14:56 +08:00
|
|
|
|
|
2023-03-20 14:30:27 +08:00
|
|
|
|
// 注册模板函数
|
2017-05-11 13:39:34 +08:00
|
|
|
|
func RegisterFunction() {
|
2021-03-26 15:37:19 +08:00
|
|
|
|
err := web.AddFuncMap("config", models.GetOptionValue)
|
2017-05-12 10:45:40 +08:00
|
|
|
|
|
2019-02-27 17:55:18 +08:00
|
|
|
|
if err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("注册函数 config 出错 ->", err)
|
2019-02-27 17:55:18 +08:00
|
|
|
|
os.Exit(-1)
|
|
|
|
|
}
|
2021-03-26 15:37:19 +08:00
|
|
|
|
err = web.AddFuncMap("cdn", func(p string) string {
|
2021-03-25 10:42:18 +08:00
|
|
|
|
cdn := web.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
|
|
|
|
|
}
|
2018-03-13 14:14:56 +08:00
|
|
|
|
//如果没有设置cdn,则使用baseURL拼接
|
|
|
|
|
if cdn == "" {
|
2021-03-25 10:42:18 +08:00
|
|
|
|
baseUrl := web.AppConfig.DefaultString("baseurl", "")
|
2017-05-12 10:45:40 +08:00
|
|
|
|
|
2018-03-24 17:24:02 +08:00
|
|
|
|
if strings.HasPrefix(p, "/") && strings.HasSuffix(baseUrl, "/") {
|
2018-03-13 14:14:56 +08:00
|
|
|
|
return baseUrl + p[1:]
|
|
|
|
|
}
|
2018-03-24 17:24:02 +08:00
|
|
|
|
if !strings.HasPrefix(p, "/") && !strings.HasSuffix(baseUrl, "/") {
|
2018-03-13 14:14:56 +08:00
|
|
|
|
return baseUrl + "/" + p
|
|
|
|
|
}
|
2018-03-24 17:24:02 +08:00
|
|
|
|
return baseUrl + p
|
2018-01-18 19:54:05 +08:00
|
|
|
|
}
|
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
|
|
|
|
})
|
2019-02-27 17:55:18 +08:00
|
|
|
|
if err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("注册函数 cdn 出错 ->", err)
|
2019-02-27 17:55:18 +08:00
|
|
|
|
os.Exit(-1)
|
|
|
|
|
}
|
2018-03-13 14:14:56 +08:00
|
|
|
|
|
2021-03-26 15:37:19 +08:00
|
|
|
|
err = web.AddFuncMap("cdnjs", conf.URLForWithCdnJs)
|
2019-02-27 17:55:18 +08:00
|
|
|
|
if err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("注册函数 cdnjs 出错 ->", err)
|
2019-02-27 17:55:18 +08:00
|
|
|
|
os.Exit(-1)
|
|
|
|
|
}
|
2021-03-26 15:37:19 +08:00
|
|
|
|
err = web.AddFuncMap("cdncss", conf.URLForWithCdnCss)
|
2019-02-27 17:55:18 +08:00
|
|
|
|
if err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("注册函数 cdncss 出错 ->", err)
|
2019-02-27 17:55:18 +08:00
|
|
|
|
os.Exit(-1)
|
|
|
|
|
}
|
2021-03-26 15:37:19 +08:00
|
|
|
|
err = web.AddFuncMap("cdnimg", conf.URLForWithCdnImage)
|
2019-02-27 17:55:18 +08:00
|
|
|
|
if err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("注册函数 cdnimg 出错 ->", err)
|
2019-02-27 17:55:18 +08:00
|
|
|
|
os.Exit(-1)
|
|
|
|
|
}
|
2018-03-13 14:14:56 +08:00
|
|
|
|
//重写url生成,支持配置域名以及域名前缀
|
2021-03-26 15:37:19 +08:00
|
|
|
|
err = web.AddFuncMap("urlfor", conf.URLFor)
|
2019-02-27 17:55:18 +08:00
|
|
|
|
if err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("注册函数 urlfor 出错 ->", err)
|
2019-02-27 17:55:18 +08:00
|
|
|
|
os.Exit(-1)
|
|
|
|
|
}
|
2021-10-09 18:22:43 +08:00
|
|
|
|
//读取配置值(未作任何转换)
|
|
|
|
|
err = web.AddFuncMap("conf", conf.CONF)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logs.Error("注册函数 conf 出错 ->", err)
|
|
|
|
|
os.Exit(-1)
|
|
|
|
|
}
|
2021-03-26 15:37:19 +08:00
|
|
|
|
err = web.AddFuncMap("date_format", func(t time.Time, format string) string {
|
2018-03-23 10:00:36 +08:00
|
|
|
|
return t.Local().Format(format)
|
|
|
|
|
})
|
2019-02-27 17:55:18 +08:00
|
|
|
|
if err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("注册函数 date_format 出错 ->", err)
|
2019-02-27 17:55:18 +08:00
|
|
|
|
os.Exit(-1)
|
|
|
|
|
}
|
2021-03-30 14:24:14 +08:00
|
|
|
|
|
2021-04-21 20:40:54 +08:00
|
|
|
|
err = web.AddFuncMap("i18n", i18n.Tr)
|
2021-03-29 18:00:24 +08:00
|
|
|
|
if err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("注册函数 i18n 出错 ->", err)
|
2021-03-29 18:00:24 +08:00
|
|
|
|
os.Exit(-1)
|
|
|
|
|
}
|
2024-11-09 11:17:16 +08:00
|
|
|
|
|
|
|
|
|
i18nList, err := web.AppConfig.String("i18n_list")
|
|
|
|
|
if err != nil {
|
|
|
|
|
logs.Error("error : failed to read i18n_list config ->", err)
|
|
|
|
|
i18nList = ""
|
|
|
|
|
}
|
|
|
|
|
if i18nList == "" { // 之所以分开判断是因为读取出的配置也可能是空串
|
|
|
|
|
logs.Error("error : config `i18n_list` is empty, please add config item like format: `i18n_list=zh-CN:简体中文|en-US:English`")
|
|
|
|
|
i18nList = "zh-cn:简体中文|en-us:English|ru-ru:Русский" // 没有配置时给个默认配置,避免啥语言都没有
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
langs := strings.Split(i18nList, "|")
|
|
|
|
|
i18nMap := make(map[string]string)
|
|
|
|
|
for _, langItem := range langs {
|
|
|
|
|
langItemSplit := strings.Split(langItem, ":")
|
|
|
|
|
if len(langItemSplit) < 2 {
|
|
|
|
|
logs.Error("error: language config value `" + langItem + "` for `i18n_list` format error")
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
lang := langItemSplit[0]
|
|
|
|
|
i18nMap[lang] = langItemSplit[1]
|
2021-03-30 14:24:14 +08:00
|
|
|
|
if err := i18n.SetMessage(lang, "conf/lang/"+lang+".ini"); err != nil {
|
|
|
|
|
logs.Error("Fail to set message file: " + err.Error())
|
2021-03-29 18:00:24 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-09 11:17:16 +08:00
|
|
|
|
|
|
|
|
|
i18nMapBytes, err := json.Marshal(i18nMap)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logs.Error("error: Fail to marshal i18n map, " + err.Error())
|
|
|
|
|
i18nMapBytes = []byte("{}")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = web.AppConfig.Set("i18n_map", string(i18nMapBytes))
|
|
|
|
|
if err != nil {
|
|
|
|
|
logs.Error("error: Fail to set i18n_map, " + err.Error())
|
|
|
|
|
}
|
2017-06-02 16:08:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-20 14:30:27 +08:00
|
|
|
|
// 解析命令
|
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
|
|
|
|
|
2019-05-20 12:08:14 +08:00
|
|
|
|
if err := flagSet.Parse(args); err != nil {
|
|
|
|
|
log.Fatal("解析命令失败 ->", err)
|
|
|
|
|
}
|
2017-06-02 16:08:14 +08:00
|
|
|
|
|
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-08-13 15:04:52 +08:00
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
|
if conf.ConfigurationFile == "" {
|
2018-11-05 18:50:01 +08:00
|
|
|
|
conf.ConfigurationFile = conf.WorkingDir("conf", "app.conf")
|
2018-07-11 14:18:27 +08:00
|
|
|
|
config := conf.WorkingDir("conf", "app.conf.example")
|
2018-03-24 17:24:02 +08:00
|
|
|
|
if !filetil.FileExists(conf.ConfigurationFile) && filetil.FileExists(config) {
|
2019-05-20 12:08:14 +08:00
|
|
|
|
_ = filetil.CopyFile(conf.ConfigurationFile, config)
|
2017-06-02 16:08:14 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-05 18:50:01 +08:00
|
|
|
|
if err := gocaptcha.ReadFonts(conf.WorkingDir("static", "fonts"), ".ttf"); err != nil {
|
|
|
|
|
log.Fatal("读取字体文件时出错 -> ", err)
|
2018-07-11 14:18:27 +08:00
|
|
|
|
}
|
2017-06-02 16:08:14 +08:00
|
|
|
|
|
2021-03-26 15:37:19 +08:00
|
|
|
|
if err := web.LoadAppConfig("ini", conf.ConfigurationFile); err != nil {
|
2018-07-11 14:18:27 +08:00
|
|
|
|
log.Fatal("An error occurred:", err)
|
2017-06-02 16:08:14 +08:00
|
|
|
|
}
|
2018-08-13 15:04:52 +08:00
|
|
|
|
if conf.LogFile == "" {
|
2021-03-25 10:42:18 +08:00
|
|
|
|
logPath, err := filepath.Abs(web.AppConfig.DefaultString("log_path", conf.WorkingDir("runtime", "logs")))
|
2018-08-13 15:04:52 +08:00
|
|
|
|
if err == nil {
|
|
|
|
|
conf.LogFile = logPath
|
2018-11-05 18:50:01 +08:00
|
|
|
|
} else {
|
|
|
|
|
conf.LogFile = conf.WorkingDir("runtime", "logs")
|
2018-08-13 15:04:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-25 10:42:18 +08:00
|
|
|
|
conf.AutoLoadDelay = web.AppConfig.DefaultInt("config_auto_delay", 0)
|
2018-07-11 14:18:27 +08:00
|
|
|
|
uploads := conf.WorkingDir("uploads")
|
2017-06-02 16:08:14 +08:00
|
|
|
|
|
2019-05-20 12:08:14 +08:00
|
|
|
|
_ = os.MkdirAll(uploads, 0666)
|
2017-06-02 16:08:14 +08:00
|
|
|
|
|
2021-03-23 21:55:50 +08:00
|
|
|
|
web.BConfig.WebConfig.StaticDir["/static"] = filepath.Join(conf.WorkingDirectory, "static")
|
|
|
|
|
web.BConfig.WebConfig.StaticDir["/uploads"] = uploads
|
|
|
|
|
web.BConfig.WebConfig.ViewsPath = conf.WorkingDir("views")
|
2021-03-25 11:49:39 +08:00
|
|
|
|
web.BConfig.WebConfig.Session.SessionCookieSameSite = http.SameSiteDefaultMode
|
2023-03-20 14:30:27 +08:00
|
|
|
|
var upload_file_size = conf.GetUploadFileSize()
|
|
|
|
|
if upload_file_size > web.BConfig.MaxUploadSize {
|
|
|
|
|
web.BConfig.MaxUploadSize = upload_file_size
|
|
|
|
|
}
|
2017-06-02 16:08:14 +08:00
|
|
|
|
|
2018-07-11 14:18:27 +08:00
|
|
|
|
fonts := conf.WorkingDir("static", "fonts")
|
2017-06-02 16:08:14 +08:00
|
|
|
|
|
2018-03-24 17:24:02 +08:00
|
|
|
|
if !filetil.FileExists(fonts) {
|
2017-06-02 16:08:14 +08:00
|
|
|
|
log.Fatal("Font path not exist.")
|
|
|
|
|
}
|
2019-05-20 12:08:14 +08:00
|
|
|
|
if err := gocaptcha.ReadFonts(filepath.Join(conf.WorkingDirectory, "static", "fonts"), ".ttf"); err != nil {
|
|
|
|
|
log.Fatal("读取字体失败 ->", err)
|
|
|
|
|
}
|
2017-06-02 16:08:14 +08:00
|
|
|
|
|
|
|
|
|
RegisterDataBase()
|
2018-02-27 17:20:42 +08:00
|
|
|
|
RegisterCache()
|
2017-06-02 16:08:14 +08:00
|
|
|
|
RegisterModel()
|
2018-01-26 17:17:38 +08:00
|
|
|
|
RegisterLogger(conf.LogFile)
|
2018-07-10 19:13:33 +08:00
|
|
|
|
|
|
|
|
|
ModifyPassword()
|
|
|
|
|
|
2017-04-30 22:13:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-20 14:30:27 +08:00
|
|
|
|
// 注册缓存管道
|
2018-03-24 17:24:02 +08:00
|
|
|
|
func RegisterCache() {
|
2021-03-25 10:42:18 +08:00
|
|
|
|
isOpenCache := web.AppConfig.DefaultBool("cache", false)
|
2018-02-27 17:20:42 +08:00
|
|
|
|
if !isOpenCache {
|
|
|
|
|
cache.Init(&cache.NullCache{})
|
2018-08-13 15:04:52 +08:00
|
|
|
|
return
|
2018-02-27 17:20:42 +08:00
|
|
|
|
}
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Info("正常初始化缓存配置.")
|
2021-03-25 10:42:18 +08:00
|
|
|
|
cacheProvider, _ := web.AppConfig.String("cache_provider")
|
2018-02-27 17:20:42 +08:00
|
|
|
|
if cacheProvider == "file" {
|
2021-03-25 10:42:18 +08:00
|
|
|
|
cacheFilePath := web.AppConfig.DefaultString("cache_file_path", "./runtime/cache/")
|
2018-02-27 17:20:42 +08:00
|
|
|
|
if strings.HasPrefix(cacheFilePath, "./") {
|
|
|
|
|
cacheFilePath = filepath.Join(conf.WorkingDirectory, string(cacheFilePath[1:]))
|
|
|
|
|
}
|
|
|
|
|
fileCache := beegoCache.NewFileCache()
|
2018-03-01 10:12:31 +08:00
|
|
|
|
|
2018-03-24 17:24:02 +08:00
|
|
|
|
fileConfig := make(map[string]string, 0)
|
2018-03-01 10:12:31 +08:00
|
|
|
|
|
2018-03-24 17:24:02 +08:00
|
|
|
|
fileConfig["CachePath"] = cacheFilePath
|
2021-03-25 10:42:18 +08:00
|
|
|
|
fileConfig["DirectoryLevel"] = web.AppConfig.DefaultString("cache_file_dir_level", "2")
|
|
|
|
|
fileConfig["EmbedExpiry"] = web.AppConfig.DefaultString("cache_file_expiry", "120")
|
|
|
|
|
fileConfig["FileSuffix"] = web.AppConfig.DefaultString("cache_file_suffix", ".bin")
|
2018-03-01 10:12:31 +08:00
|
|
|
|
|
2018-03-24 17:24:02 +08:00
|
|
|
|
bc, err := json.Marshal(&fileConfig)
|
2018-03-01 10:12:31 +08:00
|
|
|
|
if err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("初始化file缓存失败:", err)
|
2018-03-01 10:12:31 +08:00
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-20 12:08:14 +08:00
|
|
|
|
_ = fileCache.StartAndGC(string(bc))
|
2018-02-27 17:20:42 +08:00
|
|
|
|
|
|
|
|
|
cache.Init(fileCache)
|
2018-03-01 10:12:31 +08:00
|
|
|
|
|
2018-03-24 17:24:02 +08:00
|
|
|
|
} else if cacheProvider == "memory" {
|
2021-03-25 10:42:18 +08:00
|
|
|
|
cacheInterval := web.AppConfig.DefaultInt("cache_memory_interval", 60)
|
2018-02-27 17:20:42 +08:00
|
|
|
|
memory := beegoCache.NewMemoryCache()
|
|
|
|
|
beegoCache.DefaultEvery = cacheInterval
|
|
|
|
|
cache.Init(memory)
|
2018-03-24 17:24:02 +08:00
|
|
|
|
} else if cacheProvider == "redis" {
|
2018-07-25 14:46:56 +08:00
|
|
|
|
//设置Redis前缀
|
2021-03-25 10:42:18 +08:00
|
|
|
|
if key := web.AppConfig.DefaultString("cache_redis_prefix", ""); key != "" {
|
2018-07-25 14:46:56 +08:00
|
|
|
|
redis.DefaultKey = key
|
|
|
|
|
}
|
2018-03-24 17:24:02 +08:00
|
|
|
|
var redisConfig struct {
|
|
|
|
|
Conn string `json:"conn"`
|
2018-02-27 17:20:42 +08:00
|
|
|
|
Password string `json:"password"`
|
2021-08-08 11:15:46 +08:00
|
|
|
|
DbNum string `json:"dbNum"`
|
2018-02-27 17:20:42 +08:00
|
|
|
|
}
|
2021-08-08 11:15:46 +08:00
|
|
|
|
redisConfig.DbNum = "0"
|
2021-03-25 10:42:18 +08:00
|
|
|
|
redisConfig.Conn = web.AppConfig.DefaultString("cache_redis_host", "")
|
|
|
|
|
if pwd := web.AppConfig.DefaultString("cache_redis_password", ""); pwd != "" {
|
2018-02-27 17:20:42 +08:00
|
|
|
|
redisConfig.Password = pwd
|
|
|
|
|
}
|
2021-03-25 10:42:18 +08:00
|
|
|
|
if dbNum := web.AppConfig.DefaultInt("cache_redis_db", 0); dbNum > 0 {
|
2021-08-08 11:15:46 +08:00
|
|
|
|
redisConfig.DbNum = strconv.Itoa(dbNum)
|
2018-02-27 17:20:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-24 17:24:02 +08:00
|
|
|
|
bc, err := json.Marshal(&redisConfig)
|
2018-02-27 17:20:42 +08:00
|
|
|
|
if err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("初始化Redis缓存失败:", err)
|
2018-02-27 17:20:42 +08:00
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
2018-03-24 17:24:02 +08:00
|
|
|
|
redisCache, err := beegoCache.NewCache("redis", string(bc))
|
2018-02-27 17:20:42 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("初始化Redis缓存失败:", err)
|
2018-02-27 17:20:42 +08:00
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cache.Init(redisCache)
|
2018-03-24 17:24:02 +08:00
|
|
|
|
} else if cacheProvider == "memcache" {
|
2018-02-27 17:20:42 +08:00
|
|
|
|
|
2018-03-24 17:24:02 +08:00
|
|
|
|
var memcacheConfig struct {
|
2018-02-27 17:20:42 +08:00
|
|
|
|
Conn string `json:"conn"`
|
|
|
|
|
}
|
2021-03-25 10:42:18 +08:00
|
|
|
|
memcacheConfig.Conn = web.AppConfig.DefaultString("cache_memcache_host", "")
|
2018-02-27 17:20:42 +08:00
|
|
|
|
|
2018-03-24 17:24:02 +08:00
|
|
|
|
bc, err := json.Marshal(&memcacheConfig)
|
2018-02-27 17:20:42 +08:00
|
|
|
|
if err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("初始化 Memcache 缓存失败 ->", err)
|
2018-02-27 17:20:42 +08:00
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
2018-03-24 17:24:02 +08:00
|
|
|
|
memcache, err := beegoCache.NewCache("memcache", string(bc))
|
2018-02-27 17:20:42 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("初始化 Memcache 缓存失败 ->", err)
|
2018-02-27 17:20:42 +08:00
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cache.Init(memcache)
|
|
|
|
|
|
2018-03-24 17:24:02 +08:00
|
|
|
|
} else {
|
2018-02-27 17:20:42 +08:00
|
|
|
|
cache.Init(&cache.NullCache{})
|
2021-03-30 16:18:02 +08:00
|
|
|
|
logs.Warn("不支持的缓存管道,缓存将禁用 ->", cacheProvider)
|
2018-02-27 17:20:42 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Info("缓存初始化完成.")
|
2018-02-27 17:20:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-20 14:30:27 +08:00
|
|
|
|
// 自动加载配置文件.修改了监听端口号和数据库配置无法自动生效.
|
2018-11-05 18:50:01 +08:00
|
|
|
|
func RegisterAutoLoadConfig() {
|
2018-08-07 17:19:56 +08:00
|
|
|
|
if conf.AutoLoadDelay > 0 {
|
|
|
|
|
|
2018-09-19 11:32:29 +08:00
|
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("创建配置文件监控器失败 ->", err)
|
2018-09-19 11:32:29 +08:00
|
|
|
|
}
|
2018-08-07 17:19:56 +08:00
|
|
|
|
go func() {
|
|
|
|
|
for {
|
|
|
|
|
select {
|
2018-09-19 11:32:29 +08:00
|
|
|
|
case ev := <-watcher.Event:
|
|
|
|
|
//如果是修改了配置文件
|
|
|
|
|
if ev.IsModify() {
|
2021-03-26 15:37:19 +08:00
|
|
|
|
if err := web.LoadAppConfig("ini", conf.ConfigurationFile); err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("An error occurred ->", err)
|
2018-09-19 14:29:25 +08:00
|
|
|
|
continue
|
2018-08-07 17:19:56 +08:00
|
|
|
|
}
|
2018-08-13 15:04:52 +08:00
|
|
|
|
RegisterCache()
|
|
|
|
|
RegisterLogger("")
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Info("配置文件已加载 ->", conf.ConfigurationFile)
|
2018-09-19 14:29:25 +08:00
|
|
|
|
} else if ev.IsRename() {
|
2019-05-20 12:08:14 +08:00
|
|
|
|
_ = watcher.WatchFlags(conf.ConfigurationFile, fsnotify.FSN_MODIFY|fsnotify.FSN_RENAME)
|
2018-08-07 17:19:56 +08:00
|
|
|
|
}
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Info(ev.String())
|
2018-09-19 11:32:29 +08:00
|
|
|
|
case err := <-watcher.Error:
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("配置文件监控器错误 ->", err)
|
2018-09-19 14:29:25 +08:00
|
|
|
|
|
2018-08-07 17:19:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
2018-09-19 11:32:29 +08:00
|
|
|
|
|
2018-09-19 14:29:25 +08:00
|
|
|
|
err = watcher.WatchFlags(conf.ConfigurationFile, fsnotify.FSN_MODIFY|fsnotify.FSN_RENAME)
|
2018-09-19 11:32:29 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("监控配置文件失败 ->", err)
|
2018-09-19 11:32:29 +08:00
|
|
|
|
}
|
2018-08-07 17:19:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-20 20:36:14 +08:00
|
|
|
|
|
2023-03-20 14:30:27 +08:00
|
|
|
|
// 注册错误处理方法.
|
2018-11-20 20:36:14 +08:00
|
|
|
|
func RegisterError() {
|
2021-03-23 21:55:50 +08:00
|
|
|
|
web.ErrorHandler("404", func(writer http.ResponseWriter, request *http.Request) {
|
2018-11-09 18:10:34 +08:00
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
2018-11-20 20:36:14 +08:00
|
|
|
|
data := make(map[string]interface{})
|
2018-11-09 18:10:34 +08:00
|
|
|
|
data["ErrorCode"] = 404
|
|
|
|
|
data["ErrorMessage"] = "页面未找到或已删除"
|
|
|
|
|
|
2021-03-26 15:37:19 +08:00
|
|
|
|
if err := web.ExecuteViewPathTemplate(&buf, "errors/error.tpl", web.BConfig.WebConfig.ViewsPath, data); err == nil {
|
2019-05-20 12:08:14 +08:00
|
|
|
|
_, _ = fmt.Fprint(writer, buf.String())
|
2018-11-09 18:10:34 +08:00
|
|
|
|
} else {
|
2019-05-20 12:08:14 +08:00
|
|
|
|
_, _ = fmt.Fprint(writer, data["ErrorMessage"])
|
2018-11-09 18:10:34 +08:00
|
|
|
|
}
|
|
|
|
|
})
|
2021-03-23 21:55:50 +08:00
|
|
|
|
web.ErrorHandler("401", func(writer http.ResponseWriter, request *http.Request) {
|
2018-11-09 18:10:34 +08:00
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
2018-11-20 20:36:14 +08:00
|
|
|
|
data := make(map[string]interface{})
|
2018-11-09 18:10:34 +08:00
|
|
|
|
data["ErrorCode"] = 401
|
|
|
|
|
data["ErrorMessage"] = "请与 Web 服务器的管理员联系,以确认您是否具有访问所请求资源的权限。"
|
|
|
|
|
|
2021-03-26 15:37:19 +08:00
|
|
|
|
if err := web.ExecuteViewPathTemplate(&buf, "errors/error.tpl", web.BConfig.WebConfig.ViewsPath, data); err == nil {
|
2019-05-20 12:08:14 +08:00
|
|
|
|
_, _ = fmt.Fprint(writer, buf.String())
|
2018-11-09 18:10:34 +08:00
|
|
|
|
} else {
|
2019-05-20 12:08:14 +08:00
|
|
|
|
_, _ = fmt.Fprint(writer, data["ErrorMessage"])
|
2018-11-09 18:10:34 +08:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2018-08-07 17:19:56 +08:00
|
|
|
|
|
2017-05-11 13:39:34 +08:00
|
|
|
|
func init() {
|
2017-06-02 16:08:14 +08:00
|
|
|
|
|
2018-03-24 17:24:02 +08:00
|
|
|
|
if configPath, err := filepath.Abs(conf.ConfigurationFile); err == nil {
|
2018-01-26 17:17:38 +08:00
|
|
|
|
conf.ConfigurationFile = configPath
|
|
|
|
|
}
|
2019-05-20 12:08:14 +08:00
|
|
|
|
if err := gocaptcha.ReadFonts(conf.WorkingDir("static", "fonts"), ".ttf"); err != nil {
|
|
|
|
|
log.Fatal("读取字体文件失败 ->", err)
|
|
|
|
|
}
|
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
|
|
|
|
}
|