2015-01-18 19:44:41 +00:00
|
|
|
-- eidolon -- A simple gallery in Haskell and Yesod
|
|
|
|
-- Copyright (C) 2015 Amedeo Molnár
|
|
|
|
--
|
|
|
|
-- This program is free software: you can redistribute it and/or modify
|
|
|
|
-- it under the terms of the GNU Affero General Public License as published
|
|
|
|
-- by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
-- (at your option) any later version.
|
|
|
|
--
|
|
|
|
-- This program is distributed in the hope that it will be useful,
|
|
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
-- GNU Affero General Public License for more details.
|
|
|
|
--
|
|
|
|
-- You should have received a copy of the GNU Affero General Public License
|
2015-01-21 09:00:18 +00:00
|
|
|
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2015-01-18 19:44:41 +00:00
|
|
|
|
2014-08-09 18:33:22 +00:00
|
|
|
-- | Development version to be run inside GHCi.
|
|
|
|
--
|
|
|
|
-- start this up with:
|
|
|
|
--
|
|
|
|
-- cabal repl --ghc-options="-O0 -fobject-code"
|
|
|
|
--
|
2014-12-23 04:01:53 +00:00
|
|
|
-- run with:
|
|
|
|
--
|
|
|
|
-- :l DevelMain
|
|
|
|
-- DevelMain.update
|
|
|
|
--
|
2014-08-09 18:33:22 +00:00
|
|
|
-- You will need to add these packages to your .cabal file
|
2014-12-23 04:01:53 +00:00
|
|
|
-- * foreign-store >= 0.1 (very light-weight)
|
2014-08-09 18:33:22 +00:00
|
|
|
-- * warp (you already depend on this, it just isn't in your .cabal file)
|
|
|
|
--
|
|
|
|
-- If you don't use cabal repl, you will need
|
2014-12-23 04:01:53 +00:00
|
|
|
-- to add settings to your .ghci file.
|
2014-08-09 18:33:22 +00:00
|
|
|
--
|
|
|
|
-- :set -DDEVELOPMENT
|
|
|
|
--
|
2014-12-23 04:01:53 +00:00
|
|
|
-- There is more information about using ghci
|
2014-08-09 18:33:22 +00:00
|
|
|
-- on the wiki: https://github.com/yesodweb/yesod/wiki/ghci
|
|
|
|
|
|
|
|
module DevelMain where
|
|
|
|
|
|
|
|
import Application (getApplicationDev)
|
|
|
|
|
|
|
|
import Control.Exception (finally)
|
|
|
|
import Control.Concurrent
|
|
|
|
import Data.IORef
|
|
|
|
import Foreign.Store
|
|
|
|
import Network.Wai.Handler.Warp
|
|
|
|
|
|
|
|
-- | Start or restart the server.
|
2014-12-23 04:01:53 +00:00
|
|
|
-- A Store holds onto some data across ghci reloads
|
2014-08-09 18:33:22 +00:00
|
|
|
update :: IO ()
|
|
|
|
update = do
|
2014-12-23 04:01:53 +00:00
|
|
|
mtidStore <- lookupStore tidStoreNum
|
2014-08-09 18:33:22 +00:00
|
|
|
case mtidStore of
|
2014-12-23 04:01:53 +00:00
|
|
|
-- no server running
|
2014-08-09 18:33:22 +00:00
|
|
|
Nothing -> do
|
2014-12-23 04:01:53 +00:00
|
|
|
done <- storeAction doneStore newEmptyMVar
|
2014-08-09 18:33:22 +00:00
|
|
|
tid <- start done
|
2014-12-23 04:01:53 +00:00
|
|
|
_ <- storeAction (Store tidStoreNum) (newIORef tid)
|
2014-08-09 18:33:22 +00:00
|
|
|
return ()
|
2014-12-23 04:01:53 +00:00
|
|
|
-- server is already running
|
|
|
|
Just tidStore ->
|
|
|
|
-- shut the server down with killThread and wait for the done signal
|
|
|
|
modifyStoredIORef tidStore $ \tid -> do
|
|
|
|
killThread tid
|
|
|
|
withStore doneStore takeMVar >> readStore doneStore >>= start
|
|
|
|
where
|
|
|
|
doneStore = Store 0
|
|
|
|
tidStoreNum = 1
|
|
|
|
|
|
|
|
modifyStoredIORef :: Store (IORef a) -> (a -> IO a) -> IO ()
|
|
|
|
modifyStoredIORef store f = withStore store $ \ref -> do
|
|
|
|
v <- readIORef ref
|
|
|
|
f v >>= writeIORef ref
|
2014-08-09 18:33:22 +00:00
|
|
|
|
|
|
|
-- | Start the server in a separate thread.
|
|
|
|
start :: MVar () -- ^ Written to when the thread is killed.
|
|
|
|
-> IO ThreadId
|
|
|
|
start done = do
|
2014-12-23 04:01:53 +00:00
|
|
|
(settings,app) <- getApplicationDev
|
|
|
|
forkIO (finally (runSettings settings app)
|
2014-08-09 18:33:22 +00:00
|
|
|
(putMVar done ()))
|