Broken reading of config file. Need to create som sort of singleton at start of app so we don't need to read the config file multiple times

This commit is contained in:
Rolf Martin Glomsrud 2023-05-05 16:09:12 +02:00
parent 299548383e
commit c6af3665e0
6 changed files with 16 additions and 15 deletions

View file

@ -1,5 +1,5 @@
{ {
"dataseedpath" : "./data/small.json", "dataseedpath" : "data/small.json",
"dbPath" : "data/db.db", "dbPath" : "data/db.db",
"bulkDataLink" : "https://data.scryfall.io/default-cards/default-cards-20230430090702.json" "bulkDataLink" : "https://data.scryfall.io/default-cards/default-cards-20230430090702.json"
} }

View file

@ -48,6 +48,7 @@ library
, http-client-tls , http-client-tls
, scotty , scotty
, sqlite-simple , sqlite-simple
, strict
, text , text
default-language: Haskell2010 default-language: Haskell2010
@ -68,6 +69,7 @@ executable mtgsearch-exe
, mtgsearch , mtgsearch
, scotty , scotty
, sqlite-simple , sqlite-simple
, strict
, text , text
default-language: Haskell2010 default-language: Haskell2010
@ -89,5 +91,6 @@ test-suite mtgsearch-test
, mtgsearch , mtgsearch
, scotty , scotty
, sqlite-simple , sqlite-simple
, strict
, text , text
default-language: Haskell2010 default-language: Haskell2010

View file

@ -29,6 +29,7 @@ dependencies:
- directory - directory
- http-client - http-client
- http-client-tls - http-client-tls
- strict
ghc-options: ghc-options:
- -Wall - -Wall
- -Wcompat - -Wcompat

View file

@ -67,7 +67,7 @@ cardToHtml (Card _ _ _ _ _ _ _ _ [cardFace]) = singleCardFaceHTML cardFace
cardToHtml (Card _ _ _ _ _ _ _ _ cardFaces) = "<div style=\"text-align:center;\"><div style=\"display: inline-flex\">" ++ concatMap singleCardFaceHTML cardFaces ++"</div></div>" cardToHtml (Card _ _ _ _ _ _ _ _ cardFaces) = "<div style=\"text-align:center;\"><div style=\"display: inline-flex\">" ++ concatMap singleCardFaceHTML cardFaces ++"</div></div>"
singleCardFaceHTML :: CardFace -> String singleCardFaceHTML :: CardFace -> String
singleCardFaceHTML (CardFace _ _ name cmc oracle_text type_line mana_cost (ImageUris _ _ _ image _ _ _ _)) = singleCardFaceHTML (CardFace _ _ name _ oracle_text type_line mana_cost (ImageUris _ _ _ image _ _ _ _)) =
"<div style=\"text-align:center;\">" ++ "<div style=\"text-align:center;\">" ++
"<h2>" ++ unpack name ++ "</h2>" ++ "<h2>" ++ unpack name ++ "</h2>" ++
"<img src=" ++ unpack image ++ " width=\"200px\"/>"++ "<img src=" ++ unpack image ++ " width=\"200px\"/>"++
@ -76,7 +76,3 @@ singleCardFaceHTML (CardFace _ _ name cmc oracle_text type_line mana_cost (Image
"<p style=\"width:205px;margin: 5 auto;font-size:16;\">Mana cost: " ++ filter (`notElem` ['{','}']) (unpack (Data.Maybe.fromMaybe "" mana_cost)) ++ "</p>"++ "<p style=\"width:205px;margin: 5 auto;font-size:16;\">Mana cost: " ++ filter (`notElem` ['{','}']) (unpack (Data.Maybe.fromMaybe "" mana_cost)) ++ "</p>"++
" </div>" " </div>"
parseCMC :: Maybe Int -> String
parseCMC (Just a) = show a
parseCMC Nothing = ""

View file

@ -6,13 +6,14 @@ import Data.Aeson
import Data.Text import Data.Text
import qualified Data.ByteString.Lazy as B import qualified Data.ByteString.Lazy as B
import qualified Data.ByteString.Lazy.Char8 as C8 (pack)
import GHC.Generics import GHC.Generics
import Control.Exception (try) import Control.Exception (try)
import Data.ByteString.Builder (lazyByteString)
import qualified Data.Aeson.Key as B
import qualified Data.Data as B
import GHC.IO.Exception import GHC.IO.Exception
import System.IO.Strict as L
import Data.ByteString.Builder (lazyByteString)
data Config = Config{ data Config = Config{
@ -31,10 +32,10 @@ configFile = "./Config/config.json"
getJSON :: IO B.ByteString getJSON :: IO B.ByteString
getJSON = do getJSON = do
a <- try $ B.readFile configFile :: IO (Either IOException B.ByteString ) a <- try $ L.readFile configFile :: IO (Either IOException String)
case a of case a of
Right a -> return a Right a2 -> return $ C8.pack a2
Left b -> error $ "Could not load configuration file" Left b -> error $ "Could not load configuration file " ++ (show b)
@ -51,11 +52,11 @@ readConfig = do
getConfig :: IO Config getConfig :: IO Config
getConfig = extract readConfig getConfig = extract readConfig
getDataSeedPath ::IO (String) getDataSeedPath ::IO String
getDataSeedPath = do getDataSeedPath = do
dataseedpath <$> getConfig dataseedpath <$> getConfig
getDbPath ::IO (String) getDbPath ::IO String
getDbPath = do getDbPath = do
dbPath <$> getConfig dbPath <$> getConfig

View file

@ -5,7 +5,7 @@
<div style="text-align: center;"> <div style="text-align: center;">
<h1>TMagic the gathering search engine!</h1> <h1>TMagic the gathering search engine!</h1>
<p>This site will be to search for magic the gathering cards using a custom sort of DSL!</p> <p>In order to use this site</p>
<form style="text-align: center;" method="POST" action="/api/req"> <form style="text-align: center;" method="POST" action="/api/req">