commit 3863ddf42e3564940bc52de008fadb28676788be
parent 19c6801d12341056ac2bea9901d8e8b3ed64eba8
Author: James Eversole <james@eversole.co>
Date: Sat, 24 Feb 2024 13:07:33 -0600
Remove redundant entry point; adjust comments in main init; update init function names from 'main'
Diffstat:
6 files changed, 40 insertions(+), 48 deletions(-)
diff --git a/Purr.cabal b/Purr.cabal
@@ -16,8 +16,7 @@ extra-source-files:
executable Purr
main-is: Main.hs
hs-source-dirs:
- app
- , src
+ src
default-extensions:
ConstraintKinds
DeriveGeneric
@@ -64,5 +63,4 @@ executable Purr
Feature.Sharing.HTTP
Feature.Sharing.SQLite
Feature.Sharing.Templates
- Lib
default-language: Haskell2010
diff --git a/app/Main.hs b/app/Main.hs
@@ -1,7 +0,0 @@
-module Main where
-
-import Prelude
-import qualified Lib
-
-main :: IO ()
-main = Lib.main
diff --git a/src/Core/Configuration.hs b/src/Core/Configuration.hs
@@ -1,21 +1,22 @@
module Core.Configuration ( adminEmail, appPort
, confLinkLength, dataPath, dbPath
, encKey, getRuntimeEnvironment
- , keyFileInit, main) where
+ , keyFileInit, init) where
import qualified Data.ByteString as B
import Control.Monad (mapM)
+import Configuration.Dotenv
import Core.Types
import Crypto.Saltine.Core.SecretBox (newKey)
import Crypto.Saltine.Class (encode)
-import Configuration.Dotenv
+import Prelude hiding (init)
import System.Directory (doesFileExist)
import System.Environment (getEnv, lookupEnv)
-- Load environment variables from dotenv file if required
-main :: IO ()
-main = do
+init :: IO ()
+init = do
reqEnvLookup <- getRequiredEnv
if (Nothing `elem` reqEnvLookup)
then checkEnvFile requiredEnvVars
diff --git a/src/Core/SQLite.hs b/src/Core/SQLite.hs
@@ -6,12 +6,13 @@ import Core.Types
import Data.ByteString as B
import Database.SQLite.Simple
import Database.SQLite.Simple.FromRow
+import Prelude hiding (init)
import qualified Data.Text as T
-- Set up SQLite database table when Purr starts if it doesn't already exist
-main :: IO ()
-main = do
+init :: IO ()
+init = do
conn <- open dbPath
execute_ conn
"CREATE TABLE IF NOT EXISTS pws\
diff --git a/src/Lib.hs b/src/Lib.hs
@@ -1,32 +0,0 @@
-module Lib ( main ) where
-
-import qualified Core.Configuration as Configuration
-import qualified Core.HTTP as HTTP
-import qualified Core.SQLite as DB
-import Core.Types
-
-import Crypto.Saltine (sodiumInit)
-import GHC.Natural (popCountNatural)
-import Prelude hiding (id)
-import Web.Scotty
-
--- Purr's entrypoint
-main :: IO ()
-main = do
- -- Initialize the RNG used for sodium encryption (Saltine library)
- sodiumInit
- {- Initialize our dotenv configuration which reads from a .env configuration
- file unless the PURRNOFILE env var exists already. -}
- Configuration.main
- {- Initialize the encryption key file if it doesn't
- exist yet or use the existing key -}
- Configuration.keyFileInit
- {- Initialize our database by ensuring the SQLite file exists
- and has tables setup as the application expects -}
- DB.main
- {- Get the configured port to run on and start the Scotty webserver app
- defined in HTTP.app -}
- appPortStr <- Configuration.appPort
- env <- Configuration.getRuntimeEnvironment
- let appPort = read appPortStr :: Int
- scotty appPort (HTTP.app env)
diff --git a/src/Main.hs b/src/Main.hs
@@ -0,0 +1,31 @@
+module Main ( main ) where
+
+import qualified Core.Configuration as Configuration
+import qualified Core.HTTP as HTTP
+import qualified Core.SQLite as DB
+import Core.Types
+
+import Crypto.Saltine (sodiumInit)
+import GHC.Natural (popCountNatural)
+import Prelude hiding (id)
+import Web.Scotty
+
+-- Purr's entrypoint
+main :: IO ()
+main = do
+ -- Initialize the RNG used for sodium encryption (Saltine library)
+ sodiumInit
+ {- Initialize our dotenv configuration which reads from a .env configuration
+ if any required variables are missing from the provided environment -}
+ Configuration.init
+ {- Initialize the encryption key file if it doesn't
+ exist yet or use the existing key -}
+ Configuration.keyFileInit
+ {- Initialize our database by ensuring the SQLite file exists
+ and has tables setup as the application expects -}
+ DB.init
+ {- Get the configured port to run on and start the Scotty webserver app
+ defined in HTTP.app -}
+ port <- Configuration.appPort
+ env <- Configuration.getRuntimeEnvironment
+ scotty (read port) (HTTP.app env)