From 4ecc8a8f4d3efba1ec3599d2d25cf9542dfec457 Mon Sep 17 00:00:00 2001 From: "Yann Esposito (Yogsototh)" Date: Sat, 13 Apr 2019 23:47:35 +0200 Subject: [PATCH] Cleaning up --- src/Aggreact.hs | 2 +- src/Aggreact/Slugs/StoreService.hs | 8 +++----- src/Aggreact/Slugs/Types.hs | 6 ------ src/Service/Config.hs | 20 ++++++++++++-------- src/Service/Service.hs | 1 - 5 files changed, 16 insertions(+), 21 deletions(-) diff --git a/src/Aggreact.hs b/src/Aggreact.hs index f935557..3a70146 100644 --- a/src/Aggreact.hs +++ b/src/Aggreact.hs @@ -109,7 +109,7 @@ initialize :: Conf -> IO (Settings,Application) initialize Conf{..} = do uh <- newUserHandler userDBConf defaultAdminUser ch <- newCommentHandler (dbstore uh) commentDBConf - sh <- newSlugHandler (dbstore uh) slugDBConf + sh <- newSlugHandler slugDBConf ah <- newAuthorizationHandler authorizationStrategy myKey <- readKey jwtKeyFilePath let jwtSettings = defaultJWTSettings myKey diff --git a/src/Aggreact/Slugs/StoreService.hs b/src/Aggreact/Slugs/StoreService.hs index b2333d8..1812e42 100644 --- a/src/Aggreact/Slugs/StoreService.hs +++ b/src/Aggreact/Slugs/StoreService.hs @@ -43,12 +43,11 @@ import Protolude -------------------------------------------------------------------------------- import Aggreact.Slugs.Types -import qualified Aggreact.User as User -------------------------------------------------------------------------------- import Data.Time.Clock.Serialize () import Data.Time.Format () -import Database.SQLite.Simple (Only (..), query, query_) +import Database.SQLite.Simple (query_) import Database.Store (DefaultMetas (..), Id (..), Store (..)) @@ -105,10 +104,9 @@ data SlugHandler = } -- | Init a new slug handler -newSlugHandler :: User.DBStore - -> SlugDBConf +newSlugHandler :: SlugDBConf -> IO SlugHandler -newSlugHandler userStore conf = do +newSlugHandler conf = do dbstore <- initDBSlugs conf pure SlugHandler { createSlug = createSlug' dbstore , readSlug = readSlug' dbstore diff --git a/src/Aggreact/Slugs/Types.hs b/src/Aggreact/Slugs/Types.hs index 7c97206..7ccf808 100644 --- a/src/Aggreact/Slugs/Types.hs +++ b/src/Aggreact/Slugs/Types.hs @@ -54,21 +54,15 @@ import Protolude import Aggreact.User (User,UserId) -import qualified Control.Exception as Ex import Data.Aeson (FromJSON (..), ToJSON (..), defaultOptions, genericParseJSON, genericToJSON) -import Data.Char (isAlphaNum) import Data.Data (Data (..)) import qualified Data.IxSet.Typed as IxSet -import qualified Data.Text as Text import Data.Time.Clock.Serialize () import Data.Time.Format () import Data.Typeable (Typeable) -import Data.UUID (UUID) -import qualified Data.UUID as UUID -import Database.SQLite.Simple (SQLData (..)) import Database.SQLite.Simple.FromField (FromField (..)) import Database.SQLite.Simple.FromRow (FromRow (..), field) import Database.SQLite.Simple.ToRow (ToRow (..)) diff --git a/src/Service/Config.hs b/src/Service/Config.hs index 687c2ad..bc5f217 100644 --- a/src/Service/Config.hs +++ b/src/Service/Config.hs @@ -48,7 +48,8 @@ import Data.Yaml import System.FilePath data ConfigService a -newtype ConfigServiceConfig = ConfigServiceConfig { configFile :: FilePath } +newtype ConfigServiceConfig conf = + ConfigServiceConfig { config :: Either conf FilePath } newtype ConfigSvc a = ConfigSvc { getConfig :: a } {- @@ -60,16 +61,19 @@ instance ( a ~ b , Interpret b , FromJSON b ) => Service (ConfigService a) b where - type Config (ConfigService a) b = ConfigServiceConfig + type Config (ConfigService a) b = ConfigServiceConfig b type Handle (ConfigService a) b = ConfigSvc b init _ ConfigServiceConfig{..} = ConfigSvc <$> - case takeExtension configFile of - ".dhall" -> input auto (toS configFile) - ".json" -> parseyaml -- json are yaml - ".yaml" -> parseyaml - _ -> die "unrecognized format (only .dhall, .json and .yaml are supported)" + case config of + Left c -> return c + Right configFile -> + case takeExtension configFile of + ".dhall" -> input auto (toS configFile) + ".json" -> parseyaml configFile -- json are yaml + ".yaml" -> parseyaml configFile + _ -> die "unrecognized format (only .dhall, .json and .yaml are supported)" where - parseyaml = do + parseyaml configFile = do parseResult <- decodeFileEither configFile case parseResult of Left err -> die ("Error while parsing " <> toS configFile <> ":\n" diff --git a/src/Service/Service.hs b/src/Service/Service.hs index b42bb68..f921c10 100644 --- a/src/Service/Service.hs +++ b/src/Service/Service.hs @@ -42,4 +42,3 @@ class Service s a where withService :: Config s a -> (Handle s a -> IO b) -> IO b withService config = bracket (init svc config) (stop svc) where svc = Proxy :: Proxy (Service s a) -