module Ideas.Main.Default
( defaultMain, defaultMainWith, defaultCGI
, serviceList, metaServiceList, Service
, module Ideas.Service.DomainReasoner
) where
import Control.Exception
import Control.Monad
import Data.ByteString (ByteString, unpack)
import Data.Char
import Data.Maybe
import Data.Semigroup ((<>))
import Data.String
import Ideas.Encoding.ModeJSON (processJSON)
import Ideas.Encoding.ModeXML (processXML)
import Ideas.Encoding.Options (Options, maxTime, optionCgiBin)
import Ideas.Encoding.Request
import Ideas.Main.CmdLineOptions hiding (fullVersion)
import Ideas.Service.DomainReasoner
import Ideas.Service.FeedbackScript.Analysis
import Ideas.Service.ServiceList
import Ideas.Service.Types (Service)
import Ideas.Utils.BlackBoxTests
import Ideas.Utils.Prelude
import Ideas.Utils.TestSuite
import Network.HTTP.Types
import Network.Wai hiding (Request)
import System.IO
import qualified Ideas.Encoding.Logging as Log
import qualified Ideas.Main.CGI as CGI
import qualified Ideas.Main.CmdLineOptions as Options
import qualified Network.Wai as CGI
defaultMain :: DomainReasoner -> IO ()
defaultMain = defaultMainWith mempty
defaultMainWith :: Options -> DomainReasoner -> IO ()
defaultMainWith options dr = do
cmdLineOptions <- getCmdLineOptions
if null cmdLineOptions
then defaultCGI options dr
else defaultCommandLine options (addVersion dr) cmdLineOptions
defaultCGI :: Options -> DomainReasoner -> IO ()
defaultCGI options dr = CGI.run $ \req respond -> do
logRef <- Log.newLogRef
let script = fromMaybe "" (findHeader "CGI-Script-Name" req)
addr = fromMaybe "" (findHeader "REMOTE_ADDR" req)
input <- inputOrDefault req
(preq, txt, ctp) <-
process (options <> optionCgiBin script) dr logRef input
when (useLogging preq) $ do
Log.changeLog logRef $ \r -> Log.addRequest preq r
{ Log.ipaddress = addr
, Log.version = shortVersion
, Log.input = input
, Log.output = txt
}
Log.logRecord (getSchema preq) logRef
respond $ responseLBS
status200
[ (fromString "Content-Type", fromString ctp)
, (fromString "Access-Control-Allow-Origin", fromString "*")
]
(fromString txt)
inputOrDefault :: CGI.Request -> IO String
inputOrDefault req = do
maybeInput <- inputFromRequest req
case maybeInput of
Just s -> return s
Nothing
| acceptsHTML -> return defaultBrowser
| otherwise -> fail "environment variable 'input' is empty"
where
defaultBrowser :: String
defaultBrowser = "<request service='index' encoding='html'/>"
acceptsHTML :: Bool
acceptsHTML = "text/html" `elem` accepts req
defaultCommandLine :: Options -> DomainReasoner -> [CmdLineOption] -> IO ()
defaultCommandLine options dr cmdLineOptions = do
hSetBinaryMode stdout True
mapM_ doAction cmdLineOptions
where
doAction cmdLineOption =
case cmdLineOption of
Version -> putStrLn ("IDEAS, " ++ versionText)
Help -> putStrLn helpText
InputFile file ->
withBinaryFile file ReadMode $ \h -> do
logRef <- Log.newLogRef
input <- hGetContents h
(req, txt, _) <- process options dr logRef input
putStrLn txt
when (PrintLog `elem` cmdLineOptions) $ do
Log.changeLog logRef $ \r -> Log.addRequest req r
{ Log.ipaddress = "command-line"
, Log.version = shortVersion
, Log.input = input
, Log.output = txt
}
Log.printLog logRef
Test dir -> do
tests <- blackBoxTests (makeTestRunner dr) ["xml", "json"] dir
result <- runTestSuiteResult True tests
printSummary result
MakeScriptFor s -> makeScriptFor dr s
AnalyzeScript file -> parseAndAnalyzeScript dr file
PrintLog -> return ()
process :: Options -> DomainReasoner -> Log.LogRef -> String -> IO (Request, String, String)
process options dr logRef input = do
format <- discoverDataFormat input
run format options {maxTime = Just 5} (addVersion dr) logRef input
`catch` \e -> do
let msg = "Error: " ++ show (e :: SomeException)
Log.changeLog logRef (\r -> r { Log.errormsg = msg })
return (mempty, msg, "text/plain")
where
run XML = processXML
run JSON = processJSON
makeTestRunner :: DomainReasoner -> String -> IO String
makeTestRunner dr input = do
(_, out, _) <- process mempty dr Log.noLogRef input
return out
addVersion :: DomainReasoner -> DomainReasoner
addVersion dr = dr
{ version = update version Options.shortVersion
, fullVersion = update fullVersion Options.fullVersion
}
where
update f s = if null (f dr) then s else f dr
findHeader :: String -> CGI.Request -> Maybe String
findHeader s = fmap fromByteString . lookup (fromString s) . requestHeaders
inputFromRequest :: CGI.Request -> IO (Maybe String)
inputFromRequest req =
case inputFromQuery (queryString req) of
Just s -> return (Just s)
Nothing -> do
body <- requestBody req
return (inputFromQuery (parseQuery body))
inputFromQuery :: Query -> Maybe String
inputFromQuery = fmap fromByteString . join . lookup (fromString "input")
accepts :: CGI.Request -> [String]
accepts = maybe [] (splitsWithElem ',') . findHeader "Accept"
fromByteString :: ByteString -> String
fromByteString = map (chr . fromEnum) . unpack