main.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. package main
  2. import (
  3. "flag"
  4. "path/filepath"
  5. "strings"
  6. log "github.com/sirupsen/logrus"
  7. "github.com/OliveTin/OliveTin/internal/auth"
  8. "github.com/OliveTin/OliveTin/internal/entities"
  9. "github.com/OliveTin/OliveTin/internal/executor"
  10. "github.com/OliveTin/OliveTin/internal/httpservers"
  11. "github.com/OliveTin/OliveTin/internal/installationinfo"
  12. "github.com/OliveTin/OliveTin/internal/oncalendarfile"
  13. "github.com/OliveTin/OliveTin/internal/oncron"
  14. "github.com/OliveTin/OliveTin/internal/onfileindir"
  15. "github.com/OliveTin/OliveTin/internal/onstartup"
  16. "github.com/OliveTin/OliveTin/internal/servicehost"
  17. updatecheck "github.com/OliveTin/OliveTin/internal/updatecheck"
  18. "os"
  19. "strconv"
  20. config "github.com/OliveTin/OliveTin/internal/config"
  21. "github.com/knadh/koanf/parsers/yaml"
  22. "github.com/knadh/koanf/providers/env"
  23. "github.com/knadh/koanf/providers/file"
  24. "github.com/knadh/koanf/v2"
  25. )
  26. var (
  27. cfg *config.Config
  28. version = "dev"
  29. commit = "nocommit"
  30. date = "nodate"
  31. )
  32. func init() {
  33. initLog()
  34. initConfig(initCliFlags())
  35. initCheckEnvironment()
  36. initInstallationInfo()
  37. log.Info("OliveTin initialization complete")
  38. }
  39. func initLog() {
  40. logFormat := os.Getenv("OLIVETIN_LOG_FORMAT")
  41. if logFormat == "json" {
  42. log.SetFormatter(&log.JSONFormatter{})
  43. } else {
  44. log.SetFormatter(&log.TextFormatter{
  45. ForceQuote: true,
  46. DisableTimestamp: true,
  47. })
  48. }
  49. // Use debug this early on to catch details about startup errors. The
  50. // default config will raise the log level later, if not set.
  51. log.SetLevel(log.DebugLevel) // Default to debug, to catch cfg issue
  52. }
  53. func initCliFlags() string {
  54. var configDir string
  55. flag.StringVar(&configDir, "configdir", ".", "Config directory path")
  56. var printVersion bool
  57. flag.BoolVar(&printVersion, "version", false, "Prints the version number and exits")
  58. flag.Parse()
  59. // This log message should be the first log message OliveTin prints.
  60. if printVersion {
  61. logStartupMessage("OliveTin is just printing the startup message")
  62. os.Exit(1)
  63. } else {
  64. logStartupMessage("OliveTin initializing")
  65. }
  66. log.WithFields(log.Fields{
  67. "value": configDir,
  68. }).Debugf("Value of -configdir flag")
  69. return configDir
  70. }
  71. func getBasePort() int {
  72. var err error
  73. defaultPort := 1337
  74. basePort := defaultPort
  75. envPort := os.Getenv("PORT")
  76. if envPort != "" {
  77. basePort, err = strconv.Atoi(os.Getenv("PORT"))
  78. if err != nil {
  79. log.Errorf("Error converting port to int. %s", err)
  80. os.Exit(1)
  81. }
  82. }
  83. if defaultPort != basePort {
  84. log.WithFields(log.Fields{
  85. "basePort": basePort,
  86. }).Debug("Base port")
  87. }
  88. return basePort
  89. }
  90. func getConfigPath(directory string) string {
  91. joinedPath := filepath.Join(directory, "config.yaml")
  92. configPath, err := filepath.Abs(joinedPath)
  93. if err != nil {
  94. log.WithError(err).Warnf("Error getting absolute path for %s", joinedPath)
  95. return joinedPath
  96. }
  97. return configPath
  98. }
  99. func initConfig(configDir string) {
  100. k := koanf.New(".")
  101. err := k.Load(env.Provider(".", ".", nil), nil)
  102. if err != nil {
  103. log.WithFields(log.Fields{
  104. "error": err,
  105. }).Fatalf("Error loading environment variables")
  106. }
  107. directories := []string{
  108. configDir,
  109. }
  110. // Only load additional configs if not in integration test mode
  111. absConfigDir, _ := filepath.Abs(configDir)
  112. if !strings.Contains(absConfigDir, "integration-tests") {
  113. directories = append(directories,
  114. servicehost.GetConfigFilePath(),
  115. "/config", // For containers.
  116. "/etc/OliveTin/",
  117. )
  118. }
  119. var baseConfigPath string
  120. for _, directory := range directories {
  121. configPath := getConfigPath(directory)
  122. found := true
  123. if _, err := os.Stat(configPath); err != nil {
  124. found = false
  125. }
  126. log.WithFields(log.Fields{
  127. "configPath": configPath,
  128. "found": found,
  129. }).Debug("Checking base config path")
  130. if !found {
  131. continue
  132. }
  133. if baseConfigPath == "" {
  134. baseConfigPath = configPath
  135. }
  136. log.WithFields(log.Fields{
  137. "configPath": configPath,
  138. }).Info("Loading config from path")
  139. f := file.Provider(configPath)
  140. if err := k.Load(f, yaml.Parser()); err != nil {
  141. log.Fatalf("error loading config from %s: %v", configPath, err)
  142. os.Exit(1)
  143. }
  144. err := f.Watch(func(evt interface{}, err error) {
  145. log.Infof("config file changed: %v", evt)
  146. errLoad := k.Load(f, yaml.Parser())
  147. if errLoad != nil {
  148. log.WithFields(log.Fields{
  149. "error": errLoad,
  150. }).Fatalf("Error loading config file")
  151. }
  152. config.AppendSource(cfg, k, configPath)
  153. })
  154. if err != nil {
  155. log.WithFields(log.Fields{
  156. "error": err,
  157. }).Fatalf("Error watching config file")
  158. }
  159. break
  160. }
  161. cfg = config.DefaultConfigWithBasePort(getBasePort())
  162. if baseConfigPath == "" {
  163. log.Fatalf("No base config file found")
  164. os.Exit(1)
  165. }
  166. config.AppendSource(cfg, k, baseConfigPath)
  167. }
  168. func initInstallationInfo() {
  169. installationinfo.Config = cfg
  170. installationinfo.Build.Version = version
  171. installationinfo.Build.Commit = commit
  172. installationinfo.Build.Date = date
  173. }
  174. func logStartupMessage(message string) {
  175. log.WithFields(log.Fields{
  176. "version": version,
  177. "commit": commit,
  178. "date": date,
  179. }).Info(message)
  180. }
  181. func initCheckEnvironment() {
  182. warnIfPuidGuid()
  183. }
  184. func warnIfPuidGuid() {
  185. if os.Getenv("PUID") != "" || os.Getenv("PGID") != "" {
  186. log.Warnf("PUID or PGID seem to be set to something, but they are ignored by OliveTin. Please check https://docs.olivetin.app/no-puid-pgid.html")
  187. }
  188. }
  189. func main() {
  190. servicehost.Start(cfg.ServiceHostMode)
  191. log.WithFields(log.Fields{
  192. "configDir": cfg.GetDir(),
  193. }).Infof("OliveTin started")
  194. log.Debugf("Config: %+v", cfg)
  195. executor := executor.DefaultExecutor(cfg)
  196. executor.RebuildActionMap()
  197. config.AddListener(executor.RebuildActionMap)
  198. executor.LoadLogsFromDisk()
  199. go onstartup.Execute(cfg, executor)
  200. go oncron.Schedule(cfg, executor)
  201. go onfileindir.WatchFilesInDirectory(cfg, executor)
  202. go oncalendarfile.Schedule(cfg, executor)
  203. entities.AddListener(executor.RebuildActionMap)
  204. go entities.SetupEntityFileWatchers(cfg)
  205. go updatecheck.StartUpdateChecker(cfg)
  206. // Load persistent sessions from disk
  207. auth.LoadUserSessions(cfg)
  208. httpservers.StartFrontendMux(cfg, executor)
  209. }