From 8eb44f25441e58f27884c2a87b4505555f74dffc Mon Sep 17 00:00:00 2001 From: 1computer1 Date: Tue, 23 Jul 2019 01:24:02 -0400 Subject: [PATCH] Separate lib and exe; add languageDir setting --- {src => app}/Main.hs | 15 +++------------ config.dhall | 2 ++ package.yaml | 10 ++++++++-- src/Myriad.hs | 21 +++++++++++++++++++++ src/Myriad/Core.hs | 1 + src/Myriad/Docker.hs | 5 +++-- 6 files changed, 38 insertions(+), 16 deletions(-) rename {src => app}/Main.hs (60%) create mode 100644 src/Myriad.hs diff --git a/src/Main.hs b/app/Main.hs similarity index 60% rename from src/Main.hs rename to app/Main.hs index 53826f1..84243fd 100644 --- a/src/Main.hs +++ b/app/Main.hs @@ -1,13 +1,10 @@ module Main where import qualified Data.Text as T + import Options.Applicative -import Network.Wai.Handler.Warp (run) - -import Myriad.Core -import Myriad.Docker -import Myriad.Server +import Myriad data Args = Args { configInput :: T.Text @@ -26,10 +23,4 @@ parseArgs = execParser $ info (helper <*> args) (fullDesc <> progDesc "Run the M main :: IO () main = do Args { configInput } <- parseArgs - env <- initEnv configInput - runMyriadT env do - buildAllImages - startCleanup - logInfo ["Finished Docker-related setup"] - logInfo ["Starting server"] - run (fromIntegral . port . config $ env) $ app env + runMyriadServer configInput diff --git a/config.dhall b/config.dhall index 9e990eb..b6958a3 100644 --- a/config.dhall +++ b/config.dhall @@ -24,6 +24,7 @@ let Config : Type = , prepareContainers : Bool -- Whether to setup all containers on startup , cleanupInterval : Natural -- The interval in minutes to kill containers periodically , port : Natural -- Port to run on + , languagesDir : Text -- Where the languages are stored } -- Write your config here! @@ -35,5 +36,6 @@ let config : Config = , prepareContainers = False , cleanupInterval = 30 , port = 8081 + , languagesDir = "./languages" } in config diff --git a/package.yaml b/package.yaml index fc55a70..39962c2 100644 --- a/package.yaml +++ b/package.yaml @@ -20,12 +20,12 @@ dependencies: - bytestring - containers - dhall +- filepath - lifted-base - lifted-async - monad-control - monad-logger - mtl -- optparse-applicative - servant - servant-server - snowflake @@ -66,11 +66,17 @@ default-extensions: - TypeFamilies - ViewPatterns +library: + source-dirs: src + executable: main: Main.hs - source-dirs: src + source-dirs: app ghc-options: - -threaded - -rtsopts - -with-rtsopts=-N - -Wall + dependencies: + - myriad + - optparse-applicative diff --git a/src/Myriad.hs b/src/Myriad.hs new file mode 100644 index 0000000..9104735 --- /dev/null +++ b/src/Myriad.hs @@ -0,0 +1,21 @@ +module Myriad + ( runMyriadServer + ) where + +import qualified Data.Text as T + +import Network.Wai.Handler.Warp (run) + +import Myriad.Core +import Myriad.Docker +import Myriad.Server + +runMyriadServer :: T.Text -> IO () +runMyriadServer configInput = do + env <- initEnv configInput + runMyriadT env do + buildAllImages + startCleanup + logInfo ["Finished Docker-related setup"] + logInfo ["Starting server"] + run (fromIntegral . port . config $ env) $ app env diff --git a/src/Myriad/Core.hs b/src/Myriad/Core.hs index 6b2d57e..4e015e6 100644 --- a/src/Myriad/Core.hs +++ b/src/Myriad/Core.hs @@ -61,6 +61,7 @@ data MyriadConfig = MyriadConfig , prepareContainers :: Bool , cleanupInterval :: Natural , port :: Natural + , languagesDir :: T.Text } deriving (Show, Generic) instance Interpret MyriadConfig diff --git a/src/Myriad/Docker.hs b/src/Myriad/Docker.hs index ff4d5fc..ace7794 100644 --- a/src/Myriad/Docker.hs +++ b/src/Myriad/Docker.hs @@ -20,17 +20,18 @@ import Control.Concurrent.Lifted (fork, threadDelay) import Control.Concurrent.MVar.Lifted import Control.Concurrent.QSem.Lifted import Control.Exception.Lifted +import System.FilePath (()) import System.Process.Typed import Myriad.Core buildImage :: Myriadic m => LanguageConfig -> m () buildImage lang@LanguageConfig { name, concurrent } = do + MyriadConfig { prepareContainers, languagesDir } <- asks config logInfo ["Building image ", cs $ imageName lang] - exec_ ["docker build -t ", imageName lang, " ./languages/", cs name] + exec_ ["docker build -t ", imageName lang, " ", cs languagesDir cs name] setupQSems logInfo ["Built image ", cs $ imageName lang] - MyriadConfig { prepareContainers } <- asks config when_ prepareContainers $ setupContainer lang where setupQSems :: Myriadic m => m ()