diff --git a/config.toml b/config.toml new file mode 100644 index 0000000000000000000000000000000000000000..94e189cdfe5b00958725d27b59917776e2398a09 --- /dev/null +++ b/config.toml @@ -0,0 +1,8 @@ + + +port = 8080 + +tmpdir = "/tmp" +entrypoint = "main.json" +lintconfig = "./config.json" + diff --git a/package.yaml b/package.yaml index 7208cd9ff4a0dc6169f7c3b101614f56d838ed63..a4681bc45ebef76578042f3ec2d434fd05ce7834 100644 --- a/package.yaml +++ b/package.yaml @@ -73,3 +73,4 @@ executables: - containers - microlens - microlens-th + - tomland diff --git a/server/Main.hs b/server/Main.hs index ecaf6b795527e7d02c8218fea4488ceb00c21e0e..0fbc4b4aa801ee06547519ee4ab47633821b51be 100644 --- a/server/Main.hs +++ b/server/Main.hs @@ -71,7 +71,6 @@ app config = main :: IO () main = do + config' <- loadConfig "./config.toml" state <- newMVar defaultState - let config = Config "/tmp" 8080 "main.json" "./config.json" - config' <- loadConfig config - run (port config) (app config' state) + run (port config') (app config' state) diff --git a/server/Server.hs b/server/Server.hs index 93bfb30b78659eba7a9fcd8da147c97eed1d5d8b..a5a820a5fc49fdde3f0ad242bbd1fc16478d2619 100644 --- a/server/Server.hs +++ b/server/Server.hs @@ -1,12 +1,16 @@ -{-# LANGUAGE DataKinds #-} -{-# LANGUAGE DeriveAnyClass #-} -{-# LANGUAGE DeriveGeneric #-} -{-# LANGUAGE KindSignatures #-} -{-# LANGUAGE ScopedTypeVariables #-} -{-# LANGUAGE TemplateHaskell #-} -{-# LANGUAGE TypeApplications #-} -{-# LANGUAGE TypeFamilies #-} -{-# LANGUAGE TypeOperators #-} +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE DeriveAnyClass #-} +{-# LANGUAGE DeriveGeneric #-} +{-# LANGUAGE DerivingStrategies #-} +{-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE GeneralizedNewtypeDeriving #-} +{-# LANGUAGE KindSignatures #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE TemplateHaskell #-} +{-# LANGUAGE TypeApplications #-} +{-# LANGUAGE TypeFamilies #-} +{-# LANGUAGE TypeOperators #-} module Server (loadConfig, Config(..), RemoteRef(..), State, registry, jobs, JobStatus(..), setJobStatus,defaultState,setRegistry) where @@ -23,6 +27,10 @@ import GHC.Generics (Generic) import Lens.Micro (over) import Lens.Micro.TH import LintConfig (LintConfig') +import System.Exit.Compat (exitFailure) +import Toml (TomlCodec) +import qualified Toml +import Toml.Codec ((.=)) -- | a reference in a remote git repository @@ -36,14 +44,21 @@ type family ConfigRes (b :: Bool) a where ConfigRes False a = FilePath -- | the server's configuration -data Config l = Config +data Config (loaded :: Bool) = Config { tmpdir :: FilePath -- ^ dir to clone git things in , port :: Int -- ^ port to bind to , entrypoint :: FilePath - , lintconfig :: ConfigRes l LintConfig' - } + , lintconfig :: ConfigRes loaded LintConfig' + } deriving Generic + +configCodec :: TomlCodec (Config False) +configCodec = Config + <$> Toml.string "tmpdir" .= tmpdir + <*> Toml.int "port" .= port + <*> Toml.string "entrypoint" .= entrypoint + <*> Toml.string "lintconfig" .= lintconfig data JobStatus = Pending | Linted DirResult | Failed Text @@ -59,9 +74,17 @@ makeLenses ''State defaultState :: State defaultState = State mempty mempty +loadConfig :: FilePath -> IO (Config True) +loadConfig path = do + res <- Toml.decodeFileEither configCodec path + case res of + Right config -> loadConfig' config + Left err -> do + print err + exitFailure -loadConfig :: Config False -> IO (Config True) -loadConfig config = do +loadConfig' :: Config False -> IO (Config True) +loadConfig' config = do loaded <- LB.readFile (lintconfig config) >>= \res -> case eitherDecode res :: Either String LintConfig' of Left err -> error $ "config file invalid: " <> err diff --git a/walint.cabal b/walint.cabal index 9f37d59f945730966210b9c26b2529aae2a95641..b9982d1270bc85341e611357cb2772d1653961a1 100644 --- a/walint.cabal +++ b/walint.cabal @@ -88,6 +88,7 @@ executable server , string-conversions , text , time + , tomland , uuid , wai , walint