-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(Main): simplify body of
main
by using ExceptT
transformer
- Loading branch information
1 parent
163876f
commit 5ff4ecc
Showing
1 changed file
with
26 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,42 @@ | ||
module Main where | ||
|
||
import Control.Monad.Except.Trans (ExceptT(..), runExceptT, withExceptT) | ||
import Data.Either (Either(..)) | ||
import Effect (Effect) | ||
import HTTP.Server as HTTPServer | ||
import HTTP.Utils as HTTPUtils | ||
import Logger as Logger | ||
import Orchestrator.FS (readFile) | ||
import Orchestrator.JSON (fromJSON) | ||
import Orchestrator.Main (makeId, makeSecret, runDefinitionWithIdAndSecret) | ||
import Orchestrator.Main (Definitions, makeId, makeSecret, runDefinitionWithIdAndSecret) | ||
import Prelude | ||
import System.Environment (readEnvInt) | ||
|
||
readConfiguration :: ExceptT String Effect String | ||
readConfiguration = | ||
withExceptT | ||
(const "Failure reading configuration.json") | ||
(ExceptT $ readFile "configuration.json") | ||
|
||
parseConfiguration :: String -> ExceptT String Effect Definitions | ||
parseConfiguration string = | ||
withExceptT | ||
(\lines -> "Configuration file is not structured properly" <> "\n" <> lines) | ||
(ExceptT $ pure $ fromJSON string) | ||
|
||
getConfiguration :: Effect (Either String Definitions) | ||
getConfiguration = runExceptT $ readConfiguration >>= parseConfiguration | ||
|
||
main :: Effect Unit | ||
main = do | ||
port <- readEnvInt "PORT" 8181 | ||
configuration <- readFile "configuration.json" | ||
configuration <- getConfiguration | ||
case configuration of | ||
(Left _) -> Logger.error "Failure reading configuration.json" | ||
(Right contents) -> do | ||
parsed <- pure $ fromJSON contents | ||
case parsed of | ||
(Left error) -> do | ||
Logger.error "Configuration file is not structured properly" | ||
Logger.quote error | ||
(Right definitions) -> do | ||
HTTPServer.startSync port $ \route -> do | ||
Logger.info $ "Incoming request " <> route | ||
when (HTTPUtils.pathname route == "/run") do | ||
runDefinitionWithIdAndSecret (makeId (HTTPUtils.param "definition" route)) (makeSecret (HTTPUtils.param "secret" route)) definitions | ||
Logger.log $ "Orchy server is running at http:https://localhost:" <> (show port) | ||
Logger.line | ||
(Left msg) -> Logger.error msg | ||
(Right definitions) -> do | ||
HTTPServer.startSync port $ \route -> do | ||
Logger.info $ "Incoming request " <> route | ||
when (HTTPUtils.pathname route == "/run") do | ||
runDefinitionWithIdAndSecret (makeId (HTTPUtils.param "definition" route)) (makeSecret (HTTPUtils.param "secret" route)) definitions | ||
Logger.log $ "Orchy server is running at http:https://localhost:" <> (show port) | ||
Logger.line |