config: use new form + add example [skip ci]

* also unstage config.dhall and add it to the list of the ignored
This commit is contained in:
leo 2023-05-04 21:40:25 +02:00
parent 746eb82f67
commit d9265cafa8
Signed by: wanderer
SSH Key Fingerprint: SHA256:Dp8+iwKHSlrMEHzE3bJnPng70I7LEsa3IJXRH/U+idQ
3 changed files with 25 additions and 95 deletions

3
.gitignore vendored

@ -1,6 +1,9 @@
# main binary
pcmt
# dev config.
/config.dhall
# air tmp dir
/tmp

@ -1,95 +0,0 @@
-- convenience funcs for validation.
let Prelude =
https://prelude.dhall-lang.org/v20.2.0/package.dhall
sha256:a6036bc38d883450598d1de7c98ead113196fe2db02e9733855668b18096f07b
let NuConfig =
-- | define a configuration schema.
{ Type =
{ Port : Natural
, AppName : Text
, LiveMode : Bool
, DevelMode : Bool
, LoginCookieName : Optional Text
}
, default =
{ Port = 3000
, AppName = "pcmt"
, LiveMode = False
, DevelMode = False
, LoginCookieName = None Text
}
}
let NuConfig/validate
: NuConfig.Type -> Type
=
-- | define validation.
\(c : NuConfig.Type) ->
let expected = { validPort = True }
let actual =
{ validPort =
-- | make sure port number belongs to the <1;65535> range.
Prelude.Natural.lessThanEqual 1 c.Port
&& Prelude.Natural.lessThanEqual c.Port 65535
}
in expected === actual
let nuconf = NuConfig::{ LiveMode = True, DevelMode = True }
let _ =
-- | validate the configuration.
assert : NuConfig/validate nuconf
let Config =
-- | define configuration schema.
{ Host : Text
, Port : Natural
, AppName : Text
, LiveMode : Bool
, DevelMode : Bool
, SessionCookieName : Text
, SessionCookieSecret : Text
}
let defconf
-- | a full default configuration.
-- | TODO: have this reside on the Internet and import it similar to how
-- | the Dhall Prelude is imported.
: Config
= { Host = ""
, Port = 3000
, AppName = "pcmt"
, LiveMode = False
, DevelMode = False
, SessionCookieName = "sessionz"
, SessionCookieSecret = "secret"
}
let conf
: Config
= defconf // { LiveMode = True, DevelMode = False }
let Config/validate
: Config -> Type
=
-- | define validation.
\(config : Config) ->
let expected = { validPort = True }
let actual =
{ validPort =
-- | make sure port number belongs to the <1;65535> range.
Prelude.Natural.lessThanEqual 1 config.Port
&& Prelude.Natural.lessThanEqual config.Port 65535
}
in expected === actual
let _ =
-- | validate the configuration.
assert : Config/validate conf
in conf

22
exampleConfig.dhall Normal file

@ -0,0 +1,22 @@
{- import config schema that is integrity-check protected -}
let ConfigSchema =
https://git.dotya.ml/mirre-mt/pcmt/raw/branch/development/config/schema/package.dhall
sha256:ad7ba86d5d388a99b7543faa0e4c81ba1d9b78fa6c32fdaf4ac4477089d177be
let Config = ConfigSchema.Schema
let c =
{- below we allow overriding config values using env vars -}
Config::{
, Host = env:PCMT_HOST as Text ? Config.default.Host
, LiveMode = env:PCMT_LIVE ? True
, DevelMode = env:PCMT_DEVEL ? True
}
let _ =
{- validate that the above adheres to the config schema and contains valid
entries (not everything gets validated though).
-}
assert : ConfigSchema.Schema/validate c
in c