Adds configuration from env variables

master
Abhinav Sarkar 2017-09-30 20:32:07 +05:30
parent dbf198fe6b
commit 8dcd8d253e
4 changed files with 65 additions and 17 deletions

View File

@ -9,16 +9,15 @@
"dependencies": {
"purescript-prelude": "^3.1.0",
"purescript-console": "^3.0.0",
"purescript-foreign-generic": "^5.0.0",
"purescript-foreign-generic": "^4.1.0",
"purescript-postgresql-client": "^2.1.0",
"purescript-aff": "^3.1.0",
"purescript-express": "^0.5.2",
"purescript-integers": "^3.1.0"
"purescript-integers": "^3.1.0",
"purescript-node-process": "^4.0.0",
"purescript-config": "^0.0.6"
},
"devDependencies": {
"purescript-psci-support": "^3.0.0"
},
"resolutions": {
"purescript-foreign-generic": "^4.1.0"
}
}

8
setenv.sh Normal file
View File

@ -0,0 +1,8 @@
export SS_PORT=4000
export SS_DB_USER="abhinav"
export SS_DB_PASSWORD=""
export SS_DB_HOST="localhost"
export SS_DB_PORT=5432
export SS_DB_DATABASE="simple_server"
export SS_DB_POOL_SIZE=10
export SS_DB_IDLE_CONN_TIMEOUT_MILLIS=1000

View File

@ -3,23 +3,24 @@ module Main where
import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE)
import Control.Monad.Eff.Console (CONSOLE, log)
import Data.Either (Either(..))
import Data.Set (toUnfoldable)
import Data.String (joinWith)
import Database.PostgreSQL as PG
import Node.Express.Types (EXPRESS)
import Node.Process (PROCESS)
import Node.Process as Process
import SimpleService.Config (readServerConfig)
import SimpleService.Server (runServer)
main :: forall eff. Eff ( console :: CONSOLE
, express :: EXPRESS
, postgreSQL :: PG.POSTGRESQL
, process :: PROCESS
| eff) Unit
main = runServer port databaseConfig
where
port = 4000
databaseConfig = { user: "abhinav"
, password: ""
, host: "localhost"
, port: 5432
, database: "simple_service"
, max: 10
, idleTimeoutMillis: 1000
}
main = readServerConfig >>= case _ of
Left missingKeys -> do
log $ "Unable to start. Missing Env keys: " <> joinWith ", " (toUnfoldable missingKeys)
Process.exit 1
Right { port, databaseConfig } -> runServer port databaseConfig

View File

@ -0,0 +1,40 @@
module SimpleService.Config where
import Data.Config
import Prelude
import Control.Monad.Eff (Eff)
import Data.Config.Node (fromEnv)
import Data.Either (Either)
import Data.Set (Set)
import Database.PostgreSQL as PG
import Node.Process (PROCESS)
type ServerConfig =
{ port :: Int
, databaseConfig :: PG.PoolConfiguration
}
databaseConfig :: Config {name :: String} PG.PoolConfiguration
databaseConfig =
{ user: _, password: _, host: _, port: _, database: _, max: _, idleTimeoutMillis: _ }
<$> string {name: "user"}
<*> string {name: "password"}
<*> string {name: "host"}
<*> int {name: "port"}
<*> string {name: "database"}
<*> int {name: "pool_size"}
<*> int {name: "idle_conn_timeout_millis"}
portConfig :: Config {name :: String} Int
portConfig = int {name: "port"}
serverConfig :: Config {name :: String} ServerConfig
serverConfig =
{ port: _, databaseConfig: _}
<$> portConfig
<*> prefix {name: "db"} databaseConfig
readServerConfig :: forall eff.
Eff (process :: PROCESS | eff) (Either (Set String) ServerConfig)
readServerConfig = fromEnv "SS" serverConfig