2012-07-26 01:26:13 +00:00
|
|
|
{- git-annex assistant webapp
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE TypeFamilies, QuasiQuotes, MultiParamTypeClasses, TemplateHaskell, OverloadedStrings #-}
|
|
|
|
|
|
|
|
module Assistant.Threads.WebApp where
|
|
|
|
|
|
|
|
import Assistant.Common
|
2012-07-26 03:13:01 +00:00
|
|
|
import Assistant.ThreadedMonad
|
2012-07-26 01:26:13 +00:00
|
|
|
import Assistant.DaemonStatus
|
|
|
|
import Utility.WebApp
|
2012-07-26 06:45:01 +00:00
|
|
|
import Utility.Yesod
|
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:50:29 +00:00
|
|
|
import Text.Hamlet
|
2012-07-26 03:13:01 +00:00
|
|
|
import Network.Socket (PortNumber)
|
2012-07-26 07:38:20 +00:00
|
|
|
import Text.Blaze.Renderer.String
|
2012-07-26 08:50:09 +00:00
|
|
|
import Data.Text
|
|
|
|
|
|
|
|
thisThread :: String
|
|
|
|
thisThread = "WebApp"
|
2012-07-26 01:26:13 +00:00
|
|
|
|
2012-07-26 06:45:01 +00:00
|
|
|
data WebApp = WebApp
|
|
|
|
{ daemonStatus :: DaemonStatusHandle
|
2012-07-26 08:50:09 +00:00
|
|
|
, secretToken :: Text
|
2012-07-26 06:45:01 +00:00
|
|
|
, baseTitle :: String
|
|
|
|
, getStatic :: Static
|
|
|
|
}
|
|
|
|
|
|
|
|
staticFiles "static"
|
2012-07-26 01:26:13 +00:00
|
|
|
|
|
|
|
mkYesod "WebApp" [parseRoutes|
|
|
|
|
/ HomeR GET
|
|
|
|
/config ConfigR GET
|
2012-07-26 09:20:52 +00:00
|
|
|
/static StaticR Static getStatic
|
2012-07-26 01:26:13 +00:00
|
|
|
|]
|
|
|
|
|
2012-07-26 06:45:01 +00:00
|
|
|
instance Yesod WebApp where
|
|
|
|
defaultLayout contents = do
|
|
|
|
page <- widgetToPageContent contents
|
|
|
|
mmsg <- getMessage
|
|
|
|
webapp <- getYesod
|
|
|
|
hamletToRepHtml $(hamletFile $ hamletTemplate "default-layout")
|
2012-07-26 01:26:13 +00:00
|
|
|
|
2012-07-26 08:50:09 +00:00
|
|
|
{- Require an auth token be set when accessing any (non-static route) -}
|
|
|
|
isAuthorized _ _ = checkAuthToken secretToken
|
|
|
|
|
|
|
|
{- Add the auth token to every url generated, except static subsite
|
|
|
|
- urls (which can show up in Permission Denied pages). -}
|
|
|
|
joinPath = insertAuthToken secretToken excludeStatic
|
|
|
|
where
|
|
|
|
excludeStatic [] = True
|
|
|
|
excludeStatic (p:_) = p /= "static"
|
|
|
|
|
2012-07-26 16:41:20 +00:00
|
|
|
makeSessionBackend = webAppSessionBackend
|
2012-07-26 16:10:53 +00:00
|
|
|
|
2012-07-26 01:26:13 +00:00
|
|
|
getHomeR :: Handler RepHtml
|
2012-07-26 06:45:01 +00:00
|
|
|
getHomeR = defaultLayout $ do
|
2012-07-26 09:20:52 +00:00
|
|
|
[whamlet|Hello, World<p><a href="@{ConfigR}">config|]
|
2012-07-26 01:26:13 +00:00
|
|
|
|
|
|
|
getConfigR :: Handler RepHtml
|
2012-07-26 06:45:01 +00:00
|
|
|
getConfigR = defaultLayout $ do
|
|
|
|
setTitle "configuration"
|
2012-07-26 09:20:52 +00:00
|
|
|
[whamlet|<a href="@{HomeR}">main|]
|
2012-07-26 01:26:13 +00:00
|
|
|
|
2012-07-26 03:13:01 +00:00
|
|
|
webAppThread :: ThreadState -> DaemonStatusHandle -> IO ()
|
|
|
|
webAppThread st dstatus = do
|
2012-07-26 06:45:01 +00:00
|
|
|
webapp <- mkWebApp st dstatus
|
2012-07-26 04:39:25 +00:00
|
|
|
app <- toWaiApp webapp
|
2012-07-26 01:26:13 +00:00
|
|
|
app' <- ifM debugEnabled
|
|
|
|
( return $ httpDebugLogger app
|
|
|
|
, return app
|
|
|
|
)
|
2012-07-26 07:38:20 +00:00
|
|
|
runWebApp app' $ \port -> runThreadState st $ writeHtmlShim webapp port
|
2012-07-26 06:45:01 +00:00
|
|
|
|
|
|
|
mkWebApp :: ThreadState -> DaemonStatusHandle -> IO WebApp
|
|
|
|
mkWebApp st dstatus = do
|
|
|
|
dir <- absPath =<< runThreadState st (fromRepo repoPath)
|
|
|
|
home <- myHomeDir
|
|
|
|
let reldir = if dirContains home dir
|
|
|
|
then relPathDirToFile home dir
|
|
|
|
else dir
|
2012-07-26 07:38:20 +00:00
|
|
|
token <- genRandomToken
|
2012-07-26 06:45:01 +00:00
|
|
|
return $ WebApp
|
|
|
|
{ daemonStatus = dstatus
|
2012-07-26 08:50:09 +00:00
|
|
|
, secretToken = pack token
|
2012-07-26 06:45:01 +00:00
|
|
|
, baseTitle = reldir
|
2012-07-26 07:38:20 +00:00
|
|
|
, getStatic = $(embed "static")
|
2012-07-26 06:45:01 +00:00
|
|
|
}
|
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-26 07:38:20 +00:00
|
|
|
writeHtmlShim :: WebApp -> PortNumber -> Annex ()
|
|
|
|
writeHtmlShim webapp port = do
|
2012-07-26 08:50:09 +00:00
|
|
|
liftIO $ debug thisThread ["running on port", show port]
|
2012-07-26 03:13:01 +00:00
|
|
|
htmlshim <- fromRepo gitAnnexHtmlShim
|
2012-07-26 07:38:20 +00:00
|
|
|
liftIO $ viaTmp go htmlshim $ genHtmlShim webapp port
|
|
|
|
where
|
|
|
|
go file content = do
|
|
|
|
h <- openFile file WriteMode
|
|
|
|
modifyFileMode file $ removeModes [groupReadMode, otherReadMode]
|
|
|
|
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
|
|
|
|
genHtmlShim webapp port = renderHtml $(shamletFile $ hamletTemplate "htmlshim")
|
2012-07-26 01:26:13 +00:00
|
|
|
where
|
2012-07-26 08:50:09 +00:00
|
|
|
url = "http://localhost:" ++ show port ++
|
|
|
|
"/?auth=" ++ unpack (secretToken webapp)
|