summaryrefslogtreecommitdiff
path: root/Command/WebApp.hs
blob: a3294911f9ad8918e436c90b1b937a5ef8a6ce34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
{- 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
import Assistant.DaemonStatus
import Assistant.TransferQueue
import Assistant.Threads.WebApp
import Utility.WebApp
import Utility.ThreadScheduler
import Utility.Daemon (checkDaemon)
import qualified Command.Watch

import Control.Concurrent.STM

def :: [Command]
def = [oneShot $ noRepo firstRun $
	withOptions [Command.Watch.foregroundOption, Command.Watch.stopOption] $
        command "webapp" paramNothing seek "launch webapp"]

seek :: [CommandSeek]
seek = [withFlag Command.Watch.stopOption $ \stopdaemon ->
	withFlag Command.Watch.foregroundOption $ \foreground ->
	withNothing $ start foreground stopdaemon]

start :: Bool -> Bool -> CommandStart
start foreground stopdaemon = notBareRepo $ do
	if stopdaemon
		then stopDaemon
		else do
			f <- liftIO . absPath =<< fromRepo gitAnnexHtmlShim
			ifM (checkpid <&&> checkshim f) $
				( liftIO $ openBrowser f 
				, startDaemon True foreground $ Just openBrowser
				)
	stop
	where
		checkpid = do
			pidfile <- fromRepo gitAnnexPidFile
			liftIO $ isJust <$> checkDaemon pidfile
		checkshim f = liftIO $ doesFileExist f

openBrowser :: FilePath -> IO ()
openBrowser htmlshim = unlessM (runBrowser url) $
	error $ "failed to start web browser on url " ++ url
	where
		url = "file://" ++ htmlshim

firstRun :: IO ()
firstRun = do
	dstatus <- atomically . newTMVar =<< newDaemonStatus
	transferqueue <- newTransferQueue
	webAppThread Nothing dstatus transferqueue $ Just $ \f -> do
		openBrowser f
		waitForTermination