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-10-18 13:28:20 +00:00
|
|
|
{-# LANGUAGE TypeFamilies, QuasiQuotes, MultiParamTypeClasses, TemplateHaskell, OverloadedStrings, RankNTypes, CPP #-}
|
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-10-18 13:28:20 +00:00
|
|
|
#ifdef WITH_S3
|
2012-09-26 18:44:07 +00:00
|
|
|
import Assistant.WebApp.Configurators.S3
|
2012-10-18 13:28:20 +00:00
|
|
|
#endif
|
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
|
2012-09-18 21:50:07 +00:00
|
|
|
import Assistant.WebApp.OtherRepos
|
2012-07-26 03:13:01 +00:00
|
|
|
import Assistant.ThreadedMonad
|
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
|
|
|
|
|
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
|
|
|
|
)
|
2012-10-29 04:15:43 +00:00
|
|
|
runWebApp app' $ \port -> if noannex
|
|
|
|
then withTempFile "webapp.html" $ \tmpfile _ ->
|
2012-09-18 21:50:07 +00:00
|
|
|
go port 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
|
|
|
|
go port 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))
|
|
|
|
go port webapp htmlshim urlfile = do
|
|
|
|
let url = myUrl webapp port
|
|
|
|
maybe noop (`writeFile` url) urlfile
|
|
|
|
writeHtmlShim url htmlshim
|
|
|
|
maybe noop (\a -> a url 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-09-18 21:50:07 +00:00
|
|
|
writeHtmlShim :: String -> FilePath -> IO ()
|
|
|
|
writeHtmlShim url file = viaTmp go file $ genHtmlShim url
|
2012-10-29 18:30:10 +00:00
|
|
|
where
|
|
|
|
go tmpfile content = do
|
|
|
|
h <- openFile tmpfile WriteMode
|
|
|
|
modifyFileMode tmpfile $ removeModes [groupReadMode, otherReadMode]
|
|
|
|
hPutStr h content
|
|
|
|
hClose h
|
2012-07-26 03:13:01 +00:00
|
|
|
|
|
|
|
{- TODO: generate this static file using Yesod. -}
|
2012-09-18 21:50:07 +00:00
|
|
|
genHtmlShim :: String -> String
|
|
|
|
genHtmlShim url = unlines
|
2012-08-02 01:26:36 +00:00
|
|
|
[ "<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-08-01 20:10:26 +00:00
|
|
|
|
2012-09-18 21:50:07 +00:00
|
|
|
myUrl :: WebApp -> PortNumber -> Url
|
|
|
|
myUrl webapp port = unpack $ yesodRender webapp urlbase HomeR []
|
2012-10-29 18:30:10 +00:00
|
|
|
where
|
|
|
|
urlbase = pack $ "http://localhost:" ++ show port
|