Commit ab0a8838 authored by Bas Lijnse's avatar Bas Lijnse

Merge branch 'embedaslibrary' into 'master'

Support for running with options for usage in a library

To support running as a library. I am using it to embed iTasks in Android. I want to be able to set options without using the command line because it is not available. A other reason can be that when you embed iTasks in another project you want custom command line arguments for example --iTasks-port=8080.

See merge request !1
parents a046438e 65e528e5
...@@ -25,6 +25,17 @@ RELATIVE_LOCATIONS :== [".": take 5 (iterate ((</>) "..") "..")] ...@@ -25,6 +25,17 @@ RELATIVE_LOCATIONS :== [".": take 5 (iterate ((</>) "..") "..")]
, task :: TaskWrapper , task :: TaskWrapper
, defaultFormat :: ServiceFormat , defaultFormat :: ServiceFormat
} }
:: ServerOptions =
{ appName :: String
, appPath :: FilePath
, sdkPath :: Maybe FilePath
, serverPort :: Int
, keepalive :: Int
, webDirPaths :: Maybe [FilePath]
, storeOpt :: Maybe FilePath
, saplOpt :: Maybe FilePath
}
:: TaskWrapper = E.a: TaskWrapper (HTTPRequest -> Task a) & iTask a :: TaskWrapper = E.a: TaskWrapper (HTTPRequest -> Task a) & iTask a
...@@ -53,6 +64,16 @@ RELATIVE_LOCATIONS :== [".": take 5 (iterate ((</>) "..") "..")] ...@@ -53,6 +64,16 @@ RELATIVE_LOCATIONS :== [".": take 5 (iterate ((</>) "..") "..")]
*/ */
startEngine :: a !*World -> *World | Publishable a startEngine :: a !*World -> *World | Publishable a
/**
* Starts the task engine with options and a list of published task definitions.
*
* @param Tasks to start
* @param Options to use like port and server paths.
* @param The world
* @return The world
*/
startEngineWithOptions :: a ServerOptions !*World -> *World | Publishable a
/** /**
* Wraps a task together with a url to make it publishable by the engine * Wraps a task together with a url to make it publishable by the engine
*/ */
......
...@@ -46,10 +46,18 @@ from Sapl.Linker.LazyLinker import generateLoaderState, :: LoaderStateExt ...@@ -46,10 +46,18 @@ from Sapl.Linker.LazyLinker import generateLoaderState, :: LoaderStateExt
from Sapl.Linker.SaplLinkerShared import :: SkipSet from Sapl.Linker.SaplLinkerShared import :: SkipSet
from Sapl.Target.Flavour import :: Flavour, toFlavour from Sapl.Target.Flavour import :: Flavour, toFlavour
show :: ![String] !*World -> *World
show lines world
# (console,world) = stdio world
# console = seqSt (\s c -> fwrites (s +++ "\n") c) lines console
# (_,world) = fclose console world
= world
startEngine :: a !*World -> *World | Publishable a startEngine :: a !*World -> *World | Publishable a
startEngine publishable world startEngine publishable world
# (opts,world) = getCommandLine world # (opts,world) = getCommandLine world
# (app,world) = determineAppName world # (app,world) = determineAppName world
# (appPath,world) = determineAppPath world
# (mbSDKPath,world) = determineSDKPath SEARCH_PATHS world # (mbSDKPath,world) = determineSDKPath SEARCH_PATHS world
// Show server name // Show server name
# world = show (infoline app) world # world = show (infoline app) world
...@@ -66,20 +74,18 @@ startEngine publishable world ...@@ -66,20 +74,18 @@ startEngine publishable world
| help = show instructions world | help = show instructions world
//Check sdkpath //Check sdkpath
# mbSDKPath = maybe mbSDKPath Just sdkOpt //Commandline SDK option overrides found paths # mbSDKPath = maybe mbSDKPath Just sdkOpt //Commandline SDK option overrides found paths
//Normal execution # options =
# world = show (running port) world { appName = app
# iworld = initIWorld mbSDKPath webDirPaths storeOpt saplOpt world , appPath = appPath
//Reset connectedTo for all task instances , sdkPath = mbSDKPath
# iworld = clearConnections iworld , serverPort = port
// mark all instance as outdated initially , keepalive = keepalive
# iworld = queueAllPersistent iworld , webDirPaths = webDirPaths
//Start task server , storeOpt = storeOpt
# iworld = serve port (httpServer port keepalive (engine publishable) taskInstanceUIs) [BackgroundTask removeOutdatedSessions,BackgroundTask updateClocks, BackgroundTask (processEvents MAX_EVENTS)] timeout iworld , saplOpt = saplOpt
= finalizeIWorld iworld }
= startEngineWithOptions publishable options world
where where
infoline :: !String -> [String]
infoline app = ["*** " +++ app +++ " HTTP server ***",""]
instructions :: [String] instructions :: [String]
instructions = instructions =
["Available commandline options:" ["Available commandline options:"
...@@ -93,16 +99,9 @@ where ...@@ -93,16 +99,9 @@ where
,"" ,""
] ]
running :: !Int -> [String] infoline :: !String -> [String]
running port = ["Running at http://localhost" +++ (if (port == 80) "/" (":" +++ toString port +++ "/"))] infoline app = ["*** " +++ app +++ " HTTP server ***",""]
show :: ![String] !*World -> *World
show lines world
# (console,world) = stdio world
# console = seqSt (\s c -> fwrites (s +++ "\n") c) lines console
# (_,world) = fclose console world
= world
boolOpt :: !String ![String] -> Bool boolOpt :: !String ![String] -> Bool
boolOpt key opts = isMember key opts boolOpt key opts = isMember key opts
...@@ -121,7 +120,23 @@ where ...@@ -121,7 +120,23 @@ where
stringOpt key [n,v:r] stringOpt key [n,v:r]
| n == key = Just v | n == key = Just v
= stringOpt key [v:r] = stringOpt key [v:r]
startEngineWithOptions :: a ServerOptions !*World -> *World | Publishable a
startEngineWithOptions publishable options world
# port = options.serverPort
# world = show (running port) world
# iworld = initIWorld options world
//Reset connectedTo for all task instances
# iworld = clearConnections iworld
// mark all instance as outdated initially
# iworld = queueAllPersistent iworld
//Start task server
# iworld = serve port (httpServer port options.keepalive (engine publishable) taskInstanceUIs) [BackgroundTask removeOutdatedSessions,BackgroundTask updateClocks, BackgroundTask (processEvents MAX_EVENTS)] timeout iworld
= finalizeIWorld iworld
where
running :: !Int -> [String]
running port = ["Running at http://localhost" +++ (if (port == 80) "/" (":" +++ toString port +++ "/"))]
timeout :: !*IWorld -> (!Maybe Timeout,!*IWorld) timeout :: !*IWorld -> (!Maybe Timeout,!*IWorld)
timeout iworld = case 'SDS'.read taskEvents iworld of //Check if there are events in the queue timeout iworld = case 'SDS'.read taskEvents iworld of //Check if there are events in the queue
(Ok (Queue [] []),iworld) = (Just 100,iworld) //Empty queue, don't waste CPU, but refresh (Ok (Queue [] []),iworld) = (Just 100,iworld) //Empty queue, don't waste CPU, but refresh
...@@ -176,29 +191,29 @@ where ...@@ -176,29 +191,29 @@ where
defaultHandlers = [sdsService, simpleHTTPResponse (const True, handleStaticResourceRequest)] defaultHandlers = [sdsService, simpleHTTPResponse (const True, handleStaticResourceRequest)]
initIWorld :: !(Maybe FilePath) !(Maybe [FilePath]) !(Maybe FilePath) !(Maybe FilePath) !*World -> *IWorld initIWorld :: ServerOptions !*World -> *IWorld
initIWorld mbSDKPath mbWebdirPaths mbStorePath mbSaplPath world initIWorld options world
# (appName,world) = determineAppName world # appName = options.appName
# (appPath,world) = determineAppPath world # appPath = options.appPath
# appDir = takeDirectory appPath # appDir = takeDirectory appPath
# dataDir = case mbStorePath of # dataDir = case options.storeOpt of
Just path = path Just path = path
Nothing = appDir </> appName +++ "-data" Nothing = appDir </> appName +++ "-data"
# (webdirPaths,world) = case mbWebdirPaths of # (webdirPaths,world) = case options.webDirPaths of
Just paths = (paths,world) Just paths = (paths,world)
Nothing Nothing
# appWebDirs = [appDir </> "WebPublic"] # appWebDirs = [appDir </> "WebPublic"]
= case mbSDKPath of = case options.sdkPath of
Just sdkDir //Scan extensions for public web files Just sdkDir //Scan extensions for public web files
# (libWebDirs,world) = determineWebPublicDirs (sdkDir </>"Server"</>"iTasks"</>"API"</>"Extensions") world # (libWebDirs,world) = determineWebPublicDirs (sdkDir </>"Server"</>"iTasks"</>"API"</>"Extensions") world
= ([sdkDir</>"Client"] ++ appWebDirs ++ libWebDirs,world) = ([sdkDir</>"Client"] ++ appWebDirs ++ libWebDirs,world)
Nothing Nothing
= (appWebDirs,world) = (appWebDirs,world)
# (customCSS,world) = checkCustomCSS appName webdirPaths world # (customCSS,world) = checkCustomCSS appName webdirPaths world
# saplPath = case mbSaplPath of # saplPath = case options.saplOpt of
Just path = path Just path = path
Nothing = appDir</>"sapl" Nothing = appDir</>"sapl"
# flavourPath = case mbSDKPath of # flavourPath = case options.sdkPath of
Just sdkPath = sdkPath </> "Dependencies" </> "clean-sapl" </> "src" </>"clean.f" Just sdkPath = sdkPath </> "Dependencies" </> "clean-sapl" </> "src" </>"clean.f"
Nothing = saplPath </> "clean.f" Nothing = saplPath </> "clean.f"
# (res,world) = getFileInfo appPath world # (res,world) = getFileInfo appPath world
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment