summaryrefslogtreecommitdiff
path: root/Assistant/Threads/WebApp.hs
blob: d475865dcabbedc2f995ab140228da21f43bb3be (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
63
64
65
{- 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
import Assistant.ThreadedMonad
import Assistant.DaemonStatus
import Utility.WebApp

import Yesod
import Text.Hamlet
import Network.Socket (PortNumber)
import Text.Blaze.Renderer.Utf8
import Data.ByteString.Lazy as L

data WebApp = WebApp DaemonStatusHandle

mkYesod "WebApp" [parseRoutes|
/ HomeR GET
/config ConfigR GET
|]

instance Yesod WebApp

getHomeR :: Handler RepHtml
getHomeR = defaultLayout [whamlet|Hello, World<p><a href=@{ConfigR}>config|]

getConfigR :: Handler RepHtml
getConfigR = defaultLayout [whamlet|<a href=@{HomeR}>main|]

webAppThread :: ThreadState -> DaemonStatusHandle -> IO ()
webAppThread st dstatus = do
	app <- toWaiApp (WebApp dstatus)
	app' <- ifM debugEnabled
		( return $ httpDebugLogger app
		, return app
		)
	runWebApp app' $ \p -> runThreadState st $ writeHtmlShim p

{- Creates a html shim file that's used to redirect into the webapp. -}
writeHtmlShim :: PortNumber -> Annex ()
writeHtmlShim port = do
	htmlshim <- fromRepo gitAnnexHtmlShim
	liftIO $ L.writeFile htmlshim $ genHtmlShim port

{- TODO: generate this static file using Yesod. -}
genHtmlShim :: PortNumber -> L.ByteString
genHtmlShim port = renderHtml [shamlet|
!!!
<html>
  <head>
    <meta http-equiv="refresh" content="0; URL=#{url}">
  <body>
    <p>
      <a href="#{url}">Starting webapp...
|]
	where
		url = "http://localhost:" ++ show port ++ "/"