kernel.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // SiYuan - Refactor your thinking
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package mobile
  17. import (
  18. "fmt"
  19. "os"
  20. "path/filepath"
  21. "strings"
  22. "time"
  23. "github.com/siyuan-note/filelock"
  24. "github.com/siyuan-note/logging"
  25. "github.com/siyuan-note/siyuan/kernel/cache"
  26. "github.com/siyuan-note/siyuan/kernel/job"
  27. "github.com/siyuan-note/siyuan/kernel/model"
  28. "github.com/siyuan-note/siyuan/kernel/server"
  29. "github.com/siyuan-note/siyuan/kernel/sql"
  30. "github.com/siyuan-note/siyuan/kernel/util"
  31. _ "golang.org/x/mobile/bind"
  32. )
  33. func StartKernelFast(container, appDir, workspaceBaseDir, localIPs string) {
  34. go server.Serve(true)
  35. }
  36. func StartKernel(container, appDir, workspaceBaseDir, timezoneID, localIPs, lang, osVer string) {
  37. SetTimezone(container, appDir, timezoneID)
  38. util.Mode = "prod"
  39. util.MobileOSVer = osVer
  40. util.LocalIPs = strings.Split(localIPs, ",")
  41. util.BootMobile(container, appDir, workspaceBaseDir, lang)
  42. model.InitConf()
  43. go server.Serve(false)
  44. go func() {
  45. model.InitAppearance()
  46. sql.InitDatabase(false)
  47. sql.InitHistoryDatabase(false)
  48. sql.InitAssetContentDatabase(false)
  49. sql.SetCaseSensitive(model.Conf.Search.CaseSensitive)
  50. sql.SetIndexAssetPath(model.Conf.Search.IndexAssetPath)
  51. model.BootSyncData()
  52. model.InitBoxes()
  53. model.LoadFlashcards()
  54. util.LoadAssetsTexts()
  55. util.SetBooted()
  56. util.PushClearAllMsg()
  57. job.StartCron()
  58. go model.AutoGenerateFileHistory()
  59. go cache.LoadAssets()
  60. }()
  61. }
  62. func Language(num int) string {
  63. return model.Conf.Language(num)
  64. }
  65. func ShowMsg(msg string, timeout int) {
  66. util.PushMsg(msg, timeout)
  67. }
  68. func IsHttpServing() bool {
  69. return util.HttpServing
  70. }
  71. func SetHttpServerPort(port int) {
  72. filelock.AndroidServerPort = port
  73. }
  74. func GetCurrentWorkspacePath() string {
  75. return util.WorkspaceDir
  76. }
  77. func GetAssetAbsPath(asset string) (ret string) {
  78. ret, err := model.GetAssetAbsPath(asset)
  79. if nil != err {
  80. logging.LogErrorf("get asset [%s] abs path failed: %s", asset, err)
  81. ret = asset
  82. }
  83. return
  84. }
  85. func GetMimeTypeByExt(ext string) string {
  86. return util.GetMimeTypeByExt(ext)
  87. }
  88. func SetTimezone(container, appDir, timezoneID string) {
  89. if "ios" == container {
  90. os.Setenv("ZONEINFO", filepath.Join(appDir, "app", "zoneinfo.zip"))
  91. }
  92. z, err := time.LoadLocation(strings.TrimSpace(timezoneID))
  93. if err != nil {
  94. fmt.Printf("load location failed: %s\n", err)
  95. time.Local = time.FixedZone("CST", 8*3600)
  96. return
  97. }
  98. time.Local = z
  99. }