Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add DevelMain support
  • Loading branch information
chrisdone committed Nov 14, 2014
1 parent 5628f0d commit c5e0e85
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 27 deletions.
3 changes: 3 additions & 0 deletions .dir-locals.el
@@ -0,0 +1,3 @@
((haskell-mode . ((haskell-process-type . cabal-repl)
(haskell-process-use-ghci . t)))
(css-mode . ((haskell-process-use-ghci . t))))
33 changes: 33 additions & 0 deletions src/DevelMain.hs
@@ -0,0 +1,33 @@
-- | Main entry point to tryhaskell.
--
-- Try Haskell!

module DevelMain where

import Control.Concurrent
import Control.Exception
import Foreign.Store
import TryHaskell

-- | Main entry point.
main :: IO ()
main =
do ((stats,statsT),(cache,cacheT)) <- setupServer
tid <- forkIO (finally (startServer cache stats)
(do killThread statsT
killThread cacheT))
putStrLn ("Writing store with " ++ show tid ++ " ...")
writeStore (Store 0) tid

-- | Update the running server.
update :: IO ()
update =
do m <- lookupStore 0
case m of
Nothing -> do putStrLn "Starting fresh ..."
main
Just s ->
do tid <- readStore s
putStrLn ("Killing thread " ++ show tid)
killThread tid
main
19 changes: 13 additions & 6 deletions src/TryHaskell.hs
Expand Up @@ -56,15 +56,22 @@ data EvalResult
data Stats = Stats
{ statsUsers :: !(HashMap ByteString UTCTime) }

-- | Start a web server.
startServer :: IO ()
startServer =
-- | Setup the server threads and state.
setupServer :: IO ((MVar Stats,ThreadId),(MVar (HashMap (ByteString,ByteString) Value),ThreadId))
setupServer =
do checkMuEval
cache <- newMVar mempty
stats <- newMVar (Stats mempty)
void (forkIO (expireVisitors stats))
void (forkIO (expireCache cache))
httpServe server (dispatch cache stats)
expire <- forkIO (expireVisitors stats)
cacheT <- forkIO (expireCache cache)
return ((stats,expire),(cache,cacheT))

-- | Start a web server.
startServer :: MVar (HashMap (ByteString,ByteString) Value)
-> MVar Stats
-> IO ()
startServer cache stats =
httpServe server (dispatch cache stats)
where server = setDefaults defaultConfig
setDefaults =
setPort 4001 .
Expand Down
8 changes: 7 additions & 1 deletion src/main/Main.hs
Expand Up @@ -4,8 +4,14 @@

module Main where

import Control.Concurrent
import Control.Exception
import TryHaskell

-- | Main entry point.
main :: IO ()
main = startServer
main =
do ((stats,statsT),(cache,cacheT)) <- setupServer
finally (startServer cache stats)
(do killThread statsT
killThread cacheT)
41 changes: 21 additions & 20 deletions tryhaskell.cabal
Expand Up @@ -17,26 +17,27 @@ library
other-modules: Paths_tryhaskell
hs-source-dirs: src/
ghc-options: -Wall
build-depends: base >= 4 && < 5,
snap-server,
snap-core,
blaze,
aeson >= 0.8.0.1,
mueval,
process-extras,
text,
mtl,
bytestring >= 0.10.4.0,
pure-io,
safe,
unordered-containers,
time,
hashable,
old-locale,
containers,
bifunctors,
QuickCheck,
show
build-depends: QuickCheck
, aeson >= 0.8.0.1
, base >= 4 && < 5
, bifunctors
, blaze
, bytestring >= 0.10.4.0
, containers
, foreign-store
, hashable
, mtl
, mueval
, old-locale
, process-extras
, pure-io
, safe
, show
, snap-core
, snap-server
, text
, time
, unordered-containers

executable tryhaskell
hs-source-dirs: src/main
Expand Down

0 comments on commit c5e0e85

Please sign in to comment.