style/refactor

This commit is contained in:
1computer1 2020-06-16 22:37:38 -04:00
parent 3a10e3e72c
commit e2942e38b9
5 changed files with 43 additions and 37 deletions

View file

@ -8,8 +8,8 @@ import Data.String.Conversions
import Network.Wai.Handler.Warp import Network.Wai.Handler.Warp
import Myriad.Core
import Myriad.Config import Myriad.Config
import Myriad.Core
import Myriad.Docker import Myriad.Docker
import Myriad.Server import Myriad.Server

View file

@ -6,9 +6,9 @@ module Myriad.Config
) where ) where
import qualified Data.ByteString.Lazy as BL import qualified Data.ByteString.Lazy as BL
import Data.Maybe import Data.Maybe
import qualified Data.Text as T import qualified Data.Text as T
import Data.YAML import Data.YAML
type LanguageName = T.Text type LanguageName = T.Text
@ -29,26 +29,36 @@ data Language = Language
, retries :: Int , retries :: Int
} deriving (Show) } deriving (Show)
readConfig :: FilePath -> IO Config
readConfig = fmap fromRawConfig . readRawConfig
readRawConfig :: FilePath -> IO RawConfig
readRawConfig f = do
x <- BL.readFile f
case decode1 x of
Left (pos, e) -> error $ prettyPosWithSource pos x e
Right y -> pure y
fromRawConfig :: RawConfig -> Config fromRawConfig :: RawConfig -> Config
fromRawConfig (r@RawConfig { rawLanguages, rawDefaultLanguage }) = fromRawConfig r =
Config Config
{ languages = map f rawLanguages { languages = map (fromRawLanguage $ rawDefaultLanguage r) $ rawLanguages r
, buildConcurrently = rawBuildConcurrently r , buildConcurrently = rawBuildConcurrently r
, prepareContainers = rawPrepareContainers r , prepareContainers = rawPrepareContainers r
, cleanupInterval = rawCleanupInterval r , cleanupInterval = rawCleanupInterval r
, port = rawPort r , port = rawPort r
} }
where
f :: RawLanguage -> Language fromRawLanguage :: DefaultLanguage -> RawLanguage -> Language
f l = fromRawLanguage d l =
Language Language
{ name = rawName l { name = rawName l
, memory = fromMaybe (defMemory rawDefaultLanguage) (rawMemory l) , memory = fromMaybe (defMemory d) (rawMemory l)
, cpus = fromMaybe (defCpus rawDefaultLanguage) (rawCpus l) , cpus = fromMaybe (defCpus d) (rawCpus l)
, timeout = fromMaybe (defTimeout rawDefaultLanguage) (rawTimeout l) , timeout = fromMaybe (defTimeout d) (rawTimeout l)
, concurrent = fromMaybe (defConcurrent rawDefaultLanguage) (rawConcurrent l) , concurrent = fromMaybe (defConcurrent d) (rawConcurrent l)
, retries = fromMaybe (defRetries rawDefaultLanguage) (rawRetries l) , retries = fromMaybe (defRetries d) (rawRetries l)
} }
data RawConfig = RawConfig data RawConfig = RawConfig
{ rawLanguages :: [RawLanguage] { rawLanguages :: [RawLanguage]
@ -101,13 +111,3 @@ instance FromYAML RawLanguage where
<*> m .:? "timeout" <*> m .:? "timeout"
<*> m .:? "concurrent" <*> m .:? "concurrent"
<*> m .:? "retries" <*> m .:? "retries"
readConfig :: FilePath -> IO Config
readConfig = fmap fromRawConfig . readRawConfig
readRawConfig :: FilePath -> IO RawConfig
readRawConfig f = do
x <- BL.readFile f
case decode1 x of
Left (pos, e) -> error $ prettyPosWithSource pos x e
Right y -> pure y

View file

@ -27,7 +27,7 @@ import Control.Monad.Writer
import qualified Data.ByteString.Lazy as BL import qualified Data.ByteString.Lazy as BL
import qualified Data.Map.Strict as M import qualified Data.Map.Strict as M
import Data.Snowflake import Data.Snowflake
import qualified Data.Text as T import qualified Data.Text as T
import Control.Concurrent.MVar.Lifted import Control.Concurrent.MVar.Lifted

View file

@ -12,8 +12,8 @@ module Myriad.Docker
import Control.Monad.Reader import Control.Monad.Reader
import qualified Data.Map.Strict as M import qualified Data.Map.Strict as M
import Data.Snowflake import Data.Snowflake
import Data.String.Conversions import Data.String.Conversions
import Control.Concurrent.Async.Lifted import Control.Concurrent.Async.Lifted
import Control.Concurrent.Lifted (fork, threadDelay) import Control.Concurrent.Lifted (fork, threadDelay)
@ -23,8 +23,8 @@ import Control.Exception.Lifted
import System.FilePath ((</>)) import System.FilePath ((</>))
import System.Process.Typed import System.Process.Typed
import Myriad.Core
import Myriad.Config import Myriad.Config
import Myriad.Core
type Myriad = MyriadT IO type Myriad = MyriadT IO

View file

@ -7,25 +7,31 @@ module Myriad.Server
import Control.Monad.Except import Control.Monad.Except
import Control.Monad.Reader import Control.Monad.Reader
import Data.Aeson import Data.Aeson
import Data.List (find) import Data.List (find)
import qualified Data.Map as M import qualified Data.Map as M
import Data.String.Conversions import Data.String.Conversions
import qualified Data.Text as T import qualified Data.Text as T
import GHC.Generics import GHC.Generics
import Control.Concurrent.Async.Lifted import Control.Concurrent.Async.Lifted
import Control.Concurrent.MVar.Lifted import Control.Concurrent.MVar.Lifted
import Servant import Servant
import Myriad.Core
import Myriad.Config import Myriad.Config
import Myriad.Core
import Myriad.Docker import Myriad.Docker
type Myriad = MyriadT Handler type Myriad = MyriadT Handler
data EvalRequest = EvalRequest { language :: T.Text, code :: String } deriving (Generic, FromJSON) data EvalRequest = EvalRequest
data EvalResponse = EvalResponse { result :: T.Text } deriving (Generic, ToJSON) { language :: T.Text
, code :: String
} deriving (Generic, FromJSON)
data EvalResponse = EvalResponse
{ result :: T.Text
} deriving (Generic, ToJSON)
type API type API
= "languages" :> Get '[JSON] [T.Text] = "languages" :> Get '[JSON] [T.Text]