conf.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package base
  5. import (
  6. "fmt"
  7. "os"
  8. "os/exec"
  9. "path"
  10. "path/filepath"
  11. "strings"
  12. "github.com/Unknwon/com"
  13. "github.com/Unknwon/goconfig"
  14. "github.com/gogits/cache"
  15. "github.com/gogits/session"
  16. "github.com/gogits/gogs/modules/log"
  17. )
  18. // Mailer represents a mail service.
  19. type Mailer struct {
  20. Name string
  21. Host string
  22. User, Passwd string
  23. }
  24. var (
  25. AppVer string
  26. AppName string
  27. AppLogo string
  28. AppUrl string
  29. Domain string
  30. SecretKey string
  31. RunUser string
  32. RepoRootPath string
  33. Cfg *goconfig.ConfigFile
  34. MailService *Mailer
  35. LogMode string
  36. LogConfig string
  37. Cache cache.Cache
  38. CacheAdapter string
  39. CacheConfig string
  40. SessionProvider string
  41. SessionConfig *session.Config
  42. SessionManager *session.Manager
  43. PictureService string
  44. PictureRootPath string
  45. )
  46. var Service struct {
  47. RegisterEmailConfirm bool
  48. DisenableRegisteration bool
  49. RequireSignInView bool
  50. EnableCacheAvatar bool
  51. ActiveCodeLives int
  52. ResetPwdCodeLives int
  53. }
  54. func exeDir() (string, error) {
  55. file, err := exec.LookPath(os.Args[0])
  56. if err != nil {
  57. return "", err
  58. }
  59. p, err := filepath.Abs(file)
  60. if err != nil {
  61. return "", err
  62. }
  63. return path.Dir(p), nil
  64. }
  65. var logLevels = map[string]string{
  66. "Trace": "0",
  67. "Debug": "1",
  68. "Info": "2",
  69. "Warn": "3",
  70. "Error": "4",
  71. "Critical": "5",
  72. }
  73. func newService() {
  74. Service.ActiveCodeLives = Cfg.MustInt("service", "ACTIVE_CODE_LIVE_MINUTES", 180)
  75. Service.ResetPwdCodeLives = Cfg.MustInt("service", "RESET_PASSWD_CODE_LIVE_MINUTES", 180)
  76. Service.DisenableRegisteration = Cfg.MustBool("service", "DISENABLE_REGISTERATION", false)
  77. Service.RequireSignInView = Cfg.MustBool("service", "REQUIRE_SIGNIN_VIEW", false)
  78. Service.EnableCacheAvatar = Cfg.MustBool("service", "ENABLE_CACHE_AVATAR", false)
  79. }
  80. func newLogService() {
  81. // Get and check log mode.
  82. LogMode = Cfg.MustValue("log", "MODE", "console")
  83. modeSec := "log." + LogMode
  84. if _, err := Cfg.GetSection(modeSec); err != nil {
  85. fmt.Printf("Unknown log mode: %s\n", LogMode)
  86. os.Exit(2)
  87. }
  88. // Log level.
  89. levelName := Cfg.MustValue("log."+LogMode, "LEVEL", "Trace")
  90. level, ok := logLevels[levelName]
  91. if !ok {
  92. fmt.Printf("Unknown log level: %s\n", levelName)
  93. os.Exit(2)
  94. }
  95. // Generate log configuration.
  96. switch LogMode {
  97. case "console":
  98. LogConfig = fmt.Sprintf(`{"level":%s}`, level)
  99. case "file":
  100. logPath := Cfg.MustValue(modeSec, "FILE_NAME", "log/gogs.log")
  101. os.MkdirAll(path.Dir(logPath), os.ModePerm)
  102. LogConfig = fmt.Sprintf(
  103. `{"level":%s,"filename":%s,"rotate":%v,"maxlines":%d,"maxsize",%d,"daily":%v,"maxdays":%d}`, level,
  104. logPath,
  105. Cfg.MustBool(modeSec, "LOG_ROTATE", true),
  106. Cfg.MustInt(modeSec, "MAX_LINES", 1000000),
  107. 1<<uint(Cfg.MustInt(modeSec, "MAX_SIZE_SHIFT", 28)),
  108. Cfg.MustBool(modeSec, "DAILY_ROTATE", true),
  109. Cfg.MustInt(modeSec, "MAX_DAYS", 7))
  110. case "conn":
  111. LogConfig = fmt.Sprintf(`{"level":%s,"reconnectOnMsg":%v,"reconnect":%v,"net":%s,"addr":%s}`, level,
  112. Cfg.MustBool(modeSec, "RECONNECT_ON_MSG", false),
  113. Cfg.MustBool(modeSec, "RECONNECT", false),
  114. Cfg.MustValue(modeSec, "PROTOCOL", "tcp"),
  115. Cfg.MustValue(modeSec, "ADDR", ":7020"))
  116. case "smtp":
  117. LogConfig = fmt.Sprintf(`{"level":%s,"username":%s,"password":%s,"host":%s,"sendTos":%s,"subject":%s}`, level,
  118. Cfg.MustValue(modeSec, "USER", "example@example.com"),
  119. Cfg.MustValue(modeSec, "PASSWD", "******"),
  120. Cfg.MustValue(modeSec, "HOST", "127.0.0.1:25"),
  121. Cfg.MustValue(modeSec, "RECEIVERS", "[]"),
  122. Cfg.MustValue(modeSec, "SUBJECT", "Diagnostic message from serve"))
  123. }
  124. log.NewLogger(Cfg.MustInt64("log", "BUFFER_LEN", 10000), LogMode, LogConfig)
  125. log.Info("Log Mode: %s(%s)", strings.Title(LogMode), levelName)
  126. }
  127. func newCacheService() {
  128. CacheAdapter = Cfg.MustValue("cache", "ADAPTER", "memory")
  129. switch CacheAdapter {
  130. case "memory":
  131. CacheConfig = fmt.Sprintf(`{"interval":%d}`, Cfg.MustInt("cache", "INTERVAL", 60))
  132. case "redis", "memcache":
  133. CacheConfig = fmt.Sprintf(`{"conn":"%s"}`, Cfg.MustValue("cache", "HOST"))
  134. default:
  135. fmt.Printf("Unknown cache adapter: %s\n", CacheAdapter)
  136. os.Exit(2)
  137. }
  138. var err error
  139. Cache, err = cache.NewCache(CacheAdapter, CacheConfig)
  140. if err != nil {
  141. fmt.Printf("Init cache system failed, adapter: %s, config: %s, %v\n",
  142. CacheAdapter, CacheConfig, err)
  143. os.Exit(2)
  144. }
  145. log.Info("Cache Service Enabled")
  146. }
  147. func newSessionService() {
  148. SessionProvider = Cfg.MustValue("session", "PROVIDER", "memory")
  149. SessionConfig = new(session.Config)
  150. SessionConfig.ProviderConfig = Cfg.MustValue("session", "PROVIDER_CONFIG")
  151. SessionConfig.CookieName = Cfg.MustValue("session", "COOKIE_NAME", "i_like_gogits")
  152. SessionConfig.CookieSecure = Cfg.MustBool("session", "COOKIE_SECURE")
  153. SessionConfig.EnableSetCookie = Cfg.MustBool("session", "ENABLE_SET_COOKIE", true)
  154. SessionConfig.GcIntervalTime = Cfg.MustInt64("session", "GC_INTERVAL_TIME", 86400)
  155. SessionConfig.SessionLifeTime = Cfg.MustInt64("session", "SESSION_LIFE_TIME", 86400)
  156. SessionConfig.SessionIDHashFunc = Cfg.MustValue("session", "SESSION_ID_HASHFUNC", "sha1")
  157. SessionConfig.SessionIDHashKey = Cfg.MustValue("session", "SESSION_ID_HASHKEY")
  158. if SessionProvider == "file" {
  159. os.MkdirAll(path.Dir(SessionConfig.ProviderConfig), os.ModePerm)
  160. }
  161. var err error
  162. SessionManager, err = session.NewManager(SessionProvider, *SessionConfig)
  163. if err != nil {
  164. fmt.Printf("Init session system failed, provider: %s, %v\n",
  165. SessionProvider, err)
  166. os.Exit(2)
  167. }
  168. log.Info("Session Service Enabled")
  169. }
  170. func newMailService() {
  171. // Check mailer setting.
  172. if Cfg.MustBool("mailer", "ENABLED") {
  173. MailService = &Mailer{
  174. Name: Cfg.MustValue("mailer", "NAME", AppName),
  175. Host: Cfg.MustValue("mailer", "HOST", "127.0.0.1:25"),
  176. User: Cfg.MustValue("mailer", "USER", "example@example.com"),
  177. Passwd: Cfg.MustValue("mailer", "PASSWD", "******"),
  178. }
  179. log.Info("Mail Service Enabled")
  180. }
  181. }
  182. func newRegisterMailService() {
  183. if !Cfg.MustBool("service", "REGISTER_EMAIL_CONFIRM") {
  184. return
  185. } else if MailService == nil {
  186. log.Warn("Register Mail Service: Mail Service is not enabled")
  187. return
  188. }
  189. Service.RegisterEmailConfirm = true
  190. log.Info("Register Mail Service Enabled")
  191. }
  192. func NewConfigContext() {
  193. var err error
  194. workDir, err := exeDir()
  195. if err != nil {
  196. fmt.Printf("Fail to get work directory: %s\n", err)
  197. os.Exit(2)
  198. }
  199. cfgPath := filepath.Join(workDir, "conf/app.ini")
  200. Cfg, err = goconfig.LoadConfigFile(cfgPath)
  201. if err != nil {
  202. fmt.Printf("Cannot load config file '%s'\n", cfgPath)
  203. os.Exit(2)
  204. }
  205. Cfg.BlockMode = false
  206. cfgPath = filepath.Join(workDir, "custom/conf/app.ini")
  207. if com.IsFile(cfgPath) {
  208. if err = Cfg.AppendFiles(cfgPath); err != nil {
  209. fmt.Printf("Cannot load config file '%s'\n", cfgPath)
  210. os.Exit(2)
  211. }
  212. }
  213. AppName = Cfg.MustValue("", "APP_NAME", "Gogs: Go Git Service")
  214. AppLogo = Cfg.MustValue("", "APP_LOGO", "img/favicon.png")
  215. AppUrl = Cfg.MustValue("server", "ROOT_URL")
  216. Domain = Cfg.MustValue("server", "DOMAIN")
  217. SecretKey = Cfg.MustValue("security", "SECRET_KEY")
  218. RunUser = Cfg.MustValue("", "RUN_USER")
  219. PictureService = Cfg.MustValue("picture", "SERVICE")
  220. PictureRootPath = Cfg.MustValue("picture", "PATH")
  221. // Determine and create root git reposiroty path.
  222. RepoRootPath = Cfg.MustValue("repository", "ROOT")
  223. if err = os.MkdirAll(RepoRootPath, os.ModePerm); err != nil {
  224. fmt.Printf("models.init(fail to create RepoRootPath(%s)): %v\n", RepoRootPath, err)
  225. os.Exit(2)
  226. }
  227. }
  228. func NewServices() {
  229. newService()
  230. newLogService()
  231. newCacheService()
  232. newSessionService()
  233. newMailService()
  234. newRegisterMailService()
  235. }