summaryrefslogtreecommitdiff
path: root/lambda-calcul/haskell/src/Minilang/Server.hs
diff options
context:
space:
mode:
authorArnaud Bailly <arnaud@pankzsoft.com>2025-11-05 06:44:24 +0100
committerArnaud Bailly <arnaud@pankzsoft.com>2025-11-05 06:44:24 +0100
commitdff1c912eb69ad1f97f030bd0fdb7d440b2dc534 (patch)
treef8c196f6236dfc7e39958cb8194e207c67458d28 /lambda-calcul/haskell/src/Minilang/Server.hs
parentcd46eaf57fac4e452f11747a1d420f901c630552 (diff)
downloadlambda-nantes-dff1c912eb69ad1f97f030bd0fdb7d440b2dc534.tar.gz
use default challenges if file does not exist
Diffstat (limited to 'lambda-calcul/haskell/src/Minilang/Server.hs')
-rw-r--r--lambda-calcul/haskell/src/Minilang/Server.hs14
1 files changed, 10 insertions, 4 deletions
diff --git a/lambda-calcul/haskell/src/Minilang/Server.hs b/lambda-calcul/haskell/src/Minilang/Server.hs
index ea66e1e..7cf5718 100644
--- a/lambda-calcul/haskell/src/Minilang/Server.hs
+++ b/lambda-calcul/haskell/src/Minilang/Server.hs
@@ -13,7 +13,7 @@ import Data.Aeson
decode,
eitherDecodeFileStrict,
object,
- (.=),
+ (.=), eitherDecode,
)
import Data.ByteString.Lazy (fromStrict)
import Data.Data (Proxy (..))
@@ -31,6 +31,8 @@ import Servant (hoistServer)
import Servant.Server (serve)
import System.Log.FastLogger (fromLogStr)
import System.Random (newStdGen)
+import Control.Exception (try)
+import qualified Data.ByteString.Lazy as LBS
data Server = Server
{ -- | If the server is running, this will contain the underlying
@@ -71,9 +73,13 @@ startServer serverPort = do
readChallenges :: IO [Challenge]
readChallenges = do
- eitherDecodeFileStrict "challenges.json" >>= \case
- Right challenges -> pure challenges
- Left _ -> pure defaultChallenges
+ file <- try $ LBS.readFile "challenges.json"
+ case file of
+ Left (_ :: IOError) -> pure defaultChallenges
+ Right content -> case eitherDecode content of
+ Right challenges -> pure challenges
+ Left _ -> pure defaultChallenges
+
serveAPI :: Env -> Application
serveAPI env =