you're telling me a puppy coded this??
{-# LANGUAGE TemplateHaskell #-}

-- | Allows access to files relevant to the server only.
module Puppy.Files where

import Effectful
import Effectful.TH (makeEffect)
import qualified Data.ByteString.Lazy as LBS
import qualified Data.Aeson as JSON
import Effectful.Dispatch.Dynamic
import System.Directory (doesFileExist)

data Files :: Effect where
  ReadConfigFile  :: Files m (Maybe JSON.Object)
  ReadFavicon     :: Files m LBS.ByteString  
  ReadServerKey   :: Files m (Maybe LBS.ByteString)
  WriteConfigFile :: JSON.Object -> Files m ()
  WriteServerKey  :: LBS.ByteString -> Files m ()

makeEffect ''Files

runFiles
  :: (IOE :> es)
  => FilePath
  -- ^ The path to the state directory
  -> FilePath
  -- ^ The path to the static resources directory
  -> Eff (Files : es) a
  -> Eff es a
runFiles stateMnt resMnt = interpret $ \_ -> liftIO . \case
  ReadConfigFile -> do
    let fp = stateMnt <> "/config.json"
    doesFileExist fp >>= \case
      True -> JSON.decode <$> LBS.readFile fp
      False -> return Nothing   
  ReadFavicon -> LBS.readFile (resMnt <> "/favicon.png")
  ReadServerKey -> do
    let fp = stateMnt <> "/serverKey"
    doesFileExist fp >>= \case
      True -> Just <$> LBS.readFile fp
      False -> return Nothing   
  WriteConfigFile cfg -> LBS.writeFile (stateMnt <> "/config.json") (JSON.encode cfg)
  WriteServerKey key -> LBS.writeFile (stateMnt <> "/serverKey") key