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.

636 lines
18 KiB

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