2
0

static.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. // Repository editor settings
  67. Editor struct {
  68. LineWrapExtensions []string
  69. PreviewableFileModes []string
  70. } `ini:"repository.editor"`
  71. // Repository upload settings
  72. Upload struct {
  73. Enabled bool
  74. TempPath string
  75. AllowedTypes []string `delim:"|"`
  76. FileMaxSize int64
  77. MaxFiles int
  78. } `ini:"repository.upload"`
  79. }
  80. // Security settings
  81. Security struct {
  82. InstallLock bool
  83. SecretKey string
  84. LoginRememberDays int
  85. CookieRememberName string
  86. CookieUsername string
  87. CookieSecure bool
  88. EnableLoginStatusCookie bool
  89. LoginStatusCookieName string
  90. LocalNetworkAllowlist []string `delim:","`
  91. // Deprecated: Use Auth.ReverseProxyAuthenticationHeader instead, will be removed in 0.13.
  92. ReverseProxyAuthenticationUser string
  93. }
  94. // Email settings
  95. Email struct {
  96. Enabled bool
  97. SubjectPrefix string
  98. Host string
  99. From string
  100. User string
  101. Password string
  102. DisableHELO bool `ini:"DISABLE_HELO"`
  103. HELOHostname string `ini:"HELO_HOSTNAME"`
  104. SkipVerify bool
  105. UseCertificate bool
  106. CertFile string
  107. KeyFile string
  108. UsePlainText bool
  109. AddPlainTextAlt bool
  110. // Derived from other static values
  111. FromEmail string `ini:"-"` // Parsed email address of From without person's name.
  112. // Deprecated: Use Password instead, will be removed in 0.13.
  113. Passwd string
  114. }
  115. // Authentication settings
  116. Auth struct {
  117. ActivateCodeLives int
  118. ResetPasswordCodeLives int
  119. RequireEmailConfirmation bool
  120. RequireSigninView bool
  121. DisableRegistration bool
  122. EnableRegistrationCaptcha bool
  123. EnableReverseProxyAuthentication bool
  124. EnableReverseProxyAutoRegistration bool
  125. ReverseProxyAuthenticationHeader string
  126. // Deprecated: Use ActivateCodeLives instead, will be removed in 0.13.
  127. ActiveCodeLiveMinutes int
  128. // Deprecated: Use ResetPasswordCodeLives instead, will be removed in 0.13.
  129. ResetPasswdCodeLiveMinutes int
  130. // Deprecated: Use RequireEmailConfirmation instead, will be removed in 0.13.
  131. RegisterEmailConfirm bool
  132. // Deprecated: Use EnableRegistrationCaptcha instead, will be removed in 0.13.
  133. EnableCaptcha bool
  134. // Deprecated: Use User.EnableEmailNotification instead, will be removed in 0.13.
  135. EnableNotifyMail bool
  136. }
  137. // User settings
  138. User struct {
  139. EnableEmailNotification bool
  140. }
  141. // Session settings
  142. Session struct {
  143. Provider string
  144. ProviderConfig string
  145. CookieName string
  146. CookieSecure bool
  147. GCInterval int64 `ini:"GC_INTERVAL"`
  148. MaxLifeTime int64
  149. CSRFCookieName string `ini:"CSRF_COOKIE_NAME"`
  150. // Deprecated: Use GCInterval instead, will be removed in 0.13.
  151. GCIntervalTime int64 `ini:"GC_INTERVAL_TIME"`
  152. // Deprecated: Use MaxLifeTime instead, will be removed in 0.13.
  153. SessionLifeTime int64
  154. }
  155. // Cache settings
  156. Cache struct {
  157. Adapter string
  158. Interval int
  159. Host string
  160. }
  161. // HTTP settings
  162. HTTP struct {
  163. AccessControlAllowOrigin string
  164. }
  165. // Attachment settings
  166. Attachment struct {
  167. Enabled bool
  168. Path string
  169. AllowedTypes []string `delim:"|"`
  170. MaxSize int64
  171. MaxFiles int
  172. }
  173. // Release settings
  174. Release struct {
  175. Attachment struct {
  176. Enabled bool
  177. AllowedTypes []string `delim:"|"`
  178. MaxSize int64
  179. MaxFiles int
  180. } `ini:"release.attachment"`
  181. }
  182. // Time settings
  183. Time struct {
  184. Format string
  185. // Derived from other static values
  186. FormatLayout string `ini:"-"` // Actual layout of the Format.
  187. }
  188. // Picture settings
  189. Picture struct {
  190. AvatarUploadPath string
  191. RepositoryAvatarUploadPath string
  192. GravatarSource string
  193. DisableGravatar bool
  194. EnableFederatedAvatar bool
  195. // Derived from other static values
  196. LibravatarService *libravatar.Libravatar `ini:"-"` // Initialized client for federated avatar.
  197. }
  198. // Mirror settings
  199. Mirror struct {
  200. DefaultInterval int
  201. }
  202. // Webhook settings
  203. Webhook struct {
  204. Types []string
  205. DeliverTimeout int
  206. SkipTLSVerify bool `ini:"SKIP_TLS_VERIFY"`
  207. PagingNum int
  208. }
  209. // Markdown settings
  210. Markdown struct {
  211. EnableHardLineBreak bool
  212. CustomURLSchemes []string `ini:"CUSTOM_URL_SCHEMES"`
  213. FileExtensions []string
  214. }
  215. // Smartypants settings
  216. Smartypants struct {
  217. Enabled bool
  218. Fractions bool
  219. Dashes bool
  220. LatexDashes bool
  221. AngledQuotes bool
  222. }
  223. // Admin settings
  224. Admin struct {
  225. DisableRegularOrgCreation bool
  226. }
  227. // Cron tasks
  228. Cron struct {
  229. UpdateMirror struct {
  230. Enabled bool
  231. RunAtStart bool
  232. Schedule string
  233. } `ini:"cron.update_mirrors"`
  234. RepoHealthCheck struct {
  235. Enabled bool
  236. RunAtStart bool
  237. Schedule string
  238. Timeout time.Duration
  239. Args []string `delim:" "`
  240. } `ini:"cron.repo_health_check"`
  241. CheckRepoStats struct {
  242. Enabled bool
  243. RunAtStart bool
  244. Schedule string
  245. } `ini:"cron.check_repo_stats"`
  246. RepoArchiveCleanup struct {
  247. Enabled bool
  248. RunAtStart bool
  249. Schedule string
  250. OlderThan time.Duration
  251. } `ini:"cron.repo_archive_cleanup"`
  252. }
  253. // Git settings
  254. Git struct {
  255. // ⚠️ WARNING: Should only be set by "internal/db/repo.go".
  256. Version string `ini:"-"`
  257. DisableDiffHighlight bool
  258. MaxDiffFiles int `ini:"MAX_GIT_DIFF_FILES"`
  259. MaxDiffLines int `ini:"MAX_GIT_DIFF_LINES"`
  260. MaxDiffLineChars int `ini:"MAX_GIT_DIFF_LINE_CHARACTERS"`
  261. GCArgs []string `ini:"GC_ARGS" delim:" "`
  262. Timeout struct {
  263. Migrate int
  264. Mirror int
  265. Clone int
  266. Pull int
  267. GC int `ini:"GC"`
  268. } `ini:"git.timeout"`
  269. }
  270. // API settings
  271. API struct {
  272. MaxResponseItems int
  273. }
  274. // UI settings
  275. UI struct {
  276. ExplorePagingNum int
  277. IssuePagingNum int
  278. FeedMaxCommitNum int
  279. ThemeColorMetaTag string
  280. MaxDisplayFileSize int64
  281. Admin struct {
  282. UserPagingNum int
  283. RepoPagingNum int
  284. NoticePagingNum int
  285. OrgPagingNum int
  286. } `ini:"ui.admin"`
  287. User struct {
  288. RepoPagingNum int
  289. NewsFeedPagingNum int
  290. CommitsPagingNum int
  291. } `ini:"ui.user"`
  292. }
  293. // Prometheus settings
  294. Prometheus struct {
  295. Enabled bool
  296. EnableBasicAuth bool
  297. BasicAuthUsername string
  298. BasicAuthPassword string
  299. }
  300. // Other settings
  301. Other struct {
  302. ShowFooterBranding bool
  303. ShowFooterTemplateLoadTime bool
  304. }
  305. // Global setting
  306. HasRobotsTxt bool
  307. )
  308. type ServerOpts struct {
  309. ExternalURL string `ini:"EXTERNAL_URL"`
  310. Domain string
  311. Protocol string
  312. HTTPAddr string `ini:"HTTP_ADDR"`
  313. HTTPPort string `ini:"HTTP_PORT"`
  314. CertFile string
  315. KeyFile string
  316. TLSMinVersion string `ini:"TLS_MIN_VERSION"`
  317. UnixSocketPermission string
  318. LocalRootURL string `ini:"LOCAL_ROOT_URL"`
  319. OfflineMode bool
  320. DisableRouterLog bool
  321. EnableGzip bool
  322. AppDataPath string
  323. LoadAssetsFromDisk bool
  324. LandingURL string `ini:"LANDING_URL"`
  325. // Derived from other static values
  326. URL *url.URL `ini:"-"` // Parsed URL object of ExternalURL.
  327. Subpath string `ini:"-"` // Subpath found the ExternalURL. Should be empty when not found.
  328. SubpathDepth int `ini:"-"` // The number of slashes found in the Subpath.
  329. UnixSocketMode os.FileMode `ini:"-"` // Parsed file mode of UnixSocketPermission.
  330. // Deprecated: Use ExternalURL instead, will be removed in 0.13.
  331. RootURL string `ini:"ROOT_URL"`
  332. // Deprecated: Use LandingURL instead, will be removed in 0.13.
  333. LangdingPage string `ini:"LANDING_PAGE"`
  334. }
  335. // Server settings
  336. var Server ServerOpts
  337. type DatabaseOpts struct {
  338. Type string
  339. Host string
  340. Name string
  341. User string
  342. Password string
  343. SSLMode string `ini:"SSL_MODE"`
  344. Path string
  345. MaxOpenConns int
  346. MaxIdleConns int
  347. // Deprecated: Use Type instead, will be removed in 0.13.
  348. DbType string
  349. // Deprecated: Use Password instead, will be removed in 0.13.
  350. Passwd string
  351. }
  352. // Database settings
  353. var Database DatabaseOpts
  354. type LFSOpts struct {
  355. Storage string
  356. ObjectsPath string
  357. }
  358. // LFS settings
  359. var LFS LFSOpts
  360. type i18nConf struct {
  361. Langs []string `delim:","`
  362. Names []string `delim:","`
  363. dateLangs map[string]string `ini:"-"`
  364. }
  365. // DateLang transforms standard language locale name to corresponding value in datetime plugin.
  366. func (c *i18nConf) DateLang(lang string) string {
  367. name, ok := c.dateLangs[lang]
  368. if ok {
  369. return name
  370. }
  371. return "en"
  372. }
  373. // I18n settings
  374. var I18n *i18nConf
  375. // handleDeprecated transfers deprecated values to the new ones when set.
  376. func handleDeprecated() {
  377. if App.AppName != "" {
  378. App.BrandName = App.AppName
  379. App.AppName = ""
  380. }
  381. if Server.RootURL != "" {
  382. Server.ExternalURL = Server.RootURL
  383. Server.RootURL = ""
  384. }
  385. if Server.LangdingPage == "explore" {
  386. Server.LandingURL = "/explore"
  387. Server.LangdingPage = ""
  388. }
  389. if Database.DbType != "" {
  390. Database.Type = Database.DbType
  391. Database.DbType = ""
  392. }
  393. if Database.Passwd != "" {
  394. Database.Password = Database.Passwd
  395. Database.Passwd = ""
  396. }
  397. if Email.Passwd != "" {
  398. Email.Password = Email.Passwd
  399. Email.Passwd = ""
  400. }
  401. if Auth.ActiveCodeLiveMinutes > 0 {
  402. Auth.ActivateCodeLives = Auth.ActiveCodeLiveMinutes
  403. Auth.ActiveCodeLiveMinutes = 0
  404. }
  405. if Auth.ResetPasswdCodeLiveMinutes > 0 {
  406. Auth.ResetPasswordCodeLives = Auth.ResetPasswdCodeLiveMinutes
  407. Auth.ResetPasswdCodeLiveMinutes = 0
  408. }
  409. if Auth.RegisterEmailConfirm {
  410. Auth.RequireEmailConfirmation = true
  411. Auth.RegisterEmailConfirm = false
  412. }
  413. if Auth.EnableCaptcha {
  414. Auth.EnableRegistrationCaptcha = true
  415. Auth.EnableCaptcha = false
  416. }
  417. if Security.ReverseProxyAuthenticationUser != "" {
  418. Auth.ReverseProxyAuthenticationHeader = Security.ReverseProxyAuthenticationUser
  419. Security.ReverseProxyAuthenticationUser = ""
  420. }
  421. if Auth.EnableNotifyMail {
  422. User.EnableEmailNotification = true
  423. Auth.EnableNotifyMail = false
  424. }
  425. if Session.GCIntervalTime > 0 {
  426. Session.GCInterval = Session.GCIntervalTime
  427. Session.GCIntervalTime = 0
  428. }
  429. if Session.SessionLifeTime > 0 {
  430. Session.MaxLifeTime = Session.SessionLifeTime
  431. Session.SessionLifeTime = 0
  432. }
  433. }
  434. // HookMode indicates whether program starts as Git server-side hook callback.
  435. // All operations should be done synchronously to prevent program exits before finishing.
  436. //
  437. // ⚠️ WARNING: Should only be set by "internal/cmd/serv.go".
  438. var HookMode bool
  439. // Indicates which database backend is currently being used.
  440. var (
  441. UseSQLite3 bool
  442. UseMySQL bool
  443. UsePostgreSQL bool
  444. UseMSSQL bool
  445. )