You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

setting.go 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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 setting
  5. import (
  6. "fmt"
  7. "net/url"
  8. "os"
  9. "os/exec"
  10. "path"
  11. "path/filepath"
  12. "runtime"
  13. "strings"
  14. "time"
  15. "github.com/Unknwon/com"
  16. _ "github.com/go-macaron/cache/memcache"
  17. _ "github.com/go-macaron/cache/redis"
  18. "github.com/go-macaron/session"
  19. _ "github.com/go-macaron/session/redis"
  20. "gopkg.in/ini.v1"
  21. "github.com/gogits/gogs/modules/bindata"
  22. "github.com/gogits/gogs/modules/log"
  23. "github.com/gogits/gogs/modules/user"
  24. )
  25. type Scheme string
  26. const (
  27. HTTP Scheme = "http"
  28. HTTPS Scheme = "https"
  29. FCGI Scheme = "fcgi"
  30. )
  31. type LandingPage string
  32. const (
  33. LANDING_PAGE_HOME LandingPage = "/"
  34. LANDING_PAGE_EXPLORE LandingPage = "/explore"
  35. )
  36. var (
  37. // Build information.
  38. BuildTime string
  39. BuildGitHash string
  40. // App settings.
  41. AppVer string
  42. AppName string
  43. AppUrl string
  44. AppSubUrl string
  45. AppPath string
  46. AppDataPath = "data"
  47. // Server settings.
  48. Protocol Scheme
  49. Domain string
  50. HttpAddr, HttpPort string
  51. DisableSSH bool
  52. StartSSHServer bool
  53. SSHDomain string
  54. SSHPort int
  55. OfflineMode bool
  56. DisableRouterLog bool
  57. CertFile, KeyFile string
  58. StaticRootPath string
  59. EnableGzip bool
  60. LandingPageUrl LandingPage
  61. // Security settings.
  62. InstallLock bool
  63. SecretKey string
  64. LogInRememberDays int
  65. CookieUserName string
  66. CookieRememberName string
  67. ReverseProxyAuthUser string
  68. // Database settings.
  69. UseSQLite3 bool
  70. UseMySQL bool
  71. UsePostgreSQL bool
  72. UseTiDB bool
  73. // Webhook settings.
  74. Webhook struct {
  75. QueueLength int
  76. DeliverTimeout int
  77. SkipTLSVerify bool
  78. Types []string
  79. PagingNum int
  80. }
  81. // Repository settings.
  82. Repository struct {
  83. AnsiCharset string
  84. ForcePrivate bool
  85. PullRequestQueueLength int
  86. }
  87. RepoRootPath string
  88. ScriptType string
  89. // UI settings.
  90. ExplorePagingNum int
  91. IssuePagingNum int
  92. FeedMaxCommitNum int
  93. AdminUserPagingNum int
  94. AdminRepoPagingNum int
  95. AdminNoticePagingNum int
  96. AdminOrgPagingNum int
  97. // Markdown sttings.
  98. Markdown struct {
  99. EnableHardLineBreak bool
  100. }
  101. // Picture settings.
  102. PictureService string
  103. AvatarUploadPath string
  104. GravatarSource string
  105. DisableGravatar bool
  106. // Log settings.
  107. LogRootPath string
  108. LogModes []string
  109. LogConfigs []string
  110. // Attachment settings.
  111. AttachmentPath string
  112. AttachmentAllowedTypes string
  113. AttachmentMaxSize int64
  114. AttachmentMaxFiles int
  115. AttachmentEnabled bool
  116. // Time settings.
  117. TimeFormat string
  118. // Cache settings.
  119. CacheAdapter string
  120. CacheInternal int
  121. CacheConn string
  122. // Session settings.
  123. SessionConfig session.Options
  124. // Git settings.
  125. Git struct {
  126. MaxGitDiffLines int
  127. GcArgs []string `delim:" "`
  128. }
  129. // Cron tasks.
  130. Cron struct {
  131. UpdateMirror struct {
  132. Enabled bool
  133. RunAtStart bool
  134. Schedule string
  135. } `ini:"cron.update_mirrors"`
  136. RepoHealthCheck struct {
  137. Enabled bool
  138. RunAtStart bool
  139. Schedule string
  140. Args []string `delim:" "`
  141. } `ini:"cron.repo_health_check"`
  142. CheckRepoStats struct {
  143. Enabled bool
  144. RunAtStart bool
  145. Schedule string
  146. } `ini:"cron.check_repo_stats"`
  147. }
  148. // I18n settings.
  149. Langs, Names []string
  150. dateLangs map[string]string
  151. // Other settings.
  152. ShowFooterBranding bool
  153. ShowFooterVersion bool
  154. // Global setting objects.
  155. Cfg *ini.File
  156. CustomPath string // Custom directory path.
  157. CustomConf string
  158. ProdMode bool
  159. RunUser string
  160. IsWindows bool
  161. HasRobotsTxt bool
  162. )
  163. func DateLang(lang string) string {
  164. name, ok := dateLangs[lang]
  165. if ok {
  166. return name
  167. }
  168. return "en"
  169. }
  170. // execPath returns the executable path.
  171. func execPath() (string, error) {
  172. file, err := exec.LookPath(os.Args[0])
  173. if err != nil {
  174. return "", err
  175. }
  176. return filepath.Abs(file)
  177. }
  178. func init() {
  179. IsWindows = runtime.GOOS == "windows"
  180. log.NewLogger(0, "console", `{"level": 0}`)
  181. var err error
  182. if AppPath, err = execPath(); err != nil {
  183. log.Fatal(4, "fail to get app path: %v\n", err)
  184. }
  185. // Note: we don't use path.Dir here because it does not handle case
  186. // which path starts with two "/" in Windows: "//psf/Home/..."
  187. AppPath = strings.Replace(AppPath, "\\", "/", -1)
  188. }
  189. // WorkDir returns absolute path of work directory.
  190. func WorkDir() (string, error) {
  191. wd := os.Getenv("GOGS_WORK_DIR")
  192. if len(wd) > 0 {
  193. return wd, nil
  194. }
  195. i := strings.LastIndex(AppPath, "/")
  196. if i == -1 {
  197. return AppPath, nil
  198. }
  199. return AppPath[:i], nil
  200. }
  201. func forcePathSeparator(path string) {
  202. if strings.Contains(path, "\\") {
  203. log.Fatal(4, "Do not use '\\' or '\\\\' in paths, instead, please use '/' in all places")
  204. }
  205. }
  206. // NewContext initializes configuration context.
  207. // NOTE: do not print any log except error.
  208. func NewContext() {
  209. workDir, err := WorkDir()
  210. if err != nil {
  211. log.Fatal(4, "Fail to get work directory: %v", err)
  212. }
  213. Cfg, err = ini.Load(bindata.MustAsset("conf/app.ini"))
  214. if err != nil {
  215. log.Fatal(4, "Fail to parse 'conf/app.ini': %v", err)
  216. }
  217. CustomPath = os.Getenv("GOGS_CUSTOM")
  218. if len(CustomPath) == 0 {
  219. CustomPath = workDir + "/custom"
  220. }
  221. if len(CustomConf) == 0 {
  222. CustomConf = CustomPath + "/conf/app.ini"
  223. }
  224. if com.IsFile(CustomConf) {
  225. if err = Cfg.Append(CustomConf); err != nil {
  226. log.Fatal(4, "Fail to load custom conf '%s': %v", CustomConf, err)
  227. }
  228. } else {
  229. log.Warn("Custom config (%s) not found, ignore this if you're running first time", CustomConf)
  230. }
  231. Cfg.NameMapper = ini.AllCapsUnderscore
  232. LogRootPath = Cfg.Section("log").Key("ROOT_PATH").MustString(path.Join(workDir, "log"))
  233. forcePathSeparator(LogRootPath)
  234. sec := Cfg.Section("server")
  235. AppName = Cfg.Section("").Key("APP_NAME").MustString("Gogs: Go Git Service")
  236. AppUrl = sec.Key("ROOT_URL").MustString("http://localhost:3000/")
  237. if AppUrl[len(AppUrl)-1] != '/' {
  238. AppUrl += "/"
  239. }
  240. // Check if has app suburl.
  241. url, err := url.Parse(AppUrl)
  242. if err != nil {
  243. log.Fatal(4, "Invalid ROOT_URL(%s): %s", AppUrl, err)
  244. }
  245. AppSubUrl = strings.TrimSuffix(url.Path, "/")
  246. Protocol = HTTP
  247. if sec.Key("PROTOCOL").String() == "https" {
  248. Protocol = HTTPS
  249. CertFile = sec.Key("CERT_FILE").String()
  250. KeyFile = sec.Key("KEY_FILE").String()
  251. } else if sec.Key("PROTOCOL").String() == "fcgi" {
  252. Protocol = FCGI
  253. }
  254. Domain = sec.Key("DOMAIN").MustString("localhost")
  255. HttpAddr = sec.Key("HTTP_ADDR").MustString("0.0.0.0")
  256. HttpPort = sec.Key("HTTP_PORT").MustString("3000")
  257. DisableSSH = sec.Key("DISABLE_SSH").MustBool()
  258. if !DisableSSH {
  259. StartSSHServer = sec.Key("START_SSH_SERVER").MustBool()
  260. }
  261. SSHDomain = sec.Key("SSH_DOMAIN").MustString(Domain)
  262. SSHPort = sec.Key("SSH_PORT").MustInt(22)
  263. OfflineMode = sec.Key("OFFLINE_MODE").MustBool()
  264. DisableRouterLog = sec.Key("DISABLE_ROUTER_LOG").MustBool()
  265. StaticRootPath = sec.Key("STATIC_ROOT_PATH").MustString(workDir)
  266. EnableGzip = sec.Key("ENABLE_GZIP").MustBool()
  267. switch sec.Key("LANDING_PAGE").MustString("home") {
  268. case "explore":
  269. LandingPageUrl = LANDING_PAGE_EXPLORE
  270. default:
  271. LandingPageUrl = LANDING_PAGE_HOME
  272. }
  273. sec = Cfg.Section("security")
  274. InstallLock = sec.Key("INSTALL_LOCK").MustBool()
  275. SecretKey = sec.Key("SECRET_KEY").String()
  276. LogInRememberDays = sec.Key("LOGIN_REMEMBER_DAYS").MustInt()
  277. CookieUserName = sec.Key("COOKIE_USERNAME").String()
  278. CookieRememberName = sec.Key("COOKIE_REMEMBER_NAME").String()
  279. ReverseProxyAuthUser = sec.Key("REVERSE_PROXY_AUTHENTICATION_USER").MustString("X-WEBAUTH-USER")
  280. sec = Cfg.Section("attachment")
  281. AttachmentPath = sec.Key("PATH").MustString(path.Join(AppDataPath, "attachments"))
  282. if !filepath.IsAbs(AttachmentPath) {
  283. AttachmentPath = path.Join(workDir, AttachmentPath)
  284. }
  285. AttachmentAllowedTypes = strings.Replace(sec.Key("ALLOWED_TYPES").MustString("image/jpeg,image/png"), "|", ",", -1)
  286. AttachmentMaxSize = sec.Key("MAX_SIZE").MustInt64(4)
  287. AttachmentMaxFiles = sec.Key("MAX_FILES").MustInt(5)
  288. AttachmentEnabled = sec.Key("ENABLE").MustBool(true)
  289. TimeFormat = map[string]string{
  290. "ANSIC": time.ANSIC,
  291. "UnixDate": time.UnixDate,
  292. "RubyDate": time.RubyDate,
  293. "RFC822": time.RFC822,
  294. "RFC822Z": time.RFC822Z,
  295. "RFC850": time.RFC850,
  296. "RFC1123": time.RFC1123,
  297. "RFC1123Z": time.RFC1123Z,
  298. "RFC3339": time.RFC3339,
  299. "RFC3339Nano": time.RFC3339Nano,
  300. "Kitchen": time.Kitchen,
  301. "Stamp": time.Stamp,
  302. "StampMilli": time.StampMilli,
  303. "StampMicro": time.StampMicro,
  304. "StampNano": time.StampNano,
  305. }[Cfg.Section("time").Key("FORMAT").MustString("RFC1123")]
  306. RunUser = Cfg.Section("").Key("RUN_USER").String()
  307. curUser := user.CurrentUsername()
  308. // Does not check run user when the install lock is off.
  309. if InstallLock && RunUser != curUser {
  310. log.Fatal(4, "Expect user(%s) but current user is: %s", RunUser, curUser)
  311. }
  312. // Determine and create root git repository path.
  313. homeDir, err := com.HomeDir()
  314. if err != nil {
  315. log.Fatal(4, "Fail to get home directory: %v", err)
  316. }
  317. homeDir = strings.Replace(homeDir, "\\", "/", -1)
  318. sec = Cfg.Section("repository")
  319. RepoRootPath = sec.Key("ROOT").MustString(path.Join(homeDir, "gogs-repositories"))
  320. forcePathSeparator(RepoRootPath)
  321. if !filepath.IsAbs(RepoRootPath) {
  322. RepoRootPath = path.Join(workDir, RepoRootPath)
  323. } else {
  324. RepoRootPath = path.Clean(RepoRootPath)
  325. }
  326. ScriptType = sec.Key("SCRIPT_TYPE").MustString("bash")
  327. Repository.AnsiCharset = sec.Key("ANSI_CHARSET").String()
  328. Repository.ForcePrivate = sec.Key("FORCE_PRIVATE").MustBool()
  329. Repository.PullRequestQueueLength = sec.Key("PULL_REQUEST_QUEUE_LENGTH").MustInt(10000)
  330. // UI settings.
  331. sec = Cfg.Section("ui")
  332. ExplorePagingNum = sec.Key("EXPLORE_PAGING_NUM").MustInt(20)
  333. IssuePagingNum = sec.Key("ISSUE_PAGING_NUM").MustInt(10)
  334. FeedMaxCommitNum = sec.Key("FEED_MAX_COMMIT_NUM").MustInt(5)
  335. sec = Cfg.Section("ui.admin")
  336. AdminUserPagingNum = sec.Key("USER_PAGING_NUM").MustInt(50)
  337. AdminRepoPagingNum = sec.Key("REPO_PAGING_NUM").MustInt(50)
  338. AdminNoticePagingNum = sec.Key("NOTICE_PAGING_NUM").MustInt(50)
  339. AdminOrgPagingNum = sec.Key("ORG_PAGING_NUM").MustInt(50)
  340. sec = Cfg.Section("picture")
  341. PictureService = sec.Key("SERVICE").In("server", []string{"server"})
  342. AvatarUploadPath = sec.Key("AVATAR_UPLOAD_PATH").MustString(path.Join(AppDataPath, "avatars"))
  343. forcePathSeparator(AvatarUploadPath)
  344. if !filepath.IsAbs(AvatarUploadPath) {
  345. AvatarUploadPath = path.Join(workDir, AvatarUploadPath)
  346. }
  347. switch source := sec.Key("GRAVATAR_SOURCE").MustString("gravatar"); source {
  348. case "duoshuo":
  349. GravatarSource = "http://gravatar.duoshuo.com/avatar/"
  350. case "gravatar":
  351. GravatarSource = "//1.gravatar.com/avatar/"
  352. default:
  353. GravatarSource = source
  354. }
  355. DisableGravatar = sec.Key("DISABLE_GRAVATAR").MustBool()
  356. if OfflineMode {
  357. DisableGravatar = true
  358. }
  359. if err = Cfg.Section("markdown").MapTo(&Markdown); err != nil {
  360. log.Fatal(4, "Fail to map Markdown settings: %v", err)
  361. } else if err = Cfg.Section("git").MapTo(&Git); err != nil {
  362. log.Fatal(4, "Fail to map Git settings: %v", err)
  363. } else if Cfg.Section("cron").MapTo(&Cron); err != nil {
  364. log.Fatal(4, "Fail to map Cron settings: %v", err)
  365. }
  366. Langs = Cfg.Section("i18n").Key("LANGS").Strings(",")
  367. Names = Cfg.Section("i18n").Key("NAMES").Strings(",")
  368. dateLangs = Cfg.Section("i18n.datelang").KeysHash()
  369. ShowFooterBranding = Cfg.Section("other").Key("SHOW_FOOTER_BRANDING").MustBool()
  370. ShowFooterVersion = Cfg.Section("other").Key("SHOW_FOOTER_VERSION").MustBool()
  371. HasRobotsTxt = com.IsFile(path.Join(CustomPath, "robots.txt"))
  372. }
  373. var Service struct {
  374. ActiveCodeLives int
  375. ResetPwdCodeLives int
  376. RegisterEmailConfirm bool
  377. DisableRegistration bool
  378. ShowRegistrationButton bool
  379. RequireSignInView bool
  380. EnableCacheAvatar bool
  381. EnableNotifyMail bool
  382. EnableReverseProxyAuth bool
  383. EnableReverseProxyAutoRegister bool
  384. DisableMinimumKeySizeCheck bool
  385. MinimumKeySizes map[string]int
  386. EnableCaptcha bool
  387. }
  388. func newService() {
  389. sec := Cfg.Section("service")
  390. Service.ActiveCodeLives = sec.Key("ACTIVE_CODE_LIVE_MINUTES").MustInt(180)
  391. Service.ResetPwdCodeLives = sec.Key("RESET_PASSWD_CODE_LIVE_MINUTES").MustInt(180)
  392. Service.DisableRegistration = sec.Key("DISABLE_REGISTRATION").MustBool()
  393. Service.ShowRegistrationButton = sec.Key("SHOW_REGISTRATION_BUTTON").MustBool(!Service.DisableRegistration)
  394. Service.RequireSignInView = sec.Key("REQUIRE_SIGNIN_VIEW").MustBool()
  395. Service.EnableCacheAvatar = sec.Key("ENABLE_CACHE_AVATAR").MustBool()
  396. Service.EnableReverseProxyAuth = sec.Key("ENABLE_REVERSE_PROXY_AUTHENTICATION").MustBool()
  397. Service.EnableReverseProxyAutoRegister = sec.Key("ENABLE_REVERSE_PROXY_AUTO_REGISTRATION").MustBool()
  398. Service.DisableMinimumKeySizeCheck = sec.Key("DISABLE_MINIMUM_KEY_SIZE_CHECK").MustBool()
  399. Service.EnableCaptcha = sec.Key("ENABLE_CAPTCHA").MustBool()
  400. minimumKeySizes := Cfg.Section("service.minimum_key_sizes").Keys()
  401. Service.MinimumKeySizes = make(map[string]int)
  402. for _, key := range minimumKeySizes {
  403. Service.MinimumKeySizes[key.Name()] = key.MustInt()
  404. }
  405. }
  406. var logLevels = map[string]string{
  407. "Trace": "0",
  408. "Debug": "1",
  409. "Info": "2",
  410. "Warn": "3",
  411. "Error": "4",
  412. "Critical": "5",
  413. }
  414. func newLogService() {
  415. log.Info("%s %s", AppName, AppVer)
  416. if len(BuildTime) > 0 {
  417. log.Info("Build Time: %s", BuildTime)
  418. log.Info("Build Git Hash: %s", BuildGitHash)
  419. }
  420. // Get and check log mode.
  421. LogModes = strings.Split(Cfg.Section("log").Key("MODE").MustString("console"), ",")
  422. LogConfigs = make([]string, len(LogModes))
  423. for i, mode := range LogModes {
  424. mode = strings.TrimSpace(mode)
  425. sec, err := Cfg.GetSection("log." + mode)
  426. if err != nil {
  427. log.Fatal(4, "Unknown log mode: %s", mode)
  428. }
  429. validLevels := []string{"Trace", "Debug", "Info", "Warn", "Error", "Critical"}
  430. // Log level.
  431. levelName := Cfg.Section("log."+mode).Key("LEVEL").In(
  432. Cfg.Section("log").Key("LEVEL").In("Trace", validLevels),
  433. validLevels)
  434. level, ok := logLevels[levelName]
  435. if !ok {
  436. log.Fatal(4, "Unknown log level: %s", levelName)
  437. }
  438. // Generate log configuration.
  439. switch mode {
  440. case "console":
  441. LogConfigs[i] = fmt.Sprintf(`{"level":%s}`, level)
  442. case "file":
  443. logPath := sec.Key("FILE_NAME").MustString(path.Join(LogRootPath, "gogs.log"))
  444. os.MkdirAll(path.Dir(logPath), os.ModePerm)
  445. LogConfigs[i] = fmt.Sprintf(
  446. `{"level":%s,"filename":"%s","rotate":%v,"maxlines":%d,"maxsize":%d,"daily":%v,"maxdays":%d}`, level,
  447. logPath,
  448. sec.Key("LOG_ROTATE").MustBool(true),
  449. sec.Key("MAX_LINES").MustInt(1000000),
  450. 1<<uint(sec.Key("MAX_SIZE_SHIFT").MustInt(28)),
  451. sec.Key("DAILY_ROTATE").MustBool(true),
  452. sec.Key("MAX_DAYS").MustInt(7))
  453. case "conn":
  454. LogConfigs[i] = fmt.Sprintf(`{"level":%s,"reconnectOnMsg":%v,"reconnect":%v,"net":"%s","addr":"%s"}`, level,
  455. sec.Key("RECONNECT_ON_MSG").MustBool(),
  456. sec.Key("RECONNECT").MustBool(),
  457. sec.Key("PROTOCOL").In("tcp", []string{"tcp", "unix", "udp"}),
  458. sec.Key("ADDR").MustString(":7020"))
  459. case "smtp":
  460. LogConfigs[i] = fmt.Sprintf(`{"level":%s,"username":"%s","password":"%s","host":"%s","sendTos":"%s","subject":"%s"}`, level,
  461. sec.Key("USER").MustString("example@example.com"),
  462. sec.Key("PASSWD").MustString("******"),
  463. sec.Key("HOST").MustString("127.0.0.1:25"),
  464. sec.Key("RECEIVERS").MustString("[]"),
  465. sec.Key("SUBJECT").MustString("Diagnostic message from serve"))
  466. case "database":
  467. LogConfigs[i] = fmt.Sprintf(`{"level":%s,"driver":"%s","conn":"%s"}`, level,
  468. sec.Key("DRIVER").String(),
  469. sec.Key("CONN").String())
  470. }
  471. log.NewLogger(Cfg.Section("log").Key("BUFFER_LEN").MustInt64(10000), mode, LogConfigs[i])
  472. log.Info("Log Mode: %s(%s)", strings.Title(mode), levelName)
  473. }
  474. }
  475. func newCacheService() {
  476. CacheAdapter = Cfg.Section("cache").Key("ADAPTER").In("memory", []string{"memory", "redis", "memcache"})
  477. switch CacheAdapter {
  478. case "memory":
  479. CacheInternal = Cfg.Section("cache").Key("INTERVAL").MustInt(60)
  480. case "redis", "memcache":
  481. CacheConn = strings.Trim(Cfg.Section("cache").Key("HOST").String(), "\" ")
  482. default:
  483. log.Fatal(4, "Unknown cache adapter: %s", CacheAdapter)
  484. }
  485. log.Info("Cache Service Enabled")
  486. }
  487. func newSessionService() {
  488. SessionConfig.Provider = Cfg.Section("session").Key("PROVIDER").In("memory",
  489. []string{"memory", "file", "redis", "mysql"})
  490. SessionConfig.ProviderConfig = strings.Trim(Cfg.Section("session").Key("PROVIDER_CONFIG").String(), "\" ")
  491. SessionConfig.CookieName = Cfg.Section("session").Key("COOKIE_NAME").MustString("i_like_gogits")
  492. SessionConfig.CookiePath = AppSubUrl
  493. SessionConfig.Secure = Cfg.Section("session").Key("COOKIE_SECURE").MustBool()
  494. SessionConfig.Gclifetime = Cfg.Section("session").Key("GC_INTERVAL_TIME").MustInt64(86400)
  495. SessionConfig.Maxlifetime = Cfg.Section("session").Key("SESSION_LIFE_TIME").MustInt64(86400)
  496. log.Info("Session Service Enabled")
  497. }
  498. // Mailer represents mail service.
  499. type Mailer struct {
  500. QueueLength int
  501. Name string
  502. Host string
  503. From string
  504. User, Passwd string
  505. DisableHelo bool
  506. HeloHostname string
  507. SkipVerify bool
  508. UseCertificate bool
  509. CertFile, KeyFile string
  510. }
  511. var (
  512. MailService *Mailer
  513. )
  514. func newMailService() {
  515. sec := Cfg.Section("mailer")
  516. // Check mailer setting.
  517. if !sec.Key("ENABLED").MustBool() {
  518. return
  519. }
  520. MailService = &Mailer{
  521. QueueLength: sec.Key("SEND_BUFFER_LEN").MustInt(100),
  522. Name: sec.Key("NAME").MustString(AppName),
  523. Host: sec.Key("HOST").String(),
  524. User: sec.Key("USER").String(),
  525. Passwd: sec.Key("PASSWD").String(),
  526. DisableHelo: sec.Key("DISABLE_HELO").MustBool(),
  527. HeloHostname: sec.Key("HELO_HOSTNAME").String(),
  528. SkipVerify: sec.Key("SKIP_VERIFY").MustBool(),
  529. UseCertificate: sec.Key("USE_CERTIFICATE").MustBool(),
  530. CertFile: sec.Key("CERT_FILE").String(),
  531. KeyFile: sec.Key("KEY_FILE").String(),
  532. }
  533. MailService.From = sec.Key("FROM").MustString(MailService.User)
  534. log.Info("Mail Service Enabled")
  535. }
  536. func newRegisterMailService() {
  537. if !Cfg.Section("service").Key("REGISTER_EMAIL_CONFIRM").MustBool() {
  538. return
  539. } else if MailService == nil {
  540. log.Warn("Register Mail Service: Mail Service is not enabled")
  541. return
  542. }
  543. Service.RegisterEmailConfirm = true
  544. log.Info("Register Mail Service Enabled")
  545. }
  546. func newNotifyMailService() {
  547. if !Cfg.Section("service").Key("ENABLE_NOTIFY_MAIL").MustBool() {
  548. return
  549. } else if MailService == nil {
  550. log.Warn("Notify Mail Service: Mail Service is not enabled")
  551. return
  552. }
  553. Service.EnableNotifyMail = true
  554. log.Info("Notify Mail Service Enabled")
  555. }
  556. func newWebhookService() {
  557. sec := Cfg.Section("webhook")
  558. Webhook.QueueLength = sec.Key("QUEUE_LENGTH").MustInt(1000)
  559. Webhook.DeliverTimeout = sec.Key("DELIVER_TIMEOUT").MustInt(5)
  560. Webhook.SkipTLSVerify = sec.Key("SKIP_TLS_VERIFY").MustBool()
  561. Webhook.Types = []string{"gogs", "slack"}
  562. Webhook.PagingNum = sec.Key("PAGING_NUM").MustInt(10)
  563. }
  564. func NewServices() {
  565. newService()
  566. newLogService()
  567. newCacheService()
  568. newSessionService()
  569. newMailService()
  570. newRegisterMailService()
  571. newNotifyMailService()
  572. newWebhookService()
  573. }