command.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. package commands
  2. import (
  3. "encoding/gob"
  4. "flag"
  5. "fmt"
  6. "log"
  7. "net/url"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. "time"
  12. "bytes"
  13. "encoding/json"
  14. "net/http"
  15. beegoCache "github.com/beego/beego/v2/client/cache"
  16. _ "github.com/beego/beego/v2/client/cache/memcache"
  17. "github.com/beego/beego/v2/client/cache/redis"
  18. _ "github.com/beego/beego/v2/client/cache/redis"
  19. "github.com/beego/beego/v2/client/orm"
  20. "github.com/beego/beego/v2/core/logs"
  21. "github.com/beego/beego/v2/server/web"
  22. "github.com/howeyc/fsnotify"
  23. "github.com/lifei6671/gocaptcha"
  24. "github.com/mindoc-org/mindoc/cache"
  25. "github.com/mindoc-org/mindoc/conf"
  26. "github.com/mindoc-org/mindoc/models"
  27. "github.com/mindoc-org/mindoc/utils/filetil"
  28. )
  29. // RegisterDataBase 注册数据库
  30. func RegisterDataBase() {
  31. logs.Info("正在初始化数据库配置.")
  32. dbadapter, _ := web.AppConfig.String("db_adapter")
  33. orm.DefaultTimeLoc = time.Local
  34. orm.DefaultRowsLimit = -1
  35. if strings.EqualFold(dbadapter, "mysql") {
  36. host, _ := web.AppConfig.String("db_host")
  37. database, _ := web.AppConfig.String("db_database")
  38. username, _ := web.AppConfig.String("db_username")
  39. password, _ := web.AppConfig.String("db_password")
  40. timezone, _ := web.AppConfig.String("timezone")
  41. location, err := time.LoadLocation(timezone)
  42. if err == nil {
  43. orm.DefaultTimeLoc = location
  44. } else {
  45. logs.Error("加载时区配置信息失败,请检查是否存在 ZONEINFO 环境变量->", err)
  46. }
  47. port, _ := web.AppConfig.String("db_port")
  48. dataSource := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true&loc=%s", username, password, host, port, database, url.QueryEscape(timezone))
  49. if err := orm.RegisterDataBase("default", "mysql", dataSource); err != nil {
  50. logs.Error("注册默认数据库失败->", err)
  51. os.Exit(1)
  52. }
  53. } else if strings.EqualFold(dbadapter, "sqlite3") {
  54. database, _ := web.AppConfig.String("db_database")
  55. if strings.HasPrefix(database, "./") {
  56. database = filepath.Join(conf.WorkingDirectory, string(database[1:]))
  57. }
  58. if p, err := filepath.Abs(database); err == nil {
  59. database = p
  60. }
  61. dbPath := filepath.Dir(database)
  62. if _, err := os.Stat(dbPath); err != nil && os.IsNotExist(err) {
  63. _ = os.MkdirAll(dbPath, 0777)
  64. }
  65. err := orm.RegisterDataBase("default", "sqlite3", database)
  66. if err != nil {
  67. logs.Error("注册默认数据库失败->", err)
  68. }
  69. } else {
  70. logs.Error("不支持的数据库类型.")
  71. os.Exit(1)
  72. }
  73. logs.Info("数据库初始化完成.")
  74. }
  75. // RegisterModel 注册Model
  76. func RegisterModel() {
  77. orm.RegisterModelWithPrefix(conf.GetDatabasePrefix(),
  78. new(models.Member),
  79. new(models.Book),
  80. new(models.Relationship),
  81. new(models.Option),
  82. new(models.Document),
  83. new(models.Attachment),
  84. new(models.Logger),
  85. new(models.MemberToken),
  86. new(models.DocumentHistory),
  87. new(models.Migration),
  88. new(models.Label),
  89. new(models.Blog),
  90. new(models.Template),
  91. new(models.Team),
  92. new(models.TeamMember),
  93. new(models.TeamRelationship),
  94. new(models.Itemsets),
  95. )
  96. gob.Register(models.Blog{})
  97. gob.Register(models.Document{})
  98. gob.Register(models.Template{})
  99. //migrate.RegisterMigration()
  100. }
  101. // RegisterLogger 注册日志
  102. func RegisterLogger(log string) {
  103. logs.SetLogFuncCall(true)
  104. _ = logs.SetLogger("console")
  105. logs.EnableFuncCallDepth(true)
  106. if web.AppConfig.DefaultBool("log_is_async", true) {
  107. logs.Async(1e3)
  108. }
  109. if log == "" {
  110. logPath, err := filepath.Abs(web.AppConfig.DefaultString("log_path", conf.WorkingDir("runtime", "logs")))
  111. if err == nil {
  112. log = logPath
  113. } else {
  114. log = conf.WorkingDir("runtime", "logs")
  115. }
  116. }
  117. logPath := filepath.Join(log, "log.log")
  118. if _, err := os.Stat(log); os.IsNotExist(err) {
  119. _ = os.MkdirAll(log, 0755)
  120. }
  121. config := make(map[string]interface{}, 1)
  122. config["filename"] = logPath
  123. config["perm"] = "0755"
  124. config["rotate"] = true
  125. if maxLines := web.AppConfig.DefaultInt("log_maxlines", 1000000); maxLines > 0 {
  126. config["maxLines"] = maxLines
  127. }
  128. if maxSize := web.AppConfig.DefaultInt("log_maxsize", 1<<28); maxSize > 0 {
  129. config["maxsize"] = maxSize
  130. }
  131. if !web.AppConfig.DefaultBool("log_daily", true) {
  132. config["daily"] = false
  133. }
  134. if maxDays := web.AppConfig.DefaultInt("log_maxdays", 7); maxDays > 0 {
  135. config["maxdays"] = maxDays
  136. }
  137. if level := web.AppConfig.DefaultString("log_level", "Trace"); level != "" {
  138. switch level {
  139. case "Emergency":
  140. config["level"] = logs.LevelEmergency
  141. case "Alert":
  142. config["level"] = logs.LevelAlert
  143. case "Critical":
  144. config["level"] = logs.LevelCritical
  145. case "Error":
  146. config["level"] = logs.LevelError
  147. case "Warning":
  148. config["level"] = logs.LevelWarning
  149. case "Notice":
  150. config["level"] = logs.LevelNotice
  151. case "Informational":
  152. config["level"] = logs.LevelInformational
  153. case "Debug":
  154. config["level"] = logs.LevelDebug
  155. }
  156. }
  157. b, err := json.Marshal(config)
  158. if err != nil {
  159. logs.Error("初始化文件日志时出错 ->", err)
  160. _ = logs.SetLogger("file", `{"filename":"`+logPath+`"}`)
  161. } else {
  162. _ = logs.SetLogger(logs.AdapterFile, string(b))
  163. }
  164. logs.SetLogFuncCall(true)
  165. }
  166. // RunCommand 注册orm命令行工具
  167. func RegisterCommand() {
  168. if len(os.Args) >= 2 && os.Args[1] == "install" {
  169. ResolveCommand(os.Args[2:])
  170. Install()
  171. } else if len(os.Args) >= 2 && os.Args[1] == "version" {
  172. CheckUpdate()
  173. os.Exit(0)
  174. }
  175. }
  176. //注册模板函数
  177. func RegisterFunction() {
  178. err := web.AddFuncMap("config", models.GetOptionValue)
  179. if err != nil {
  180. logs.Error("注册函数 config 出错 ->", err)
  181. os.Exit(-1)
  182. }
  183. err = web.AddFuncMap("cdn", func(p string) string {
  184. cdn := web.AppConfig.DefaultString("cdn", "")
  185. if strings.HasPrefix(p, "http://") || strings.HasPrefix(p, "https://") {
  186. return p
  187. }
  188. //如果没有设置cdn,则使用baseURL拼接
  189. if cdn == "" {
  190. baseUrl := web.AppConfig.DefaultString("baseurl", "")
  191. if strings.HasPrefix(p, "/") && strings.HasSuffix(baseUrl, "/") {
  192. return baseUrl + p[1:]
  193. }
  194. if !strings.HasPrefix(p, "/") && !strings.HasSuffix(baseUrl, "/") {
  195. return baseUrl + "/" + p
  196. }
  197. return baseUrl + p
  198. }
  199. if strings.HasPrefix(p, "/") && strings.HasSuffix(cdn, "/") {
  200. return cdn + string(p[1:])
  201. }
  202. if !strings.HasPrefix(p, "/") && !strings.HasSuffix(cdn, "/") {
  203. return cdn + "/" + p
  204. }
  205. return cdn + p
  206. })
  207. if err != nil {
  208. logs.Error("注册函数 cdn 出错 ->", err)
  209. os.Exit(-1)
  210. }
  211. err = web.AddFuncMap("cdnjs", conf.URLForWithCdnJs)
  212. if err != nil {
  213. logs.Error("注册函数 cdnjs 出错 ->", err)
  214. os.Exit(-1)
  215. }
  216. err = web.AddFuncMap("cdncss", conf.URLForWithCdnCss)
  217. if err != nil {
  218. logs.Error("注册函数 cdncss 出错 ->", err)
  219. os.Exit(-1)
  220. }
  221. err = web.AddFuncMap("cdnimg", conf.URLForWithCdnImage)
  222. if err != nil {
  223. logs.Error("注册函数 cdnimg 出错 ->", err)
  224. os.Exit(-1)
  225. }
  226. //重写url生成,支持配置域名以及域名前缀
  227. err = web.AddFuncMap("urlfor", conf.URLFor)
  228. if err != nil {
  229. logs.Error("注册函数 urlfor 出错 ->", err)
  230. os.Exit(-1)
  231. }
  232. err = web.AddFuncMap("date_format", func(t time.Time, format string) string {
  233. return t.Local().Format(format)
  234. })
  235. if err != nil {
  236. logs.Error("注册函数 date_format 出错 ->", err)
  237. os.Exit(-1)
  238. }
  239. }
  240. //解析命令
  241. func ResolveCommand(args []string) {
  242. flagSet := flag.NewFlagSet("MinDoc command: ", flag.ExitOnError)
  243. flagSet.StringVar(&conf.ConfigurationFile, "config", "", "MinDoc configuration file.")
  244. flagSet.StringVar(&conf.WorkingDirectory, "dir", "", "MinDoc working directory.")
  245. flagSet.StringVar(&conf.LogFile, "log", "", "MinDoc log file path.")
  246. if err := flagSet.Parse(args); err != nil {
  247. log.Fatal("解析命令失败 ->", err)
  248. }
  249. if conf.WorkingDirectory == "" {
  250. if p, err := filepath.Abs(os.Args[0]); err == nil {
  251. conf.WorkingDirectory = filepath.Dir(p)
  252. }
  253. }
  254. if conf.ConfigurationFile == "" {
  255. conf.ConfigurationFile = conf.WorkingDir("conf", "app.conf")
  256. config := conf.WorkingDir("conf", "app.conf.example")
  257. if !filetil.FileExists(conf.ConfigurationFile) && filetil.FileExists(config) {
  258. _ = filetil.CopyFile(conf.ConfigurationFile, config)
  259. }
  260. }
  261. if err := gocaptcha.ReadFonts(conf.WorkingDir("static", "fonts"), ".ttf"); err != nil {
  262. log.Fatal("读取字体文件时出错 -> ", err)
  263. }
  264. if err := web.LoadAppConfig("ini", conf.ConfigurationFile); err != nil {
  265. log.Fatal("An error occurred:", err)
  266. }
  267. if conf.LogFile == "" {
  268. logPath, err := filepath.Abs(web.AppConfig.DefaultString("log_path", conf.WorkingDir("runtime", "logs")))
  269. if err == nil {
  270. conf.LogFile = logPath
  271. } else {
  272. conf.LogFile = conf.WorkingDir("runtime", "logs")
  273. }
  274. }
  275. conf.AutoLoadDelay = web.AppConfig.DefaultInt("config_auto_delay", 0)
  276. uploads := conf.WorkingDir("uploads")
  277. _ = os.MkdirAll(uploads, 0666)
  278. web.BConfig.WebConfig.StaticDir["/static"] = filepath.Join(conf.WorkingDirectory, "static")
  279. web.BConfig.WebConfig.StaticDir["/uploads"] = uploads
  280. web.BConfig.WebConfig.ViewsPath = conf.WorkingDir("views")
  281. web.BConfig.WebConfig.Session.SessionCookieSameSite = http.SameSiteDefaultMode
  282. fonts := conf.WorkingDir("static", "fonts")
  283. if !filetil.FileExists(fonts) {
  284. log.Fatal("Font path not exist.")
  285. }
  286. if err := gocaptcha.ReadFonts(filepath.Join(conf.WorkingDirectory, "static", "fonts"), ".ttf"); err != nil {
  287. log.Fatal("读取字体失败 ->", err)
  288. }
  289. RegisterDataBase()
  290. RegisterCache()
  291. RegisterModel()
  292. RegisterLogger(conf.LogFile)
  293. ModifyPassword()
  294. }
  295. //注册缓存管道
  296. func RegisterCache() {
  297. isOpenCache := web.AppConfig.DefaultBool("cache", false)
  298. if !isOpenCache {
  299. cache.Init(&cache.NullCache{})
  300. return
  301. }
  302. logs.Info("正常初始化缓存配置.")
  303. cacheProvider, _ := web.AppConfig.String("cache_provider")
  304. if cacheProvider == "file" {
  305. cacheFilePath := web.AppConfig.DefaultString("cache_file_path", "./runtime/cache/")
  306. if strings.HasPrefix(cacheFilePath, "./") {
  307. cacheFilePath = filepath.Join(conf.WorkingDirectory, string(cacheFilePath[1:]))
  308. }
  309. fileCache := beegoCache.NewFileCache()
  310. fileConfig := make(map[string]string, 0)
  311. fileConfig["CachePath"] = cacheFilePath
  312. fileConfig["DirectoryLevel"] = web.AppConfig.DefaultString("cache_file_dir_level", "2")
  313. fileConfig["EmbedExpiry"] = web.AppConfig.DefaultString("cache_file_expiry", "120")
  314. fileConfig["FileSuffix"] = web.AppConfig.DefaultString("cache_file_suffix", ".bin")
  315. bc, err := json.Marshal(&fileConfig)
  316. if err != nil {
  317. logs.Error("初始化file缓存失败:", err)
  318. os.Exit(1)
  319. }
  320. _ = fileCache.StartAndGC(string(bc))
  321. cache.Init(fileCache)
  322. } else if cacheProvider == "memory" {
  323. cacheInterval := web.AppConfig.DefaultInt("cache_memory_interval", 60)
  324. memory := beegoCache.NewMemoryCache()
  325. beegoCache.DefaultEvery = cacheInterval
  326. cache.Init(memory)
  327. } else if cacheProvider == "redis" {
  328. //设置Redis前缀
  329. if key := web.AppConfig.DefaultString("cache_redis_prefix", ""); key != "" {
  330. redis.DefaultKey = key
  331. }
  332. var redisConfig struct {
  333. Conn string `json:"conn"`
  334. Password string `json:"password"`
  335. DbNum int `json:"dbNum"`
  336. }
  337. redisConfig.DbNum = 0
  338. redisConfig.Conn = web.AppConfig.DefaultString("cache_redis_host", "")
  339. if pwd := web.AppConfig.DefaultString("cache_redis_password", ""); pwd != "" {
  340. redisConfig.Password = pwd
  341. }
  342. if dbNum := web.AppConfig.DefaultInt("cache_redis_db", 0); dbNum > 0 {
  343. redisConfig.DbNum = dbNum
  344. }
  345. bc, err := json.Marshal(&redisConfig)
  346. if err != nil {
  347. logs.Error("初始化Redis缓存失败:", err)
  348. os.Exit(1)
  349. }
  350. redisCache, err := beegoCache.NewCache("redis", string(bc))
  351. if err != nil {
  352. logs.Error("初始化Redis缓存失败:", err)
  353. os.Exit(1)
  354. }
  355. cache.Init(redisCache)
  356. } else if cacheProvider == "memcache" {
  357. var memcacheConfig struct {
  358. Conn string `json:"conn"`
  359. }
  360. memcacheConfig.Conn = web.AppConfig.DefaultString("cache_memcache_host", "")
  361. bc, err := json.Marshal(&memcacheConfig)
  362. if err != nil {
  363. logs.Error("初始化 Memcache 缓存失败 ->", err)
  364. os.Exit(1)
  365. }
  366. memcache, err := beegoCache.NewCache("memcache", string(bc))
  367. if err != nil {
  368. logs.Error("初始化 Memcache 缓存失败 ->", err)
  369. os.Exit(1)
  370. }
  371. cache.Init(memcache)
  372. } else {
  373. cache.Init(&cache.NullCache{})
  374. logs.Warn("不支持的缓存管道,缓存将禁用 ->", cacheProvider)
  375. return
  376. }
  377. logs.Info("缓存初始化完成.")
  378. }
  379. //自动加载配置文件.修改了监听端口号和数据库配置无法自动生效.
  380. func RegisterAutoLoadConfig() {
  381. if conf.AutoLoadDelay > 0 {
  382. watcher, err := fsnotify.NewWatcher()
  383. if err != nil {
  384. logs.Error("创建配置文件监控器失败 ->", err)
  385. }
  386. go func() {
  387. for {
  388. select {
  389. case ev := <-watcher.Event:
  390. //如果是修改了配置文件
  391. if ev.IsModify() {
  392. if err := web.LoadAppConfig("ini", conf.ConfigurationFile); err != nil {
  393. logs.Error("An error occurred ->", err)
  394. continue
  395. }
  396. RegisterCache()
  397. RegisterLogger("")
  398. logs.Info("配置文件已加载 ->", conf.ConfigurationFile)
  399. } else if ev.IsRename() {
  400. _ = watcher.WatchFlags(conf.ConfigurationFile, fsnotify.FSN_MODIFY|fsnotify.FSN_RENAME)
  401. }
  402. logs.Info(ev.String())
  403. case err := <-watcher.Error:
  404. logs.Error("配置文件监控器错误 ->", err)
  405. }
  406. }
  407. }()
  408. err = watcher.WatchFlags(conf.ConfigurationFile, fsnotify.FSN_MODIFY|fsnotify.FSN_RENAME)
  409. if err != nil {
  410. logs.Error("监控配置文件失败 ->", err)
  411. }
  412. }
  413. }
  414. //注册错误处理方法.
  415. func RegisterError() {
  416. web.ErrorHandler("404", func(writer http.ResponseWriter, request *http.Request) {
  417. var buf bytes.Buffer
  418. data := make(map[string]interface{})
  419. data["ErrorCode"] = 404
  420. data["ErrorMessage"] = "页面未找到或已删除"
  421. if err := web.ExecuteViewPathTemplate(&buf, "errors/error.tpl", web.BConfig.WebConfig.ViewsPath, data); err == nil {
  422. _, _ = fmt.Fprint(writer, buf.String())
  423. } else {
  424. _, _ = fmt.Fprint(writer, data["ErrorMessage"])
  425. }
  426. })
  427. web.ErrorHandler("401", func(writer http.ResponseWriter, request *http.Request) {
  428. var buf bytes.Buffer
  429. data := make(map[string]interface{})
  430. data["ErrorCode"] = 401
  431. data["ErrorMessage"] = "请与 Web 服务器的管理员联系,以确认您是否具有访问所请求资源的权限。"
  432. if err := web.ExecuteViewPathTemplate(&buf, "errors/error.tpl", web.BConfig.WebConfig.ViewsPath, data); err == nil {
  433. _, _ = fmt.Fprint(writer, buf.String())
  434. } else {
  435. _, _ = fmt.Fprint(writer, data["ErrorMessage"])
  436. }
  437. })
  438. }
  439. func init() {
  440. if configPath, err := filepath.Abs(conf.ConfigurationFile); err == nil {
  441. conf.ConfigurationFile = configPath
  442. }
  443. if err := gocaptcha.ReadFonts(conf.WorkingDir("static", "fonts"), ".ttf"); err != nil {
  444. log.Fatal("读取字体文件失败 ->", err)
  445. }
  446. gob.Register(models.Member{})
  447. if p, err := filepath.Abs(os.Args[0]); err == nil {
  448. conf.WorkingDirectory = filepath.Dir(p)
  449. }
  450. }