{-# LANGUAGE CPP             #-}
# LANGUAGE TemplateHaskell #



 NOTE : Do n't modify this file unless Snap upstream implements a new 
 model for static / dynamic loading . 



NOTE: Don't modify this file unless Snap upstream implements a new
model for static/dynamic loading.

-}
module Main where

------------------------------------------------------------------------------
import           Control.Exception (SomeException, try)
import qualified Data.Text as T
import           Snap.Http.Server
import           Snap.Snaplet
import           Snap.Snaplet.Config
import           Snap.Core

import           System.IO
import           System.Posix.Syslog
import           System.Environment (getProgName)
import           ApplicationInit

#ifdef DEVELOPMENT
import           Snap.Loader.Dynamic
#else
import           Snap.Loader.Static
#endif


main :: IO ()
main = do
    -- Depending on the version of loadSnapTH in scope, this either enables
    -- dynamic reloading, or compiles it without. The last argument to
    -- loadSnapTH is a list of additional directories to watch for changes to
    -- trigger reloads in development mode. It doesn't need to include source
    -- directories, those are picked up automatically by the splice.
    (conf, site, cleanup) <- $(loadSnapTH [| getConf |]
                                          'getActions
                                          ["snaplets/heist/templates"])

    progName <- getProgName
    _ <- withSyslog progName [LogPID] User
      $ try $ httpServe conf site :: IO (Either SomeException ())
    cleanup


------------------------------------------------------------------------------
-- | This action loads the config used by this application. The loaded config
  is returned as the first element of the tuple produced by the loadSnapTH
  Splice . The type is not solidly fixed , though it must be an IO action that
  produces the same type as '  ' takes . It also must be an instance of
-- Typeable. If the type of this is changed, a full recompile will be needed to
-- pick up the change, even in development mode.
--
-- This action is only run once, regardless of whether development or
-- production mode is in use.
getConf :: IO (Config Snap AppConfig)
getConf = commandLineAppConfig defaultConfig


------------------------------------------------------------------------------
-- | This function generates the the site handler and cleanup action from the
-- configuration. In production mode, this action is only run once. In
-- development mode, this action is run whenever the application is reloaded.
--
-- Development mode also makes sure that the cleanup actions are run
-- appropriately before shutdown. The cleanup action returned from loadSnapTH
-- should still be used after the server has stopped handling requests, as the
-- cleanup actions are only automatically run when a reload is triggered.
--
-- This sample doesn't actually use the config passed in, but more
-- sophisticated code might.
getActions :: Config Snap AppConfig -> IO (Snap (), IO ())
getActions conf = do
    (msgs, site, cleanup) <- runSnaplet
        (appEnvironment =<< getOther conf) appInit
    hPutStrLn stderr $ T.unpack msgs
    return (site, cleanup)

# LANGUAGE CPP             #
----------------------------------------------------------------------------
 Depending on the version of loadSnapTH in scope, this either enables
 dynamic reloading, or compiles it without. The last argument to
 loadSnapTH is a list of additional directories to watch for changes to
 trigger reloads in development mode. It doesn't need to include source
 directories, those are picked up automatically by the splice.
----------------------------------------------------------------------------
 | This action loads the config used by this application. The loaded config
 Typeable. If the type of this is changed, a full recompile will be needed to
 pick up the change, even in development mode.

 This action is only run once, regardless of whether development or
 production mode is in use.
----------------------------------------------------------------------------
 | This function generates the the site handler and cleanup action from the
 configuration. In production mode, this action is only run once. In
 development mode, this action is run whenever the application is reloaded.

 Development mode also makes sure that the cleanup actions are run
 appropriately before shutdown. The cleanup action returned from loadSnapTH
 should still be used after the server has stopped handling requests, as the
 cleanup actions are only automatically run when a reload is triggered.

 This sample doesn't actually use the config passed in, but more
 sophisticated code might.
# LANGUAGE TemplateHaskell #



 NOTE : Do n't modify this file unless Snap upstream implements a new 
 model for static / dynamic loading . 



NOTE: Don't modify this file unless Snap upstream implements a new
model for static/dynamic loading.

-}
module Main where

import           Control.Exception (SomeException, try)
import qualified Data.Text as T
import           Snap.Http.Server
import           Snap.Snaplet
import           Snap.Snaplet.Config
import           Snap.Core

import           System.IO
import           System.Posix.Syslog
import           System.Environment (getProgName)
import           ApplicationInit

#ifdef DEVELOPMENT
import           Snap.Loader.Dynamic
#else
import           Snap.Loader.Static
#endif


main :: IO ()
main = do
    (conf, site, cleanup) <- $(loadSnapTH [| getConf |]
                                          'getActions
                                          ["snaplets/heist/templates"])

    progName <- getProgName
    _ <- withSyslog progName [LogPID] User
      $ try $ httpServe conf site :: IO (Either SomeException ())
    cleanup


  is returned as the first element of the tuple produced by the loadSnapTH
  Splice . The type is not solidly fixed , though it must be an IO action that
  produces the same type as '  ' takes . It also must be an instance of
getConf :: IO (Config Snap AppConfig)
getConf = commandLineAppConfig defaultConfig


getActions :: Config Snap AppConfig -> IO (Snap (), IO ())
getActions conf = do
    (msgs, site, cleanup) <- runSnaplet
        (appEnvironment =<< getOther conf) appInit
    hPutStrLn stderr $ T.unpack msgs
    return (site, cleanup)
