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-11-25 18:36:24 +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-10-09 18:43:53 +00:00
|
|
|
import Assistant.WebApp.Configurators.Edit
|
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-11-24 20:30:15 +00:00
|
|
|
import Assistant.WebApp.Configurators.AWS
|
2012-11-17 19:30:11 +00:00
|
|
|
import Assistant.WebApp.Configurators.WebDAV
|
2012-10-26 18:17:09 +00:00
|
|
|
import Assistant.WebApp.Configurators.XMPP
|
2012-07-31 06:30:26 +00:00
|
|
|
import Assistant.WebApp.Documentation
|
2013-01-03 19:16:40 +00:00
|
|
|
import Assistant.WebApp.Control
|
2012-09-18 21:50:07 +00:00
|
|
|
import Assistant.WebApp.OtherRepos
|
2012-10-29 23:07:10 +00:00
|
|
|
import Assistant.Types.ThreadedMonad
|
2012-07-26 01:26:13 +00:00
|
|
|
import Utility.WebApp
|
2012-07-26 07:38:20 +00:00
|
|
|
import Utility.TempFile
|
2013-01-03 22:50:30 +00:00
|
|
|
import Utility.FileMode
|
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
|
2013-01-10 03:17:52 +00:00
|
|
|
import Network.Socket (SockAddr)
|
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
|
|
|
|
|
2012-10-29 04:15:43 +00:00
|
|
|
webAppThread
|
|
|
|
:: AssistantData
|
2012-09-08 23:57:15 +00:00
|
|
|
-> UrlRenderer
|
2012-10-29 04:15:43 +00:00
|
|
|
-> Bool
|
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-10-29 06:21:04 +00:00
|
|
|
webAppThread assistantdata urlrenderer noannex postfirstrun onstartup = thread $ liftIO $ do
|
2012-07-31 16:17:31 +00:00
|
|
|
webapp <- WebApp
|
2012-10-29 04:15:43 +00:00
|
|
|
<$> pure assistantdata
|
2012-07-31 16:17:31 +00:00
|
|
|
<*> (pack <$> genRandomToken)
|
2012-10-29 04:15:43 +00:00
|
|
|
<*> getreldir
|
2012-07-31 16:17:31 +00:00
|
|
|
<*> pure $(embed "static")
|
|
|
|
<*> newWebAppState
|
2012-08-01 20:10:26 +00:00
|
|
|
<*> pure postfirstrun
|
2012-10-29 04:15:43 +00:00
|
|
|
<*> pure noannex
|
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
|
|
|
|
)
|
2013-01-10 03:17:52 +00:00
|
|
|
runWebApp app' $ \addr -> if noannex
|
2012-10-29 04:15:43 +00:00
|
|
|
then withTempFile "webapp.html" $ \tmpfile _ ->
|
2013-01-10 03:17:52 +00:00
|
|
|
go addr webapp tmpfile Nothing
|
2012-10-29 04:15:43 +00:00
|
|
|
else do
|
|
|
|
let st = threadState assistantdata
|
2012-09-18 21:50:07 +00:00
|
|
|
htmlshim <- runThreadState st $ fromRepo gitAnnexHtmlShim
|
|
|
|
urlfile <- runThreadState st $ fromRepo gitAnnexUrlFile
|
2013-01-10 03:17:52 +00:00
|
|
|
go addr webapp htmlshim (Just urlfile)
|
2012-10-29 18:30:10 +00:00
|
|
|
where
|
|
|
|
thread = NamedThread thisThread
|
|
|
|
getreldir
|
|
|
|
| noannex = return Nothing
|
|
|
|
| otherwise = Just <$>
|
|
|
|
(relHome =<< absPath
|
|
|
|
=<< runThreadState (threadState assistantdata) (fromRepo repoPath))
|
2013-01-10 03:17:52 +00:00
|
|
|
go addr webapp htmlshim urlfile = do
|
|
|
|
let url = myUrl webapp addr
|
2013-01-03 22:50:30 +00:00
|
|
|
maybe noop (`writeFileProtected` url) urlfile
|
|
|
|
writeHtmlShim "Starting webapp..." url htmlshim
|
2012-10-29 18:30:10 +00:00
|
|
|
maybe noop (\a -> a url htmlshim) onstartup
|
2012-07-26 03:13:01 +00:00
|
|
|
|
2013-01-10 03:17:52 +00:00
|
|
|
myUrl :: WebApp -> SockAddr -> Url
|
|
|
|
myUrl webapp addr = unpack $ yesodRender webapp urlbase HomeR []
|
2012-10-29 18:30:10 +00:00
|
|
|
where
|
2013-01-10 03:17:52 +00:00
|
|
|
urlbase = pack $ "http://" ++ show addr
|