static.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. // LFS settings
  171. LFS struct {
  172. ObjectsPath string
  173. }
  174. // Attachment settings
  175. Attachment struct {
  176. Enabled bool
  177. Path string
  178. AllowedTypes []string `delim:"|"`
  179. MaxSize int64
  180. MaxFiles int
  181. }
  182. // Release settings
  183. Release struct {
  184. Attachment struct {
  185. Enabled bool
  186. AllowedTypes []string `delim:"|"`
  187. MaxSize int64
  188. MaxFiles int
  189. } `ini:"release.attachment"`
  190. }
  191. // Time settings
  192. Time struct {
  193. Format string
  194. // Derived from other static values
  195. FormatLayout string `ini:"-"` // Actual layout of the Format.
  196. }
  197. // Picture settings
  198. Picture struct {
  199. AvatarUploadPath string
  200. RepositoryAvatarUploadPath string
  201. GravatarSource string
  202. DisableGravatar bool
  203. EnableFederatedAvatar bool
  204. // Derived from other static values
  205. LibravatarService *libravatar.Libravatar `ini:"-"` // Initialized client for federated avatar.
  206. }
  207. // Mirror settings
  208. Mirror struct {
  209. DefaultInterval int
  210. }
  211. // Webhook settings
  212. Webhook struct {
  213. Types []string
  214. DeliverTimeout int
  215. SkipTLSVerify bool `ini:"SKIP_TLS_VERIFY"`
  216. PagingNum int
  217. }
  218. // Markdown settings
  219. Markdown struct {
  220. EnableHardLineBreak bool
  221. CustomURLSchemes []string `ini:"CUSTOM_URL_SCHEMES"`
  222. FileExtensions []string
  223. }
  224. // Smartypants settings
  225. Smartypants struct {
  226. Enabled bool
  227. Fractions bool
  228. Dashes bool
  229. LatexDashes bool
  230. AngledQuotes bool
  231. }
  232. // Admin settings
  233. Admin struct {
  234. DisableRegularOrgCreation bool
  235. }
  236. // Cron tasks
  237. Cron struct {
  238. UpdateMirror struct {
  239. Enabled bool
  240. RunAtStart bool
  241. Schedule string
  242. } `ini:"cron.update_mirrors"`
  243. RepoHealthCheck struct {
  244. Enabled bool
  245. RunAtStart bool
  246. Schedule string
  247. Timeout time.Duration
  248. Args []string `delim:" "`
  249. } `ini:"cron.repo_health_check"`
  250. CheckRepoStats struct {
  251. Enabled bool
  252. RunAtStart bool
  253. Schedule string
  254. } `ini:"cron.check_repo_stats"`
  255. RepoArchiveCleanup struct {
  256. Enabled bool
  257. RunAtStart bool
  258. Schedule string
  259. OlderThan time.Duration
  260. } `ini:"cron.repo_archive_cleanup"`
  261. }
  262. // Git settings
  263. Git struct {
  264. // ⚠️ WARNING: Should only be set by "internal/db/repo.go".
  265. Version string `ini:"-"`
  266. DisableDiffHighlight bool
  267. MaxDiffFiles int `ini:"MAX_GIT_DIFF_FILES"`
  268. MaxDiffLines int `ini:"MAX_GIT_DIFF_LINES"`
  269. MaxDiffLineChars int `ini:"MAX_GIT_DIFF_LINE_CHARACTERS"`
  270. GCArgs []string `ini:"GC_ARGS" delim:" "`
  271. Timeout struct {
  272. Migrate int
  273. Mirror int
  274. Clone int
  275. Pull int
  276. GC int `ini:"GC"`
  277. } `ini:"git.timeout"`
  278. }
  279. // API settings
  280. API struct {
  281. MaxResponseItems int
  282. }
  283. // UI settings
  284. UI struct {
  285. ExplorePagingNum int
  286. IssuePagingNum int
  287. FeedMaxCommitNum int
  288. ThemeColorMetaTag string
  289. MaxDisplayFileSize int64
  290. MaxLineHighlight int
  291. Admin struct {
  292. UserPagingNum int
  293. RepoPagingNum int
  294. NoticePagingNum int
  295. OrgPagingNum int
  296. } `ini:"ui.admin"`
  297. User struct {
  298. RepoPagingNum int
  299. NewsFeedPagingNum int
  300. CommitsPagingNum int
  301. } `ini:"ui.user"`
  302. }
  303. // Prometheus settings
  304. Prometheus struct {
  305. Enabled bool
  306. EnableBasicAuth bool
  307. BasicAuthUsername string
  308. BasicAuthPassword string
  309. }
  310. // Other settings
  311. Other struct {
  312. ShowFooterBranding bool
  313. ShowFooterTemplateLoadTime bool
  314. }
  315. // Global setting
  316. HasRobotsTxt bool
  317. Search struct {
  318. IndexURL string `ini:"INDEX_URL"`
  319. SearchURL string `ini:"SEARCH_URL"`
  320. Key string `ini:"SEARCH_KEY"`
  321. }
  322. DOI struct {
  323. URL string `ini:"DOI_URL"`
  324. Key string `ini:"DOI_KEY"`
  325. Base string `ini:"DOI_BASE"`
  326. }
  327. CLIConfig struct {
  328. RSAHostKey string
  329. }
  330. WebDav struct {
  331. On bool
  332. Logged bool
  333. AuthRealm string
  334. }
  335. )
  336. type ServerOpts struct {
  337. ExternalURL string `ini:"EXTERNAL_URL"`
  338. Domain string
  339. Protocol string
  340. HTTPAddr string `ini:"HTTP_ADDR"`
  341. HTTPPort string `ini:"HTTP_PORT"`
  342. CertFile string
  343. KeyFile string
  344. TLSMinVersion string `ini:"TLS_MIN_VERSION"`
  345. UnixSocketPermission string
  346. LocalRootURL string `ini:"LOCAL_ROOT_URL"`
  347. OfflineMode bool
  348. DisableRouterLog bool
  349. EnableGzip bool
  350. AppDataPath string
  351. LoadAssetsFromDisk bool
  352. LandingURL string `ini:"LANDING_URL"`
  353. // Derived from other static values
  354. URL *url.URL `ini:"-"` // Parsed URL object of ExternalURL.
  355. Subpath string `ini:"-"` // Subpath found the ExternalURL. Should be empty when not found.
  356. SubpathDepth int `ini:"-"` // The number of slashes found in the Subpath.
  357. UnixSocketMode os.FileMode `ini:"-"` // Parsed file mode of UnixSocketPermission.
  358. // Deprecated: Use ExternalURL instead, will be removed in 0.13.
  359. RootURL string `ini:"ROOT_URL"`
  360. // Deprecated: Use LandingURL instead, will be removed in 0.13.
  361. LangdingPage string `ini:"LANDING_PAGE"`
  362. }
  363. // Server settings
  364. var Server ServerOpts
  365. type DatabaseOpts struct {
  366. Type string
  367. Host string
  368. Name string
  369. User string
  370. Password string
  371. SSLMode string `ini:"SSL_MODE"`
  372. Path string
  373. MaxOpenConns int
  374. MaxIdleConns int
  375. // Deprecated: Use Type instead, will be removed in 0.13.
  376. DbType string
  377. // Deprecated: Use Password instead, will be removed in 0.13.
  378. Passwd string
  379. }
  380. // Database settings
  381. var Database DatabaseOpts
  382. type i18nConf struct {
  383. Langs []string `delim:","`
  384. Names []string `delim:","`
  385. dateLangs map[string]string `ini:"-"`
  386. }
  387. // DateLang transforms standard language locale name to corresponding value in datetime plugin.
  388. func (c *i18nConf) DateLang(lang string) string {
  389. name, ok := c.dateLangs[lang]
  390. if ok {
  391. return name
  392. }
  393. return "en"
  394. }
  395. // I18n settings
  396. var I18n *i18nConf
  397. // handleDeprecated transfers deprecated values to the new ones when set.
  398. func handleDeprecated() {
  399. if App.AppName != "" {
  400. App.BrandName = App.AppName
  401. App.AppName = ""
  402. }
  403. if Server.RootURL != "" {
  404. Server.ExternalURL = Server.RootURL
  405. Server.RootURL = ""
  406. }
  407. if Server.LangdingPage == "explore" {
  408. Server.LandingURL = "/explore"
  409. Server.LangdingPage = ""
  410. }
  411. if Database.DbType != "" {
  412. Database.Type = Database.DbType
  413. Database.DbType = ""
  414. }
  415. if Database.Passwd != "" {
  416. Database.Password = Database.Passwd
  417. Database.Passwd = ""
  418. }
  419. if Email.Passwd != "" {
  420. Email.Password = Email.Passwd
  421. Email.Passwd = ""
  422. }
  423. if Auth.ActiveCodeLiveMinutes > 0 {
  424. Auth.ActivateCodeLives = Auth.ActiveCodeLiveMinutes
  425. Auth.ActiveCodeLiveMinutes = 0
  426. }
  427. if Auth.ResetPasswdCodeLiveMinutes > 0 {
  428. Auth.ResetPasswordCodeLives = Auth.ResetPasswdCodeLiveMinutes
  429. Auth.ResetPasswdCodeLiveMinutes = 0
  430. }
  431. if Auth.RegisterEmailConfirm {
  432. Auth.RequireEmailConfirmation = true
  433. Auth.RegisterEmailConfirm = false
  434. }
  435. if Auth.EnableCaptcha {
  436. Auth.EnableRegistrationCaptcha = true
  437. Auth.EnableCaptcha = false
  438. }
  439. if Security.ReverseProxyAuthenticationUser != "" {
  440. Auth.ReverseProxyAuthenticationHeader = Security.ReverseProxyAuthenticationUser
  441. Security.ReverseProxyAuthenticationUser = ""
  442. }
  443. if Auth.EnableNotifyMail {
  444. User.EnableEmailNotification = true
  445. Auth.EnableNotifyMail = false
  446. }
  447. if Session.GCIntervalTime > 0 {
  448. Session.GCInterval = Session.GCIntervalTime
  449. Session.GCIntervalTime = 0
  450. }
  451. if Session.SessionLifeTime > 0 {
  452. Session.MaxLifeTime = Session.SessionLifeTime
  453. Session.SessionLifeTime = 0
  454. }
  455. }
  456. // HookMode indicates whether program starts as Git server-side hook callback.
  457. // All operations should be done synchronously to prevent program exits before finishing.
  458. //
  459. // ⚠️ WARNING: Should only be set by "internal/cmd/serv.go".
  460. var HookMode bool
  461. // Indicates which database backend is currently being used.
  462. var (
  463. UseSQLite3 bool
  464. UseMySQL bool
  465. UsePostgreSQL bool
  466. UseMSSQL bool
  467. )