static.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. // Copyright 2020 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 conf
  5. import (
  6. "net/url"
  7. "os"
  8. "time"
  9. "github.com/gogs/go-libravatar"
  10. )
  11. // ℹ️ README: This file contains static values that should only be set at initialization time.
  12. // HasMinWinSvc is whether the application is built with Windows Service support.
  13. //
  14. // ⚠️ WARNING: should only be set by "internal/conf/static_minwinsvc.go".
  15. var HasMinWinSvc bool
  16. // Build time and commit information.
  17. //
  18. // ⚠️ WARNING: should only be set by "-ldflags".
  19. var (
  20. BuildTime string
  21. BuildCommit string
  22. )
  23. // CustomConf returns the absolute path of custom configuration file that is used.
  24. var CustomConf string
  25. // ⚠️ WARNING: After changing the following section, do not forget to update template of
  26. // "/admin/config" page as well.
  27. var (
  28. // Application settings
  29. App struct {
  30. // ⚠️ WARNING: Should only be set by the main package (i.e. "gogs.go").
  31. Version string `ini:"-"`
  32. BrandName string
  33. RunUser string
  34. RunMode string
  35. // Deprecated: Use BrandName instead, will be removed in 0.13.
  36. AppName string
  37. }
  38. // SSH settings
  39. SSH struct {
  40. Disabled bool `ini:"DISABLE_SSH"`
  41. Domain string `ini:"SSH_DOMAIN"`
  42. Port int `ini:"SSH_PORT"`
  43. RootPath string `ini:"SSH_ROOT_PATH"`
  44. KeygenPath string `ini:"SSH_KEYGEN_PATH"`
  45. KeyTestPath string `ini:"SSH_KEY_TEST_PATH"`
  46. MinimumKeySizeCheck bool
  47. MinimumKeySizes map[string]int `ini:"-"` // Load from [ssh.minimum_key_sizes]
  48. RewriteAuthorizedKeysAtStart bool
  49. StartBuiltinServer bool `ini:"START_SSH_SERVER"`
  50. ListenHost string `ini:"SSH_LISTEN_HOST"`
  51. ListenPort int `ini:"SSH_LISTEN_PORT"`
  52. ServerCiphers []string `ini:"SSH_SERVER_CIPHERS"`
  53. }
  54. // Repository settings
  55. Repository struct {
  56. Root string
  57. ScriptType string
  58. ANSICharset string `ini:"ANSI_CHARSET"`
  59. ForcePrivate bool
  60. MaxCreationLimit int
  61. PreferredLicenses []string
  62. DisableHTTPGit bool `ini:"DISABLE_HTTP_GIT"`
  63. EnableLocalPathMigration bool
  64. EnableRawFileRenderMode bool
  65. CommitsFetchConcurrency int
  66. // GIN settings
  67. ShowHTTPGit bool `ini:"SHOW_HTTP_GIT"`
  68. RawCaptchaMinFileSize int64
  69. CaptchaMinFileSize int64
  70. AutoInit bool
  71. // Repository editor settings
  72. Editor struct {
  73. LineWrapExtensions []string
  74. PreviewableFileModes []string
  75. } `ini:"repository.editor"`
  76. // Repository upload settings
  77. Upload struct {
  78. Enabled bool
  79. TempPath string
  80. AllowedTypes []string `delim:"|"`
  81. FileMaxSize int64
  82. MaxFiles int
  83. AnnexFileMinSize int64
  84. } `ini:"repository.upload"`
  85. }
  86. // Security settings
  87. Security struct {
  88. InstallLock bool
  89. SecretKey string
  90. LoginRememberDays int
  91. CookieRememberName string
  92. CookieUsername string
  93. CookieSecure bool
  94. EnableLoginStatusCookie bool
  95. LoginStatusCookieName string
  96. // Deprecated: Use Auth.ReverseProxyAuthenticationHeader instead, will be removed in 0.13.
  97. ReverseProxyAuthenticationUser string
  98. }
  99. // Email settings
  100. Email struct {
  101. Enabled bool
  102. SubjectPrefix string
  103. Host string
  104. From string
  105. User string
  106. Password string
  107. DisableHELO bool `ini:"DISABLE_HELO"`
  108. HELOHostname string `ini:"HELO_HOSTNAME"`
  109. SkipVerify bool
  110. UseCertificate bool
  111. CertFile string
  112. KeyFile string
  113. UsePlainText bool
  114. AddPlainTextAlt bool
  115. // Derived from other static values
  116. FromEmail string `ini:"-"` // Parsed email address of From without person's name.
  117. // Deprecated: Use Password instead, will be removed in 0.13.
  118. Passwd string
  119. }
  120. // Authentication settings
  121. Auth struct {
  122. ActivateCodeLives int
  123. ResetPasswordCodeLives int
  124. RequireEmailConfirmation bool
  125. RequireSigninView bool
  126. DisableRegistration bool
  127. EnableRegistrationCaptcha bool
  128. EnableReverseProxyAuthentication bool
  129. EnableReverseProxyAutoRegistration bool
  130. ReverseProxyAuthenticationHeader string
  131. // Deprecated: Use ActivateCodeLives instead, will be removed in 0.13.
  132. ActiveCodeLiveMinutes int
  133. // Deprecated: Use ResetPasswordCodeLives instead, will be removed in 0.13.
  134. ResetPasswdCodeLiveMinutes int
  135. // Deprecated: Use RequireEmailConfirmation instead, will be removed in 0.13.
  136. RegisterEmailConfirm bool
  137. // Deprecated: Use EnableRegistrationCaptcha instead, will be removed in 0.13.
  138. EnableCaptcha bool
  139. // Deprecated: Use User.EnableEmailNotification instead, will be removed in 0.13.
  140. EnableNotifyMail bool
  141. }
  142. // User settings
  143. User struct {
  144. EnableEmailNotification bool
  145. }
  146. // Session settings
  147. Session struct {
  148. Provider string
  149. ProviderConfig string
  150. CookieName string
  151. CookieSecure bool
  152. GCInterval int64 `ini:"GC_INTERVAL"`
  153. MaxLifeTime int64
  154. CSRFCookieName string `ini:"CSRF_COOKIE_NAME"`
  155. // Deprecated: Use GCInterval instead, will be removed in 0.13.
  156. GCIntervalTime int64 `ini:"GC_INTERVAL_TIME"`
  157. // Deprecated: Use MaxLifeTime instead, will be removed in 0.13.
  158. SessionLifeTime int64
  159. }
  160. // Cache settings
  161. Cache struct {
  162. Adapter string
  163. Interval int
  164. Host string
  165. }
  166. // HTTP settings
  167. HTTP struct {
  168. AccessControlAllowOrigin string
  169. }
  170. // Attachment settings
  171. Attachment struct {
  172. Enabled bool
  173. Path string
  174. AllowedTypes []string `delim:"|"`
  175. MaxSize int64
  176. MaxFiles int
  177. }
  178. // Release settings
  179. Release struct {
  180. Attachment struct {
  181. Enabled bool
  182. AllowedTypes []string `delim:"|"`
  183. MaxSize int64
  184. MaxFiles int
  185. } `ini:"release.attachment"`
  186. }
  187. // Time settings
  188. Time struct {
  189. Format string
  190. // Derived from other static values
  191. FormatLayout string `ini:"-"` // Actual layout of the Format.
  192. }
  193. // Picture settings
  194. Picture struct {
  195. AvatarUploadPath string
  196. RepositoryAvatarUploadPath string
  197. GravatarSource string
  198. DisableGravatar bool
  199. EnableFederatedAvatar bool
  200. // Derived from other static values
  201. LibravatarService *libravatar.Libravatar `ini:"-"` // Initialized client for federated avatar.
  202. }
  203. // Mirror settings
  204. Mirror struct {
  205. DefaultInterval int
  206. }
  207. // Webhook settings
  208. Webhook struct {
  209. Types []string
  210. DeliverTimeout int
  211. SkipTLSVerify bool `ini:"SKIP_TLS_VERIFY"`
  212. PagingNum int
  213. }
  214. // Markdown settings
  215. Markdown struct {
  216. EnableHardLineBreak bool
  217. CustomURLSchemes []string `ini:"CUSTOM_URL_SCHEMES"`
  218. FileExtensions []string
  219. }
  220. // Smartypants settings
  221. Smartypants struct {
  222. Enabled bool
  223. Fractions bool
  224. Dashes bool
  225. LatexDashes bool
  226. AngledQuotes bool
  227. }
  228. // Admin settings
  229. Admin struct {
  230. DisableRegularOrgCreation bool
  231. }
  232. // Cron tasks
  233. Cron struct {
  234. UpdateMirror struct {
  235. Enabled bool
  236. RunAtStart bool
  237. Schedule string
  238. } `ini:"cron.update_mirrors"`
  239. RepoHealthCheck struct {
  240. Enabled bool
  241. RunAtStart bool
  242. Schedule string
  243. Timeout time.Duration
  244. Args []string `delim:" "`
  245. } `ini:"cron.repo_health_check"`
  246. CheckRepoStats struct {
  247. Enabled bool
  248. RunAtStart bool
  249. Schedule string
  250. } `ini:"cron.check_repo_stats"`
  251. RepoArchiveCleanup struct {
  252. Enabled bool
  253. RunAtStart bool
  254. Schedule string
  255. OlderThan time.Duration
  256. } `ini:"cron.repo_archive_cleanup"`
  257. }
  258. // Git settings
  259. Git struct {
  260. // ⚠️ WARNING: Should only be set by "internal/db/repo.go".
  261. Version string `ini:"-"`
  262. DisableDiffHighlight bool
  263. MaxDiffFiles int `ini:"MAX_GIT_DIFF_FILES"`
  264. MaxDiffLines int `ini:"MAX_GIT_DIFF_LINES"`
  265. MaxDiffLineChars int `ini:"MAX_GIT_DIFF_LINE_CHARACTERS"`
  266. GCArgs []string `ini:"GC_ARGS" delim:" "`
  267. Timeout struct {
  268. Migrate int
  269. Mirror int
  270. Clone int
  271. Pull int
  272. GC int `ini:"GC"`
  273. } `ini:"git.timeout"`
  274. }
  275. // API settings
  276. API struct {
  277. MaxResponseItems int
  278. }
  279. // UI settings
  280. UI struct {
  281. ExplorePagingNum int
  282. IssuePagingNum int
  283. FeedMaxCommitNum int
  284. ThemeColorMetaTag string
  285. MaxDisplayFileSize int64
  286. MaxLineHighlight int
  287. Admin struct {
  288. UserPagingNum int
  289. RepoPagingNum int
  290. NoticePagingNum int
  291. OrgPagingNum int
  292. } `ini:"ui.admin"`
  293. User struct {
  294. RepoPagingNum int
  295. NewsFeedPagingNum int
  296. CommitsPagingNum int
  297. } `ini:"ui.user"`
  298. }
  299. // Prometheus settings
  300. Prometheus struct {
  301. Enabled bool
  302. EnableBasicAuth bool
  303. BasicAuthUsername string
  304. BasicAuthPassword string
  305. }
  306. // Other settings
  307. Other struct {
  308. ShowFooterBranding bool
  309. ShowFooterTemplateLoadTime bool
  310. }
  311. // Global setting
  312. HasRobotsTxt bool
  313. Search struct {
  314. IndexURL string `ini:"INDEX_URL"`
  315. SearchURL string `ini:"SEARCH_URL"`
  316. Key string `ini:"SEARCH_KEY"`
  317. }
  318. DOI struct {
  319. URL string `ini:"DOI_URL"`
  320. Key string `ini:"DOI_KEY"`
  321. Base string `ini:"DOI_BASE"`
  322. }
  323. CLIConfig struct {
  324. RSAHostKey string
  325. }
  326. )
  327. type ServerOpts struct {
  328. ExternalURL string `ini:"EXTERNAL_URL"`
  329. Domain string
  330. Protocol string
  331. HTTPAddr string `ini:"HTTP_ADDR"`
  332. HTTPPort string `ini:"HTTP_PORT"`
  333. CertFile string
  334. KeyFile string
  335. TLSMinVersion string `ini:"TLS_MIN_VERSION"`
  336. UnixSocketPermission string
  337. LocalRootURL string `ini:"LOCAL_ROOT_URL"`
  338. OfflineMode bool
  339. DisableRouterLog bool
  340. EnableGzip bool
  341. AppDataPath string
  342. LoadAssetsFromDisk bool
  343. LandingURL string `ini:"LANDING_URL"`
  344. // Derived from other static values
  345. URL *url.URL `ini:"-"` // Parsed URL object of ExternalURL.
  346. Subpath string `ini:"-"` // Subpath found the ExternalURL. Should be empty when not found.
  347. SubpathDepth int `ini:"-"` // The number of slashes found in the Subpath.
  348. UnixSocketMode os.FileMode `ini:"-"` // Parsed file mode of UnixSocketPermission.
  349. // Deprecated: Use ExternalURL instead, will be removed in 0.13.
  350. RootURL string `ini:"ROOT_URL"`
  351. // Deprecated: Use LandingURL instead, will be removed in 0.13.
  352. LangdingPage string `ini:"LANDING_PAGE"`
  353. }
  354. // Server settings
  355. var Server ServerOpts
  356. type DatabaseOpts struct {
  357. Type string
  358. Host string
  359. Name string
  360. User string
  361. Password string
  362. SSLMode string `ini:"SSL_MODE"`
  363. Path string
  364. MaxOpenConns int
  365. MaxIdleConns int
  366. // Deprecated: Use Type instead, will be removed in 0.13.
  367. DbType string
  368. // Deprecated: Use Password instead, will be removed in 0.13.
  369. Passwd string
  370. }
  371. // Database settings
  372. var Database DatabaseOpts
  373. type LFSOpts struct {
  374. Storage string
  375. ObjectsPath string
  376. }
  377. // LFS settings
  378. var LFS LFSOpts
  379. type i18nConf struct {
  380. Langs []string `delim:","`
  381. Names []string `delim:","`
  382. dateLangs map[string]string `ini:"-"`
  383. }
  384. // DateLang transforms standard language locale name to corresponding value in datetime plugin.
  385. func (c *i18nConf) DateLang(lang string) string {
  386. name, ok := c.dateLangs[lang]
  387. if ok {
  388. return name
  389. }
  390. return "en"
  391. }
  392. // I18n settings
  393. var I18n *i18nConf
  394. // handleDeprecated transfers deprecated values to the new ones when set.
  395. func handleDeprecated() {
  396. if App.AppName != "" {
  397. App.BrandName = App.AppName
  398. App.AppName = ""
  399. }
  400. if Server.RootURL != "" {
  401. Server.ExternalURL = Server.RootURL
  402. Server.RootURL = ""
  403. }
  404. if Server.LangdingPage == "explore" {
  405. Server.LandingURL = "/explore"
  406. Server.LangdingPage = ""
  407. }
  408. if Database.DbType != "" {
  409. Database.Type = Database.DbType
  410. Database.DbType = ""
  411. }
  412. if Database.Passwd != "" {
  413. Database.Password = Database.Passwd
  414. Database.Passwd = ""
  415. }
  416. if Email.Passwd != "" {
  417. Email.Password = Email.Passwd
  418. Email.Passwd = ""
  419. }
  420. if Auth.ActiveCodeLiveMinutes > 0 {
  421. Auth.ActivateCodeLives = Auth.ActiveCodeLiveMinutes
  422. Auth.ActiveCodeLiveMinutes = 0
  423. }
  424. if Auth.ResetPasswdCodeLiveMinutes > 0 {
  425. Auth.ResetPasswordCodeLives = Auth.ResetPasswdCodeLiveMinutes
  426. Auth.ResetPasswdCodeLiveMinutes = 0
  427. }
  428. if Auth.RegisterEmailConfirm {
  429. Auth.RequireEmailConfirmation = true
  430. Auth.RegisterEmailConfirm = false
  431. }
  432. if Auth.EnableCaptcha {
  433. Auth.EnableRegistrationCaptcha = true
  434. Auth.EnableCaptcha = false
  435. }
  436. if Security.ReverseProxyAuthenticationUser != "" {
  437. Auth.ReverseProxyAuthenticationHeader = Security.ReverseProxyAuthenticationUser
  438. Security.ReverseProxyAuthenticationUser = ""
  439. }
  440. if Auth.EnableNotifyMail {
  441. User.EnableEmailNotification = true
  442. Auth.EnableNotifyMail = false
  443. }
  444. if Session.GCIntervalTime > 0 {
  445. Session.GCInterval = Session.GCIntervalTime
  446. Session.GCIntervalTime = 0
  447. }
  448. if Session.SessionLifeTime > 0 {
  449. Session.MaxLifeTime = Session.SessionLifeTime
  450. Session.SessionLifeTime = 0
  451. }
  452. }
  453. // HookMode indicates whether program starts as Git server-side hook callback.
  454. // All operations should be done synchronously to prevent program exits before finishing.
  455. //
  456. // ⚠️ WARNING: Should only be set by "internal/cmd/serv.go".
  457. var HookMode bool
  458. // Indicates which database backend is currently being used.
  459. var (
  460. UseSQLite3 bool
  461. UseMySQL bool
  462. UsePostgreSQL bool
  463. UseMSSQL bool
  464. )