2012-07-31 16:17:31 +00:00
|
|
|
{- git-annex assistant webapp thread
|
2012-07-26 01:26:13 +00:00
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2012-07-29 12:52:57 +00:00
|
|
|
{-# LANGUAGE TypeFamilies, QuasiQuotes, MultiParamTypeClasses, TemplateHaskell, OverloadedStrings, RankNTypes #-}
|
2012-07-29 01:21:22 +00:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
2012-07-26 01:26:13 +00:00
|
|
|
|
|
|
|
module Assistant.Threads.WebApp where
|
|
|
|
|
|
|
|
import Assistant.Common
|
2012-07-31 05:11:32 +00:00
|
|
|
import Assistant.WebApp
|
2012-09-02 04:27:48 +00:00
|
|
|
import Assistant.WebApp.Types
|
2012-07-31 05:11:32 +00:00
|
|
|
import Assistant.WebApp.DashBoard
|
|
|
|
import Assistant.WebApp.SideBar
|
|
|
|
import Assistant.WebApp.Notifications
|
|
|
|
import Assistant.WebApp.Configurators
|
2012-08-31 19:17:12 +00:00
|
|
|
import Assistant.WebApp.Configurators.Local
|
|
|
|
import Assistant.WebApp.Configurators.Ssh
|
2012-09-08 04:26:47 +00:00
|
|
|
import Assistant.WebApp.Configurators.Pairing
|
2012-07-31 06:30:26 +00:00
|
|
|
import Assistant.WebApp.Documentation
|
2012-07-26 03:13:01 +00:00
|
|
|
import Assistant.ThreadedMonad
|
2012-07-26 01:26:13 +00:00
|
|
|
import Assistant.DaemonStatus
|
2012-08-05 01:18:57 +00:00
|
|
|
import Assistant.ScanRemotes
|
2012-07-27 15:47:34 +00:00
|
|
|
import Assistant.TransferQueue
|
2012-08-12 16:11:20 +00:00
|
|
|
import Assistant.TransferSlots
|
2012-07-26 01:26:13 +00:00
|
|
|
import Utility.WebApp
|
2012-07-26 07:38:20 +00:00
|
|
|
import Utility.FileMode
|
|
|
|
import Utility.TempFile
|
2012-07-26 06:45:01 +00:00
|
|
|
import Git
|
2012-07-26 01:26:13 +00:00
|
|
|
|
|
|
|
import Yesod
|
2012-07-26 06:45:01 +00:00
|
|
|
import Yesod.Static
|
2012-07-26 03:13:01 +00:00
|
|
|
import Network.Socket (PortNumber)
|
2012-07-31 05:11:32 +00:00
|
|
|
import Data.Text (pack, unpack)
|
2012-07-26 08:50:09 +00:00
|
|
|
|
|
|
|
thisThread :: String
|
|
|
|
thisThread = "WebApp"
|
2012-07-26 01:26:13 +00:00
|
|
|
|
2012-07-31 05:11:32 +00:00
|
|
|
mkYesodDispatch "WebApp" $(parseRoutesFile "Assistant/WebApp/routes")
|
2012-07-26 01:26:13 +00:00
|
|
|
|
2012-08-01 20:10:26 +00:00
|
|
|
type Url = String
|
|
|
|
|
|
|
|
webAppThread
|
2012-09-13 04:57:52 +00:00
|
|
|
:: Maybe ThreadState
|
2012-08-01 20:10:26 +00:00
|
|
|
-> DaemonStatusHandle
|
2012-08-05 01:18:57 +00:00
|
|
|
-> ScanRemoteMap
|
2012-08-01 20:10:26 +00:00
|
|
|
-> TransferQueue
|
2012-08-12 16:11:20 +00:00
|
|
|
-> TransferSlots
|
2012-09-08 23:57:15 +00:00
|
|
|
-> UrlRenderer
|
2012-08-01 20:10:26 +00:00
|
|
|
-> Maybe (IO String)
|
|
|
|
-> Maybe (Url -> FilePath -> IO ())
|
2012-09-06 18:56:04 +00:00
|
|
|
-> NamedThread
|
2012-09-08 23:57:15 +00:00
|
|
|
webAppThread mst dstatus scanremotes transferqueue transferslots urlrenderer postfirstrun onstartup = thread $ do
|
2012-07-31 16:17:31 +00:00
|
|
|
webapp <- WebApp
|
|
|
|
<$> pure mst
|
|
|
|
<*> pure dstatus
|
2012-08-05 01:18:57 +00:00
|
|
|
<*> pure scanremotes
|
2012-07-31 16:17:31 +00:00
|
|
|
<*> pure transferqueue
|
2012-08-12 16:11:20 +00:00
|
|
|
<*> pure transferslots
|
2012-07-31 16:17:31 +00:00
|
|
|
<*> (pack <$> genRandomToken)
|
|
|
|
<*> getreldir mst
|
|
|
|
<*> pure $(embed "static")
|
|
|
|
<*> newWebAppState
|
2012-08-01 20:10:26 +00:00
|
|
|
<*> pure postfirstrun
|
2012-09-08 23:57:15 +00:00
|
|
|
setUrlRenderer urlrenderer $ yesodRender webapp (pack "")
|
2012-07-27 03:55:51 +00:00
|
|
|
app <- toWaiAppPlain webapp
|
2012-07-26 01:26:13 +00:00
|
|
|
app' <- ifM debugEnabled
|
|
|
|
( return $ httpDebugLogger app
|
|
|
|
, return app
|
|
|
|
)
|
2012-09-13 04:57:52 +00:00
|
|
|
runWebApp app' $ \port -> case mst of
|
|
|
|
Nothing -> withTempFile "webapp.html" $ \tmpfile _ -> go port webapp tmpfile
|
|
|
|
Just st -> go port webapp =<< runThreadState st (fromRepo gitAnnexHtmlShim)
|
2012-07-27 15:47:34 +00:00
|
|
|
where
|
2012-09-06 18:56:04 +00:00
|
|
|
thread = NamedThread thisThread
|
2012-07-31 16:17:31 +00:00
|
|
|
getreldir Nothing = return Nothing
|
2012-08-02 11:47:50 +00:00
|
|
|
getreldir (Just st) = Just <$>
|
|
|
|
(relHome =<< absPath
|
|
|
|
=<< runThreadState st (fromRepo repoPath))
|
2012-07-31 16:17:31 +00:00
|
|
|
go port webapp htmlshim = do
|
|
|
|
writeHtmlShim webapp port htmlshim
|
2012-09-09 00:03:06 +00:00
|
|
|
maybe noop (\a -> a (myUrl webapp port HomeR) htmlshim) onstartup
|
2012-07-26 03:13:01 +00:00
|
|
|
|
2012-07-26 08:50:09 +00:00
|
|
|
{- Creates a html shim file that's used to redirect into the webapp,
|
|
|
|
- to avoid exposing the secretToken when launching the web browser. -}
|
2012-07-31 16:17:31 +00:00
|
|
|
writeHtmlShim :: WebApp -> PortNumber -> FilePath -> IO ()
|
|
|
|
writeHtmlShim webapp port file = do
|
|
|
|
debug thisThread ["running on port", show port]
|
|
|
|
viaTmp go file $ genHtmlShim webapp port
|
2012-07-26 07:38:20 +00:00
|
|
|
where
|
2012-07-31 16:17:31 +00:00
|
|
|
go tmpfile content = do
|
|
|
|
h <- openFile tmpfile WriteMode
|
|
|
|
modifyFileMode tmpfile $ removeModes [groupReadMode, otherReadMode]
|
2012-07-26 07:38:20 +00:00
|
|
|
hPutStr h content
|
|
|
|
hClose h
|
2012-07-26 03:13:01 +00:00
|
|
|
|
|
|
|
{- TODO: generate this static file using Yesod. -}
|
2012-07-26 07:38:20 +00:00
|
|
|
genHtmlShim :: WebApp -> PortNumber -> String
|
2012-08-02 01:26:36 +00:00
|
|
|
genHtmlShim webapp port = unlines
|
|
|
|
[ "<html>"
|
|
|
|
, "<head>"
|
|
|
|
, "<title>Starting webapp...</title>"
|
|
|
|
, "<meta http-equiv=\"refresh\" content=\"0; URL="++url++"\">"
|
|
|
|
, "<body>"
|
|
|
|
, "<p>"
|
|
|
|
, "<a href=\"" ++ url ++ "\">Starting webapp...</a>"
|
|
|
|
, "</p>"
|
|
|
|
, "</body>"
|
|
|
|
, "</html>"
|
|
|
|
]
|
2012-07-26 01:26:13 +00:00
|
|
|
where
|
2012-09-09 00:03:06 +00:00
|
|
|
url = myUrl webapp port HomeR
|
2012-08-01 20:10:26 +00:00
|
|
|
|
2012-09-09 00:03:06 +00:00
|
|
|
myUrl :: WebApp -> PortNumber -> Route WebApp -> Url
|
|
|
|
myUrl webapp port route = unpack $ yesodRender webapp urlbase route []
|
|
|
|
where
|
|
|
|
urlbase = pack $ "http://localhost:" ++ show port
|