Eventually-decentralized project hosting and management platform
Clone
HTTPS:
darcs clone https://vervis.peers.community/repos/WvWbo
SSH:
darcs clone USERNAME@vervis.peers.community:WvWbo
Tags
TODO
Settings.hs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | {- This file is part of Vervis.
-
- Written in 2016, 2018, 2019, 2022 by fr33domlover <fr33domlover@riseup.net>.
-
- ♡ Copying is an act of love. Please copy, reuse and share.
-
- The author(s) have dedicated all copyright and related and neighboring
- rights to this software to the public domain worldwide. This software is
- distributed without any warranty.
-
- You should have received a copy of the CC0 Public Domain Dedication along
- with this software. If not, see
- <http://creativecommons.org/publicdomain/zero/1.0/>.
-}
{-# Language CPP #-}
-- | Settings are centralized, as much as possible, into this file. This
-- includes database connection settings, static file locations, etc.
-- In addition, you can configure a number of different aspects of Yesod
-- by overriding methods in the Yesod typeclass. That instance is
-- declared in the Foundation.hs file.
module Vervis.Settings where
import Yesod hiding (Header, parseTime)
import Yesod.Static
import Data.Default (Default (..))
import Control.Exception (throw)
import Data.Aeson (Result (..), fromJSON, withObject, (.!=),
(.:?))
import Data.ByteString (ByteString)
import Data.FileEmbed (embedFile)
import Data.String
import Data.Text (Text)
import Data.Time.Clock
import Data.Time.Interval
import Data.Time.Interval.Aeson
import Data.Time.Units
import Data.Yaml (decodeEither')
import Database.Persist.Postgresql (PostgresConf)
import Language.Haskell.TH.Syntax (Exp, Name, Q)
import Network.Wai.Handler.Warp (HostPreference)
import System.FilePath
import Text.Pandoc.Highlighting
import Yesod.Default.Config2 (applyEnvValue, configSettingsYml)
import Yesod.Default.Util (WidgetFileSettings, widgetFileNoReload,
widgetFileReload)
import qualified Data.Text as T
import Yesod.Mail.Send (MailSettings)
import Network.FedURI
import Vervis.FedURI
import Vervis.Settings.TH
developmentMode :: Bool
developmentMode =
#if DEVELOPMENT
True
#else
False
#endif
-- | Directory from which to serve static files.
appStaticDir :: String
appStaticDir = "static"
-- | Use the reload version of templates
appReloadTemplates :: Bool
appReloadTemplates = developmentMode
-- | Perform no stylesheet/script combining
appSkipCombining :: Bool
appSkipCombining = developmentMode
-- | Runtime settings to configure this application. These settings can be
-- loaded from various sources: defaults, environment variables, config files,
-- theoretically even a database.
data AppSettings = AppSettings
{ -- | Directory from which to serve static files.
--appStaticDir :: String
-- | Configuration settings for accessing the database.
appDatabaseConf :: PostgresConf
-- | Maximal number of remote instance-scope keys to cache in our local
-- database per instance.
, appMaxInstanceKeys :: Maybe Int
-- | Maximal number of keys (personal keys or usage of shared keys) to
-- remember cached in our database per remote actor.
, appMaxActorKeys :: Maybe Int
-- | Path of the directory in which DeliveryTheater actor state is stored
, appDeliveryStateDir :: FilePath
-- | The instance's host (e.g. \"dev.angeley.es\"). Used for determining
-- which requests are remote and which are for this instance, and for
-- generating URLs. The database relies on this value, and you shouldn't
-- change it once you deploy an instance.
, appInstanceHost :: Host
-- | Host/interface the server should bind to.
, appHost :: HostPreference
-- | Port to listen on
, appPort :: Int
-- | Get the IP address from the header when logging. Useful when sitting
-- behind a reverse proxy.
, appIpFromHeader :: Bool
-- | Path of session cookie encryption key file
, appClientSessionKeyFile :: FilePath
-- | Idle timeout for session cookie expiration
, appClientSessionTimeout :: TimeInterval
-- Maximal accepted difference between current time and Date header
, appHttpSigTimeLimit :: TimeInterval
-- How often to generate a new actor key for making HTTP signatures
, appActorKeyRotation :: TimeInterval
-- | Whether to use personal actor keys, or an instance-wide key
, appPerActorKeys :: Bool
-- | Use detailed request logging system
, appDetailedRequestLogging :: Bool
-- | Should all log messages be displayed?
, appShouldLogAll :: Bool
-- | Use the reload version of templates
--, appReloadTemplates :: Bool
-- | Assume that files in the static dir may change after compilation
, appMutableStatic :: Bool
-- | Perform no stylesheet/script combining
--, appSkipCombining :: Bool
-- | Load SVG font file from the data file path of the @SVGFonts@
-- library, instead of the app's production runtime data directory.
, appLoadFontFromLibData :: Bool
-- | Path to the directory under which git repos are placed
, appRepoDir :: FilePath
-- | Number of context lines to display around changes in commit diff
, appDiffContextLines :: Int
-- | Path of the Vervis post-receive hook executable
, appPostReceiveHookFile :: FilePath
-- | Path of the Vervis darcs posthook executable
, appPostApplyHookFile :: FilePath
-- | Port for the SSH server component to listen on
, appSshPort :: Int
-- | Path to the server's SSH private key file
, appSshKeyFile :: FilePath
-- | Whether new user accounts can be created.
, appRegister :: Bool
-- | The maximal number of user accounts that can be registered.
, appAccounts :: Maybe Int
-- | Whether to send verification email to new users. If not, users will
-- be instantly considered verified, without sending email.
, appEmailVerification :: Bool
-- | SMTP server details for sending email, and other email related
-- details. If set to 'Nothing', no email will be sent.
, appMail :: Maybe MailSettings
-- | Whether to support federation. This includes:
--
-- * Accept activities from other servers in the inbox
-- * Accept activities from users in the outbox
-- * Deliver local activities to other servers
, appFederation :: Bool
-- | Signing key file for signing object capabilities sent to remote
-- users
, appCapabilitySigningKeyFile :: FilePath
-- | Salt for encoding and decoding hashids
, appHashidsSaltFile :: FilePath
-- | What do to when we wish to insert a new 'VerifKey' or
-- 'VerifKeySharedUsage' into the database, but we've reached the
-- configured storage limit.
--
-- 'True' means we simply reject HTTP signatures when it happens, which
-- means we basically don't support servers that use more keys or custom
-- setup other than what Vervis does.
--
-- 'False' means we do accept HTTP signatures even if we've reached the
-- storage limit setting. We simply handle it by remembering only the
-- amount of keys the limit allows, and otherwise we have to refetch keys
-- over HTTP, which possibly means we have to do more HTTP key fetching,
-- and the target server gets a higher load of key fetch GET requests.
, appRejectOnMaxKeys :: Bool
-- | The duration of time during which a remote actor is unreachable and
-- we periodically retry to deliver them activities. After that period of
-- time, we stop trying to deliver and we remove them from follower lists
-- of local actors.
--
-- TODO this probably isn't working anymore since the switch to DeliveryTheater
, appDropDeliveryAfter :: NominalDiffTime
-- | Base time to wait before first retry of a failed delivery.
, appDeliveryRetryBase :: TimeInterval
-- | How many activities to remember in the debug report list, showing
-- latest activities received in local inboxes and the result of their
-- processing. 'Nothing' means disable the report page entirely.
, appInboxDebugReportLength :: Maybe Int
-- | List of (hosts of) other known federating instances.
, appInstances :: [Text]
-- | Default color scheme for syntax highlighting of code blocks inside
-- documentes rendered with pandoc.
, appHighlightStyle :: Text
-- | COlor scheme to use for UI header, footer, links on pages etc.,
-- should help with visually identifying instances that may otherwise
-- look very much alike. Any number is valid; the scheme is chosen via
-- modulo the number of available schemes.
, appMainColor :: Int
}
instance FromJSON AppSettings where
parseJSON = withObject "AppSettings" $ \ o -> do
let defaultDev = developmentMode
--appStaticDir <- o .: "static-dir"
appDatabaseConf <- o .: "database"
appMaxInstanceKeys <- o .:? "max-instance-keys"
appMaxActorKeys <- o .:? "max-actor-keys"
appDeliveryStateDir <- o .: "delivery-state-dir"
port <- o .: "http-port"
appInstanceHost <- do
h <- o .: "instance-host"
return $
if developmentMode
then Authority h $ Just port
else Authority h Nothing
appHost <- fromString <$> o .: "host"
let appPort = fromIntegral port
appIpFromHeader <- o .: "ip-from-header"
appClientSessionKeyFile <- o .: "client-session-key"
appClientSessionTimeout <- interval <$> o .: "client-session-timeout"
appHttpSigTimeLimit <- interval <$> o .: "request-time-limit"
appActorKeyRotation <- interval <$> o .: "actor-key-rotation"
appPerActorKeys <- o .:? "per-actor-keys" .!= False
appDetailedRequestLogging <- o .:? "detailed-logging" .!= defaultDev
appShouldLogAll <- o .:? "should-log-all" .!= defaultDev
--appReloadTemplates <- o .:? "reload-templates" .!= defaultDev
appMutableStatic <- o .:? "mutable-static" .!= defaultDev
--appSkipCombining <- o .:? "skip-combining" .!= defaultDev
appLoadFontFromLibData <- o .:? "load-font-from-lib-data" .!= defaultDev
appRepoDir <- o .: "repo-dir"
appDiffContextLines <- o .: "diff-context-lines"
appPostReceiveHookFile <- o .:? "post-receive-hook" .!= detectedHookFile
appPostApplyHookFile <- o .:? "post-apply-hook" .!= detectedDarcsHookFile
appSshPort <- o .: "ssh-port"
appSshKeyFile <- o .: "ssh-key-file"
appRegister <- o .: "registration"
appAccounts <- o .: "max-accounts"
appEmailVerification <- o .:? "email-verification" .!= not defaultDev
appMail <- o .:? "mail"
appFederation <- o .:? "federation" .!= False
appCapabilitySigningKeyFile <- o .: "capability-signing-key"
appHashidsSaltFile <- o .: "hashids-salt-file"
appRejectOnMaxKeys <- o .: "reject-on-max-keys"
appDropDeliveryAfter <- ndt <$> o .: "drop-delivery-after"
appDeliveryRetryBase <- interval <$> o .: "retry-delivery-base"
appInboxDebugReportLength <- o .:? "activity-debug-reports"
appInstances <- o .:? "instances" .!= []
appHighlightStyle <- do
s <- o .:? "highlight-style" .!= "zenburn"
case lookup s highlightingStyles of
Nothing -> fail $ "Highlighting style " ++ s ++ " not found"
Just _ -> return $ T.pack s
appMainColor <- o .:? "main-color" .!= 0
return AppSettings {..}
where
toSeconds :: TimeInterval -> Second
toSeconds = toTimeUnit
ndt = fromIntegral . toSeconds . interval
detectedHookFile = $localInstallRoot </> "bin" </> "vervis-post-receive"
detectedDarcsHookFile = $localInstallRoot </> "bin" </> "vervis-post-apply"
-- | Settings for 'widgetFile', such as which template languages to support and
-- default Hamlet settings.
--
-- For more information on modifying behavior, see:
--
-- https://github.com/yesodweb/yesod/wiki/Overriding-widgetFile
widgetFileSettings :: WidgetFileSettings
widgetFileSettings = def
-- | How static files should be combined.
combineSettings :: CombineSettings
combineSettings = def
-- The rest of this file contains settings which rarely need changing by a
-- user.
widgetFile :: String -> Q Exp
widgetFile =
let wf =
if appReloadTemplates
then widgetFileReload
else widgetFileNoReload
in wf widgetFileSettings
{-
-- | Raw bytes at compile time of @config/settings.yml@
configSettingsYmlBS :: ByteString
configSettingsYmlBS = $(embedFile configSettingsYml)
-- | @config/settings.yml@, parsed to a @Value@.
configSettingsYmlValue :: Value
configSettingsYmlValue = either throw id $ decodeEither' configSettingsYmlBS
-- | A version of @AppSettings@ parsed at compile time from
-- @config/settings.yml@.
compileTimeAppSettings :: AppSettings
compileTimeAppSettings =
case fromJSON $ applyEnvValue False mempty configSettingsYmlValue of
Error e -> error e
Success settings -> settings
-}
-- The following two functions can be used to combine multiple CSS or JS files
-- at compile time to decrease the number of http requests.
-- Sample usage (inside a Widget):
--
-- > $(combineStylesheets 'StaticR [style1_css, style2_css])
combineStylesheets :: Name -> [Route Static] -> Q Exp
combineStylesheets =
combineStylesheets'
appSkipCombining
combineSettings
combineScripts :: Name -> [Route Static] -> Q Exp
combineScripts =
combineScripts'
appSkipCombining
combineSettings
|