conf.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. Cache cache.Cache
  36. CacheAdapter string
  37. CacheConfig string
  38. PictureService string
  39. PictureRootPath string
  40. LogMode string
  41. LogConfig string
  42. SessionProvider string
  43. SessionConfig *session.Config
  44. SessionManager *session.Manager
  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. var err error
  159. SessionManager, err = session.NewManager(SessionProvider, *SessionConfig)
  160. if err != nil {
  161. fmt.Printf("Init session system failed, provider: %s, %v\n",
  162. SessionProvider, err)
  163. os.Exit(2)
  164. }
  165. log.Info("Session Service Enabled")
  166. }
  167. func newMailService() {
  168. // Check mailer setting.
  169. if Cfg.MustBool("mailer", "ENABLED") {
  170. MailService = &Mailer{
  171. Name: Cfg.MustValue("mailer", "NAME", AppName),
  172. Host: Cfg.MustValue("mailer", "HOST", "127.0.0.1:25"),
  173. User: Cfg.MustValue("mailer", "USER", "example@example.com"),
  174. Passwd: Cfg.MustValue("mailer", "PASSWD", "******"),
  175. }
  176. log.Info("Mail Service Enabled")
  177. }
  178. }
  179. func newRegisterMailService() {
  180. if !Cfg.MustBool("service", "REGISTER_EMAIL_CONFIRM") {
  181. return
  182. } else if MailService == nil {
  183. log.Warn("Register Mail Service: Mail Service is not enabled")
  184. return
  185. }
  186. Service.RegisterEmailConfirm = true
  187. log.Info("Register Mail Service Enabled")
  188. }
  189. func NewConfigContext() {
  190. var err error
  191. workDir, err := exeDir()
  192. if err != nil {
  193. fmt.Printf("Fail to get work directory: %s\n", err)
  194. os.Exit(2)
  195. }
  196. cfgPath := filepath.Join(workDir, "conf/app.ini")
  197. Cfg, err = goconfig.LoadConfigFile(cfgPath)
  198. if err != nil {
  199. fmt.Printf("Cannot load config file '%s'\n", cfgPath)
  200. os.Exit(2)
  201. }
  202. Cfg.BlockMode = false
  203. cfgPath = filepath.Join(workDir, "custom/conf/app.ini")
  204. if com.IsFile(cfgPath) {
  205. if err = Cfg.AppendFiles(cfgPath); err != nil {
  206. fmt.Printf("Cannot load config file '%s'\n", cfgPath)
  207. os.Exit(2)
  208. }
  209. }
  210. AppName = Cfg.MustValue("", "APP_NAME", "Gogs: Go Git Service")
  211. AppLogo = Cfg.MustValue("", "APP_LOGO", "img/favicon.png")
  212. AppUrl = Cfg.MustValue("server", "ROOT_URL")
  213. Domain = Cfg.MustValue("server", "DOMAIN")
  214. SecretKey = Cfg.MustValue("security", "SECRET_KEY")
  215. RunUser = Cfg.MustValue("", "RUN_USER")
  216. PictureService = Cfg.MustValue("picture", "SERVICE")
  217. PictureRootPath = Cfg.MustValue("picture", "PATH")
  218. // Determine and create root git reposiroty path.
  219. RepoRootPath = Cfg.MustValue("repository", "ROOT")
  220. if err = os.MkdirAll(RepoRootPath, os.ModePerm); err != nil {
  221. fmt.Printf("models.init(fail to create RepoRootPath(%s)): %v\n", RepoRootPath, err)
  222. os.Exit(2)
  223. }
  224. }
  225. func NewServices() {
  226. newService()
  227. newLogService()
  228. newCacheService()
  229. newSessionService()
  230. newMailService()
  231. newRegisterMailService()
  232. }