setting.go 18 KB

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