2012-07-26 03:13:01 +00:00
|
|
|
{- git-annex webapp launcher
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.WebApp where
|
|
|
|
|
|
|
|
import Common.Annex
|
|
|
|
import Command
|
|
|
|
import Assistant
|
2012-07-31 16:17:31 +00:00
|
|
|
import Assistant.DaemonStatus
|
2012-08-05 01:18:57 +00:00
|
|
|
import Assistant.ScanRemotes
|
2012-07-31 16:17:31 +00:00
|
|
|
import Assistant.TransferQueue
|
|
|
|
import Assistant.Threads.WebApp
|
2012-07-26 03:13:01 +00:00
|
|
|
import Utility.WebApp
|
2012-08-01 20:29:38 +00:00
|
|
|
import Utility.Daemon (checkDaemon, lockPidFile)
|
2012-07-31 20:19:24 +00:00
|
|
|
import Init
|
2012-08-01 20:10:26 +00:00
|
|
|
import qualified Git.CurrentRepo
|
|
|
|
import qualified Annex
|
2012-08-02 04:42:33 +00:00
|
|
|
import Locations.UserConfig
|
2012-07-26 03:13:01 +00:00
|
|
|
|
2012-08-02 04:42:33 +00:00
|
|
|
import System.Posix.Directory
|
2012-08-01 20:10:26 +00:00
|
|
|
import Control.Concurrent
|
2012-07-31 16:17:31 +00:00
|
|
|
import Control.Concurrent.STM
|
|
|
|
|
2012-07-26 03:13:01 +00:00
|
|
|
def :: [Command]
|
2012-08-02 04:42:33 +00:00
|
|
|
def = [oneShot $ noRepo startNoRepo $ dontCheck repoExists $
|
2012-07-26 16:17:28 +00:00
|
|
|
command "webapp" paramNothing seek "launch webapp"]
|
|
|
|
|
2012-07-26 03:13:01 +00:00
|
|
|
seek :: [CommandSeek]
|
2012-08-01 20:40:38 +00:00
|
|
|
seek = [withNothing start]
|
2012-07-26 03:13:01 +00:00
|
|
|
|
2012-08-01 20:40:38 +00:00
|
|
|
start :: CommandStart
|
|
|
|
start = notBareRepo $ do
|
2012-08-02 04:42:33 +00:00
|
|
|
ifM (isInitialized) ( go , liftIO startNoRepo )
|
2012-07-31 20:19:24 +00:00
|
|
|
stop
|
|
|
|
where
|
|
|
|
go = do
|
2012-07-27 19:40:52 +00:00
|
|
|
f <- liftIO . absPath =<< fromRepo gitAnnexHtmlShim
|
|
|
|
ifM (checkpid <&&> checkshim f) $
|
2012-07-31 16:17:31 +00:00
|
|
|
( liftIO $ openBrowser f
|
2012-08-01 20:34:17 +00:00
|
|
|
, startDaemon True True $ Just $
|
2012-08-01 20:10:26 +00:00
|
|
|
const openBrowser
|
2012-07-27 19:40:52 +00:00
|
|
|
)
|
2012-07-27 19:33:24 +00:00
|
|
|
checkpid = do
|
2012-07-26 03:13:01 +00:00
|
|
|
pidfile <- fromRepo gitAnnexPidFile
|
2012-07-27 19:33:24 +00:00
|
|
|
liftIO $ isJust <$> checkDaemon pidfile
|
|
|
|
checkshim f = liftIO $ doesFileExist f
|
2012-07-31 16:17:31 +00:00
|
|
|
|
2012-08-02 04:42:33 +00:00
|
|
|
{- When run without a repo, see if there is an autoStartFile,
|
|
|
|
- and if so, start the first available listed repository.
|
|
|
|
- If not, it's our first time being run! -}
|
|
|
|
startNoRepo :: IO ()
|
|
|
|
startNoRepo = do
|
|
|
|
autostartfile <- autoStartFile
|
|
|
|
ifM (doesFileExist autostartfile) ( autoStart autostartfile , firstRun )
|
2012-07-31 16:17:31 +00:00
|
|
|
|
2012-08-02 04:42:33 +00:00
|
|
|
autoStart :: FilePath -> IO ()
|
|
|
|
autoStart autostartfile = do
|
|
|
|
dirs <- lines <$> readFile autostartfile
|
|
|
|
edirs <- filterM doesDirectoryExist dirs
|
|
|
|
case edirs of
|
|
|
|
[] -> firstRun -- what else can I do? Nothing works..
|
|
|
|
(d:_) -> do
|
|
|
|
changeWorkingDirectory d
|
|
|
|
state <- Annex.new =<< Git.CurrentRepo.get
|
|
|
|
void $ Annex.eval state $ doCommand start
|
2012-08-01 20:10:26 +00:00
|
|
|
|
|
|
|
{- Run the webapp without a repository, which prompts the user, makes one,
|
|
|
|
- changes to it, starts the regular assistant, and redirects the
|
|
|
|
- browser to its url.
|
|
|
|
-
|
|
|
|
- This is a very tricky dance -- The first webapp calls the signaler,
|
|
|
|
- which signals the main thread when it's ok to continue by writing to a
|
|
|
|
- MVar. The main thread starts the second webapp, and uses its callback
|
|
|
|
- to write its url back to the MVar, from where the signaler retrieves it,
|
|
|
|
- returning it to the first webapp, which does the redirect.
|
|
|
|
-
|
|
|
|
- Note that it's important that mainthread never terminates! Much
|
|
|
|
- of this complication is due to needing to keep the mainthread running.
|
|
|
|
-}
|
2012-07-31 16:17:31 +00:00
|
|
|
firstRun :: IO ()
|
|
|
|
firstRun = do
|
|
|
|
dstatus <- atomically . newTMVar =<< newDaemonStatus
|
2012-08-05 01:18:57 +00:00
|
|
|
scanremotes <- newScanRemoteMap
|
2012-07-31 16:17:31 +00:00
|
|
|
transferqueue <- newTransferQueue
|
2012-08-01 20:10:26 +00:00
|
|
|
v <- newEmptyMVar
|
|
|
|
let callback a = Just $ a v
|
2012-08-05 01:18:57 +00:00
|
|
|
webAppThread Nothing dstatus scanremotes transferqueue
|
|
|
|
(callback signaler) (callback mainthread)
|
2012-08-01 20:10:26 +00:00
|
|
|
where
|
|
|
|
signaler v = do
|
|
|
|
putMVar v ""
|
2012-08-01 20:34:17 +00:00
|
|
|
takeMVar v
|
2012-08-01 20:10:26 +00:00
|
|
|
mainthread v _url htmlshim = do
|
|
|
|
openBrowser htmlshim
|
|
|
|
|
|
|
|
_wait <- takeMVar v
|
|
|
|
|
|
|
|
state <- Annex.new =<< Git.CurrentRepo.get
|
2012-08-01 20:29:38 +00:00
|
|
|
Annex.eval state $ do
|
|
|
|
dummydaemonize
|
2012-08-01 20:10:26 +00:00
|
|
|
startAssistant True id $ Just $ sendurlback v
|
|
|
|
sendurlback v url _htmlshim = putMVar v url
|
2012-08-01 20:29:38 +00:00
|
|
|
{- Set up the pid file in the new repo. -}
|
|
|
|
dummydaemonize = do
|
|
|
|
liftIO . lockPidFile =<< fromRepo gitAnnexPidFile
|
2012-08-02 04:42:33 +00:00
|
|
|
|
|
|
|
openBrowser :: FilePath -> IO ()
|
|
|
|
openBrowser htmlshim = unlessM (runBrowser url) $
|
|
|
|
error $ "failed to start web browser on url " ++ url
|
|
|
|
where
|
|
|
|
url = fileUrl htmlshim
|
|
|
|
|
|
|
|
fileUrl :: FilePath -> String
|
|
|
|
fileUrl file = "file://" ++ file
|