diff --git a/bower.json b/bower.json index d890567..23bb7d8 100644 --- a/bower.json +++ b/bower.json @@ -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" } } diff --git a/setenv.sh b/setenv.sh new file mode 100644 index 0000000..eab31be --- /dev/null +++ b/setenv.sh @@ -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 diff --git a/src/Main.purs b/src/Main.purs index 96cedc9..aae1a9d 100644 --- a/src/Main.purs +++ b/src/Main.purs @@ -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 diff --git a/src/SimpleService/Config.purs b/src/SimpleService/Config.purs new file mode 100644 index 0000000..0dbdcd5 --- /dev/null +++ b/src/SimpleService/Config.purs @@ -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