module Main where import Control.Concurrent(forkIO) import System.Environment (getArgs) import System.Console.GetOpt import Data.Maybe ( fromMaybe ) import System.FilePath import System.Directory import QuicQuid.Broker import QuicQuid.BrokerCoreNaive import QuicQuid.HTTPApi import QuicQuid.Agent.And import QuicQuid.Agent.Curl import QuicQuid.Agent.Random import System.IO import QuicQuid.Log import qualified Control.Exception as E import Control.Monad.Error -- |An 'all-in-one' executable that includes the router, the HTTP api and all the deployed agents. main = do let progName = "agent.http" Options {optPort=port,optWebDir=Just webDir} <- parseOptions appDir <- getAppUserDataDirectory progName createDirectoryIfMissing False appDir -- Setup log let logFile = appDir "app.log" -- print logFile E.catch (removeFile logFile) (\e -> return ()) -- h <- verboseStreamHandler stderr DEBUG h <- fileHandler logFile DEBUG updateGlobalLogger rootLoggerName (setHandlers [h]) updateGlobalLogger rootLoggerName (setLevel DEBUG) updateGlobalLogger "Router.readCh" (setLevel WARNING) warningM "Main" $ "Starting " ++ progName k <- core mapM_ forkIO [broker k,andAgent,curlAgent,randomAgent] -- Starts the httpAgent, this never quits. startHTTP port webDir data Options = Options { optPort :: Int , optWebDir :: Maybe String } deriving Show defaultOptions = Options {optPort=80,optWebDir=Nothing} options = [ Option [] ["port","http-port"] (ReqArg (\p opts -> opts {optPort=read p}) "PORT") "HTTP port. If unspecified, defaults to 80." ,Option [] ["webDir"] (ReqArg (\d opts -> opts {optWebDir=Just d}) "DIR") "The Web Directory. Should contain one directory for every domain to serve of the format: /web/." ] parseOptions = do argv <- getArgs case getOpt Permute options argv of (o,[],[]) -> return $ foldl (flip id) defaultOptions o (_,_,errs) -> ioError (userError (concat errs ++ usageInfo header options)) where header = "Usage: agent.http [OPTIONS]"