options.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // Copyright 2019 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package config // import "miniflux.app/config"
  5. import (
  6. "fmt"
  7. "strings"
  8. )
  9. const (
  10. defaultHTTPS = false
  11. defaultLogDateTime = false
  12. defaultHSTS = true
  13. defaultHTTPService = true
  14. defaultSchedulerService = true
  15. defaultDebug = false
  16. defaultBaseURL = "http://localhost"
  17. defaultRootURL = "http://localhost"
  18. defaultBasePath = ""
  19. defaultWorkerPoolSize = 5
  20. defaultPollingFrequency = 60
  21. defaultBatchSize = 10
  22. defaultPollingScheduler = "round_robin"
  23. defaultSchedulerEntryFrequencyMinInterval = 5
  24. defaultSchedulerEntryFrequencyMaxInterval = 24 * 60
  25. defaultRunMigrations = false
  26. defaultDatabaseURL = "user=postgres password=postgres dbname=miniflux2 sslmode=disable"
  27. defaultDatabaseMaxConns = 20
  28. defaultDatabaseMinConns = 1
  29. defaultListenAddr = "127.0.0.1:8080"
  30. defaultCertFile = ""
  31. defaultKeyFile = ""
  32. defaultCertDomain = ""
  33. defaultCertCache = "/tmp/cert_cache"
  34. defaultCleanupFrequencyHours = 24
  35. defaultCleanupArchiveReadDays = 60
  36. defaultCleanupRemoveSessionsDays = 30
  37. defaultProxyImages = "http-only"
  38. defaultCreateAdmin = false
  39. defaultOAuth2UserCreation = false
  40. defaultOAuth2ClientID = ""
  41. defaultOAuth2ClientSecret = ""
  42. defaultOAuth2RedirectURL = ""
  43. defaultOAuth2OidcDiscoveryEndpoint = ""
  44. defaultOAuth2Provider = ""
  45. defaultPocketConsumerKey = ""
  46. defaultHTTPClientTimeout = 20
  47. defaultHTTPClientMaxBodySize = 15
  48. defaultAuthProxyHeader = ""
  49. defaultAuthProxyUserCreation = false
  50. )
  51. // Options contains configuration options.
  52. type Options struct {
  53. HTTPS bool
  54. logDateTime bool
  55. hsts bool
  56. httpService bool
  57. schedulerService bool
  58. debug bool
  59. baseURL string
  60. rootURL string
  61. basePath string
  62. databaseURL string
  63. databaseMaxConns int
  64. databaseMinConns int
  65. runMigrations bool
  66. listenAddr string
  67. certFile string
  68. certDomain string
  69. certCache string
  70. certKeyFile string
  71. cleanupFrequencyHours int
  72. cleanupArchiveReadDays int
  73. cleanupRemoveSessionsDays int
  74. pollingFrequency int
  75. batchSize int
  76. pollingScheduler string
  77. schedulerEntryFrequencyMinInterval int
  78. schedulerEntryFrequencyMaxInterval int
  79. workerPoolSize int
  80. createAdmin bool
  81. proxyImages string
  82. oauth2UserCreationAllowed bool
  83. oauth2ClientID string
  84. oauth2ClientSecret string
  85. oauth2RedirectURL string
  86. oauth2OidcDiscoveryEndpoint string
  87. oauth2Provider string
  88. pocketConsumerKey string
  89. httpClientTimeout int
  90. httpClientMaxBodySize int64
  91. authProxyHeader string
  92. authProxyUserCreation bool
  93. }
  94. // NewOptions returns Options with default values.
  95. func NewOptions() *Options {
  96. return &Options{
  97. HTTPS: defaultHTTPS,
  98. logDateTime: defaultLogDateTime,
  99. hsts: defaultHSTS,
  100. httpService: defaultHTTPService,
  101. schedulerService: defaultSchedulerService,
  102. debug: defaultDebug,
  103. baseURL: defaultBaseURL,
  104. rootURL: defaultRootURL,
  105. basePath: defaultBasePath,
  106. databaseURL: defaultDatabaseURL,
  107. databaseMaxConns: defaultDatabaseMaxConns,
  108. databaseMinConns: defaultDatabaseMinConns,
  109. runMigrations: defaultRunMigrations,
  110. listenAddr: defaultListenAddr,
  111. certFile: defaultCertFile,
  112. certDomain: defaultCertDomain,
  113. certCache: defaultCertCache,
  114. certKeyFile: defaultKeyFile,
  115. cleanupFrequencyHours: defaultCleanupFrequencyHours,
  116. cleanupArchiveReadDays: defaultCleanupArchiveReadDays,
  117. cleanupRemoveSessionsDays: defaultCleanupRemoveSessionsDays,
  118. pollingFrequency: defaultPollingFrequency,
  119. batchSize: defaultBatchSize,
  120. pollingScheduler: defaultPollingScheduler,
  121. schedulerEntryFrequencyMinInterval: defaultSchedulerEntryFrequencyMinInterval,
  122. schedulerEntryFrequencyMaxInterval: defaultSchedulerEntryFrequencyMaxInterval,
  123. workerPoolSize: defaultWorkerPoolSize,
  124. createAdmin: defaultCreateAdmin,
  125. proxyImages: defaultProxyImages,
  126. oauth2UserCreationAllowed: defaultOAuth2UserCreation,
  127. oauth2ClientID: defaultOAuth2ClientID,
  128. oauth2ClientSecret: defaultOAuth2ClientSecret,
  129. oauth2RedirectURL: defaultOAuth2RedirectURL,
  130. oauth2OidcDiscoveryEndpoint: defaultOAuth2OidcDiscoveryEndpoint,
  131. oauth2Provider: defaultOAuth2Provider,
  132. pocketConsumerKey: defaultPocketConsumerKey,
  133. httpClientTimeout: defaultHTTPClientTimeout,
  134. httpClientMaxBodySize: defaultHTTPClientMaxBodySize * 1024 * 1024,
  135. authProxyHeader: defaultAuthProxyHeader,
  136. authProxyUserCreation: defaultAuthProxyUserCreation,
  137. }
  138. }
  139. // LogDateTime returns true if the date/time should be displayed in log messages.
  140. func (o *Options) LogDateTime() bool {
  141. return o.logDateTime
  142. }
  143. // HasDebugMode returns true if debug mode is enabled.
  144. func (o *Options) HasDebugMode() bool {
  145. return o.debug
  146. }
  147. // BaseURL returns the application base URL with path.
  148. func (o *Options) BaseURL() string {
  149. return o.baseURL
  150. }
  151. // RootURL returns the base URL without path.
  152. func (o *Options) RootURL() string {
  153. return o.rootURL
  154. }
  155. // BasePath returns the application base path according to the base URL.
  156. func (o *Options) BasePath() string {
  157. return o.basePath
  158. }
  159. // IsDefaultDatabaseURL returns true if the default database URL is used.
  160. func (o *Options) IsDefaultDatabaseURL() bool {
  161. return o.databaseURL == defaultDatabaseURL
  162. }
  163. // DatabaseURL returns the database URL.
  164. func (o *Options) DatabaseURL() string {
  165. return o.databaseURL
  166. }
  167. // DatabaseMaxConns returns the maximum number of database connections.
  168. func (o *Options) DatabaseMaxConns() int {
  169. return o.databaseMaxConns
  170. }
  171. // DatabaseMinConns returns the minimum number of database connections.
  172. func (o *Options) DatabaseMinConns() int {
  173. return o.databaseMinConns
  174. }
  175. // ListenAddr returns the listen address for the HTTP server.
  176. func (o *Options) ListenAddr() string {
  177. return o.listenAddr
  178. }
  179. // CertFile returns the SSL certificate filename if any.
  180. func (o *Options) CertFile() string {
  181. return o.certFile
  182. }
  183. // CertKeyFile returns the private key filename for custom SSL certificate.
  184. func (o *Options) CertKeyFile() string {
  185. return o.certKeyFile
  186. }
  187. // CertDomain returns the domain to use for Let's Encrypt certificate.
  188. func (o *Options) CertDomain() string {
  189. return o.certDomain
  190. }
  191. // CertCache returns the directory to use for Let's Encrypt session cache.
  192. func (o *Options) CertCache() string {
  193. return o.certCache
  194. }
  195. // CleanupFrequencyHours returns the interval in hours for cleanup jobs.
  196. func (o *Options) CleanupFrequencyHours() int {
  197. return o.cleanupFrequencyHours
  198. }
  199. // CleanupArchiveReadDays returns the number of days after which marking read items as removed.
  200. func (o *Options) CleanupArchiveReadDays() int {
  201. return o.cleanupArchiveReadDays
  202. }
  203. // CleanupRemoveSessionsDays returns the number of days after which to remove sessions.
  204. func (o *Options) CleanupRemoveSessionsDays() int {
  205. return o.cleanupRemoveSessionsDays
  206. }
  207. // WorkerPoolSize returns the number of background worker.
  208. func (o *Options) WorkerPoolSize() int {
  209. return o.workerPoolSize
  210. }
  211. // PollingFrequency returns the interval to refresh feeds in the background.
  212. func (o *Options) PollingFrequency() int {
  213. return o.pollingFrequency
  214. }
  215. // BatchSize returns the number of feeds to send for background processing.
  216. func (o *Options) BatchSize() int {
  217. return o.batchSize
  218. }
  219. // PollingScheduler returns the scheduler used for polling feeds.
  220. func (o *Options) PollingScheduler() string {
  221. return o.pollingScheduler
  222. }
  223. // SchedulerEntryFrequencyMaxInterval returns the maximum interval in minutes for the entry frequency scheduler.
  224. func (o *Options) SchedulerEntryFrequencyMaxInterval() int {
  225. return o.schedulerEntryFrequencyMaxInterval
  226. }
  227. // SchedulerEntryFrequencyMinInterval returns the minimum interval in minutes for the entry frequency scheduler.
  228. func (o *Options) SchedulerEntryFrequencyMinInterval() int {
  229. return o.schedulerEntryFrequencyMinInterval
  230. }
  231. // IsOAuth2UserCreationAllowed returns true if user creation is allowed for OAuth2 users.
  232. func (o *Options) IsOAuth2UserCreationAllowed() bool {
  233. return o.oauth2UserCreationAllowed
  234. }
  235. // OAuth2ClientID returns the OAuth2 Client ID.
  236. func (o *Options) OAuth2ClientID() string {
  237. return o.oauth2ClientID
  238. }
  239. // OAuth2ClientSecret returns the OAuth2 client secret.
  240. func (o *Options) OAuth2ClientSecret() string {
  241. return o.oauth2ClientSecret
  242. }
  243. // OAuth2RedirectURL returns the OAuth2 redirect URL.
  244. func (o *Options) OAuth2RedirectURL() string {
  245. return o.oauth2RedirectURL
  246. }
  247. // OAuth2OidcDiscoveryEndpoint returns the OAuth2 OIDC discovery endpoint.
  248. func (o *Options) OAuth2OidcDiscoveryEndpoint() string {
  249. return o.oauth2OidcDiscoveryEndpoint
  250. }
  251. // OAuth2Provider returns the name of the OAuth2 provider configured.
  252. func (o *Options) OAuth2Provider() string {
  253. return o.oauth2Provider
  254. }
  255. // HasHSTS returns true if HTTP Strict Transport Security is enabled.
  256. func (o *Options) HasHSTS() bool {
  257. return o.hsts
  258. }
  259. // RunMigrations returns true if the environment variable RUN_MIGRATIONS is not empty.
  260. func (o *Options) RunMigrations() bool {
  261. return o.runMigrations
  262. }
  263. // CreateAdmin returns true if the environment variable CREATE_ADMIN is not empty.
  264. func (o *Options) CreateAdmin() bool {
  265. return o.createAdmin
  266. }
  267. // ProxyImages returns "none" to never proxy, "http-only" to proxy non-HTTPS, "all" to always proxy.
  268. func (o *Options) ProxyImages() string {
  269. return o.proxyImages
  270. }
  271. // HasHTTPService returns true if the HTTP service is enabled.
  272. func (o *Options) HasHTTPService() bool {
  273. return o.httpService
  274. }
  275. // HasSchedulerService returns true if the scheduler service is enabled.
  276. func (o *Options) HasSchedulerService() bool {
  277. return o.schedulerService
  278. }
  279. // PocketConsumerKey returns the Pocket Consumer Key if configured.
  280. func (o *Options) PocketConsumerKey(defaultValue string) string {
  281. if o.pocketConsumerKey != "" {
  282. return o.pocketConsumerKey
  283. }
  284. return defaultValue
  285. }
  286. // HTTPClientTimeout returns the time limit in seconds before the HTTP client cancel the request.
  287. func (o *Options) HTTPClientTimeout() int {
  288. return o.httpClientTimeout
  289. }
  290. // HTTPClientMaxBodySize returns the number of bytes allowed for the HTTP client to transfer.
  291. func (o *Options) HTTPClientMaxBodySize() int64 {
  292. return o.httpClientMaxBodySize
  293. }
  294. // AuthProxyHeader returns an HTTP header name that contains username for
  295. // authentication using auth proxy.
  296. func (o *Options) AuthProxyHeader() string {
  297. return o.authProxyHeader
  298. }
  299. // IsAuthProxyUserCreationAllowed returns true if user creation is allowed for
  300. // users authenticated using auth proxy.
  301. func (o *Options) IsAuthProxyUserCreationAllowed() bool {
  302. return o.authProxyUserCreation
  303. }
  304. func (o *Options) String() string {
  305. var builder strings.Builder
  306. builder.WriteString(fmt.Sprintf("LOG_DATE_TIME: %v\n", o.logDateTime))
  307. builder.WriteString(fmt.Sprintf("DEBUG: %v\n", o.debug))
  308. builder.WriteString(fmt.Sprintf("HTTP_SERVICE: %v\n", o.httpService))
  309. builder.WriteString(fmt.Sprintf("SCHEDULER_SERVICE: %v\n", o.schedulerService))
  310. builder.WriteString(fmt.Sprintf("HTTPS: %v\n", o.HTTPS))
  311. builder.WriteString(fmt.Sprintf("HSTS: %v\n", o.hsts))
  312. builder.WriteString(fmt.Sprintf("BASE_URL: %v\n", o.baseURL))
  313. builder.WriteString(fmt.Sprintf("ROOT_URL: %v\n", o.rootURL))
  314. builder.WriteString(fmt.Sprintf("BASE_PATH: %v\n", o.basePath))
  315. builder.WriteString(fmt.Sprintf("LISTEN_ADDR: %v\n", o.listenAddr))
  316. builder.WriteString(fmt.Sprintf("DATABASE_URL: %v\n", o.databaseURL))
  317. builder.WriteString(fmt.Sprintf("DATABASE_MAX_CONNS: %v\n", o.databaseMaxConns))
  318. builder.WriteString(fmt.Sprintf("DATABASE_MIN_CONNS: %v\n", o.databaseMinConns))
  319. builder.WriteString(fmt.Sprintf("RUN_MIGRATIONS: %v\n", o.runMigrations))
  320. builder.WriteString(fmt.Sprintf("CERT_FILE: %v\n", o.certFile))
  321. builder.WriteString(fmt.Sprintf("KEY_FILE: %v\n", o.certKeyFile))
  322. builder.WriteString(fmt.Sprintf("CERT_DOMAIN: %v\n", o.certDomain))
  323. builder.WriteString(fmt.Sprintf("CERT_CACHE: %v\n", o.certCache))
  324. builder.WriteString(fmt.Sprintf("CLEANUP_FREQUENCY_HOURS: %v\n", o.cleanupFrequencyHours))
  325. builder.WriteString(fmt.Sprintf("CLEANUP_ARCHIVE_READ_DAYS: %v\n", o.cleanupArchiveReadDays))
  326. builder.WriteString(fmt.Sprintf("CLEANUP_REMOVE_SESSIONS_DAYS: %v\n", o.cleanupRemoveSessionsDays))
  327. builder.WriteString(fmt.Sprintf("WORKER_POOL_SIZE: %v\n", o.workerPoolSize))
  328. builder.WriteString(fmt.Sprintf("POLLING_FREQUENCY: %v\n", o.pollingFrequency))
  329. builder.WriteString(fmt.Sprintf("BATCH_SIZE: %v\n", o.batchSize))
  330. builder.WriteString(fmt.Sprintf("POLLING_SCHEDULER: %v\n", o.pollingScheduler))
  331. builder.WriteString(fmt.Sprintf("SCHEDULER_ENTRY_FREQUENCY_MAX_INTERVAL: %v\n", o.schedulerEntryFrequencyMaxInterval))
  332. builder.WriteString(fmt.Sprintf("SCHEDULER_ENTRY_FREQUENCY_MIN_INTERVAL: %v\n", o.schedulerEntryFrequencyMinInterval))
  333. builder.WriteString(fmt.Sprintf("PROXY_IMAGES: %v\n", o.proxyImages))
  334. builder.WriteString(fmt.Sprintf("CREATE_ADMIN: %v\n", o.createAdmin))
  335. builder.WriteString(fmt.Sprintf("POCKET_CONSUMER_KEY: %v\n", o.pocketConsumerKey))
  336. builder.WriteString(fmt.Sprintf("OAUTH2_USER_CREATION: %v\n", o.oauth2UserCreationAllowed))
  337. builder.WriteString(fmt.Sprintf("OAUTH2_CLIENT_ID: %v\n", o.oauth2ClientID))
  338. builder.WriteString(fmt.Sprintf("OAUTH2_CLIENT_SECRET: %v\n", o.oauth2ClientSecret))
  339. builder.WriteString(fmt.Sprintf("OAUTH2_REDIRECT_URL: %v\n", o.oauth2RedirectURL))
  340. builder.WriteString(fmt.Sprintf("OAUTH2_OIDC_DISCOVERY_ENDPOINT: %v\n", o.oauth2OidcDiscoveryEndpoint))
  341. builder.WriteString(fmt.Sprintf("OAUTH2_PROVIDER: %v\n", o.oauth2Provider))
  342. builder.WriteString(fmt.Sprintf("HTTP_CLIENT_TIMEOUT: %v\n", o.httpClientTimeout))
  343. builder.WriteString(fmt.Sprintf("HTTP_CLIENT_MAX_BODY_SIZE: %v\n", o.httpClientMaxBodySize))
  344. builder.WriteString(fmt.Sprintf("AUTH_PROXY_HEADER: %v\n", o.authProxyHeader))
  345. builder.WriteString(fmt.Sprintf("AUTH_PROXY_USER_CREATION: %v\n", o.authProxyUserCreation))
  346. return builder.String()
  347. }