Skip to content

Commit

Permalink
More source style cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorycollins committed Dec 11, 2011
1 parent 0d75768 commit 424e356
Show file tree
Hide file tree
Showing 16 changed files with 260 additions and 203 deletions.
21 changes: 11 additions & 10 deletions project_template/default/src/Application.hs
@@ -1,29 +1,30 @@
{-# LANGUAGE TemplateHaskell #-}

{-
This module defines our application's state type and an alias for its handler
monad.
-}

------------------------------------------------------------------------------
-- | This module defines our application's state type and an alias for its
-- handler monad.
--
module Application where

------------------------------------------------------------------------------
import Data.Lens.Template
import Data.Time.Clock

import Snap.Snaplet
import Snap.Snaplet.Heist

------------------------------------------------------------------------------
data App = App
{ _heist :: Snaplet (Heist App)
, _startTime :: UTCTime
}

type AppHandler = Handler App App

makeLens ''App

instance HasHeist App where
heistLens = subSnaplet heist


------------------------------------------------------------------------------
type AppHandler = Handler App App


115 changes: 53 additions & 62 deletions project_template/default/src/Main.hs
@@ -1,18 +1,15 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE TemplateHaskell #-}

module Main where

------------------------------------------------------------------------------
import Control.Exception (SomeException, try)

import qualified Data.Text as T

import Snap.Http.Server
import Snap.Snaplet
import Snap.Core

import System.IO

import Site

#ifdef DEVELOPMENT
Expand All @@ -22,51 +19,46 @@ import Snap.Loader.Prod
#endif


{-|
This is the entry point for this web server application. It supports
easily switching between interpreting source and running statically
compiled code.
In either mode, the generated program should be run from the root of
the project tree. When it is run, it locates its templates, static
content, and source files in development mode, relative to the current
working directory.
When compiled with the development flag, only changes to the
libraries, your cabal file, or this file should require a recompile to
be picked up. Everything else is interpreted at runtime. There are a
few consequences of this.
First, this is much slower. Running the interpreter takes a
significant chunk of time (a couple tenths of a second on the author's
machine, at this time), regardless of the simplicity of the loaded
code. In order to recompile and re-load server state as infrequently
as possible, the source directories are watched for updates, as are
any extra directories specified below.
Second, the generated server binary is MUCH larger, since it links in
the GHC API (via the hint library).
Third, and the reason you would ever want to actually compile with
development mode, is that it enables a faster development cycle. You
can simply edit a file, save your changes, and hit reload to see your
changes reflected immediately.
When this is compiled without the development flag, all the actions
are statically compiled in. This results in faster execution, a
smaller binary size, and having to recompile the server for any code
change.
-}
------------------------------------------------------------------------------
-- | This is the entry point for this web server application. It supports
-- easily switching between interpreting source and running statically compiled
-- code.
--
-- In either mode, the generated program should be run from the root of the
-- project tree. When it is run, it locates its templates, static content, and
-- source files in development mode, relative to the current working directory.
--
-- When compiled with the development flag, only changes to the libraries, your
-- cabal file, or this file should require a recompile to be picked up.
-- Everything else is interpreted at runtime. There are a few consequences of
-- this.
--
-- First, this is much slower. Running the interpreter takes a significant
-- chunk of time (a couple tenths of a second on the author's machine, at this
-- time), regardless of the simplicity of the loaded code. In order to
-- recompile and re-load server state as infrequently as possible, the source
-- directories are watched for updates, as are any extra directories specified
-- below.
--
-- Second, the generated server binary is MUCH larger, since it links in the
-- GHC API (via the hint library).
--
-- Third, and the reason you would ever want to actually compile with
-- development mode, is that it enables a faster development cycle. You can
-- simply edit a file, save your changes, and hit reload to see your changes
-- reflected immediately.
--
-- When this is compiled without the development flag, all the actions are
-- statically compiled in. This results in faster execution, a smaller binary
-- size, and having to recompile the server for any code change.
--
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.
-- 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
["resources/templates"])
Expand All @@ -75,12 +67,12 @@ main = do
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
-- 'getActions' takes. It also must be an instance of Typeable. If
-- the type of this is changed, a full recompile will be needed to
------------------------------------------------------------------------------
-- | 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 'getActions' 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
Expand All @@ -89,16 +81,15 @@ getConf :: IO (Config Snap ())
getConf = commandLineConfig 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.
------------------------------------------------------------------------------
-- | 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.
-- 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.
Expand Down
16 changes: 7 additions & 9 deletions project_template/default/src/Site.hs
@@ -1,17 +1,15 @@
{-# LANGUAGE OverloadedStrings #-}

{-|
This is where all the routes and handlers are defined for your site. The
'app' function is the initializer that combines everything together and
is exported by this module.
-}

------------------------------------------------------------------------------
-- | This module is where all the routes and handlers are defined for your
-- site. The 'app' function is the initializer that combines everything
-- together and is exported by this module.
--
module Site
( app
) where

------------------------------------------------------------------------------
import Control.Applicative
import Control.Monad.Trans
import Control.Monad.State
Expand All @@ -26,7 +24,7 @@ import Snap.Snaplet.Heist
import Snap.Util.FileServe
import Text.Templating.Heist
import Text.XmlHtml hiding (render)

------------------------------------------------------------------------------
import Application


Expand Down
3 changes: 2 additions & 1 deletion src/Control/Access/RoleBased/Checker.hs
Expand Up @@ -3,6 +3,7 @@

module Control.Access.RoleBased.Checker where

------------------------------------------------------------------------------
import Control.Monad
import Control.Monad.Logic
import Control.Monad.Reader
Expand All @@ -11,7 +12,7 @@ import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as M
import Data.Maybe (fromMaybe, isJust)
import Data.Text (Text)

------------------------------------------------------------------------------
import Control.Access.RoleBased.Internal.RoleMap (RoleMap)
import qualified Control.Access.RoleBased.Internal.RoleMap as RM
import Control.Access.RoleBased.Internal.Types
Expand Down
8 changes: 7 additions & 1 deletion src/Control/Access/RoleBased/Internal/Role.hs
@@ -1,5 +1,6 @@
module Control.Access.RoleBased.Internal.Role where

------------------------------------------------------------------------------
import Control.Monad.ST
import Data.Hashable
import Data.HashMap.Strict (HashMap)
Expand All @@ -18,10 +19,12 @@ data RoleValue = RoleBool Bool
deriving (Ord, Eq, Show)


------------------------------------------------------------------------------
instance IsString RoleValue where
fromString = RoleText . fromString


------------------------------------------------------------------------------
instance Hashable RoleValue where
hashWithSalt salt (RoleBool e) = hashWithSalt salt e `combine` 7
hashWithSalt salt (RoleText t) = hashWithSalt salt t `combine` 196613
Expand All @@ -38,6 +41,7 @@ data Role = Role {
deriving (Eq, Show)


------------------------------------------------------------------------------
instance IsString Role where
fromString s = Role (fromString s) M.empty

Expand All @@ -51,7 +55,7 @@ toSortedList m = runST $ do
return $ V.toList v'



------------------------------------------------------------------------------
instance Hashable Role where
hashWithSalt salt (Role nm dat) =
h $ hashWithSalt salt nm
Expand All @@ -67,13 +71,15 @@ data RoleValueMeta = RoleBoolMeta
| RoleDoubleMeta


------------------------------------------------------------------------------
data RoleDataDefinition = RoleDataDefinition {
_roleDataName :: Text
, _roleValueMeta :: RoleValueMeta
, _roleDataDescription :: Text
}


------------------------------------------------------------------------------
data RoleMetadata = RoleMetadata {
_roleMetadataName :: Text
, _roleDescription :: Text
Expand Down
4 changes: 3 additions & 1 deletion src/Control/Access/RoleBased/Internal/RoleMap.hs
@@ -1,16 +1,18 @@
module Control.Access.RoleBased.Internal.RoleMap where

------------------------------------------------------------------------------
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as M
import Data.HashSet (HashSet)
import qualified Data.HashSet as S
import Data.List (find, foldl')
import Data.Text (Text)

------------------------------------------------------------------------------
import Control.Access.RoleBased.Role
import Control.Access.RoleBased.Internal.Types


------------------------------------------------------------------------------
newtype RoleMap = RoleMap (HashMap Text (HashSet Role))


Expand Down
8 changes: 7 additions & 1 deletion src/Control/Access/RoleBased/Internal/Rule.hs
@@ -1,18 +1,24 @@
module Control.Access.RoleBased.Internal.Rule where

------------------------------------------------------------------------------
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as M
import Data.List (foldl')
import Data.Monoid
import Data.Text (Text)

------------------------------------------------------------------------------
import Control.Access.RoleBased.Internal.Role


------------------------------------------------------------------------------
data Rule = Rule Text (Role -> [Role])


------------------------------------------------------------------------------
newtype RuleSet = RuleSet (HashMap Text (Role -> [Role]))


------------------------------------------------------------------------------
instance Monoid RuleSet where
mempty = RuleSet M.empty
(RuleSet m1) `mappend` (RuleSet m2) = RuleSet $ M.foldlWithKey' ins m2 m1
Expand Down
3 changes: 2 additions & 1 deletion src/Control/Access/RoleBased/Internal/Types.hs
Expand Up @@ -9,10 +9,11 @@ module Control.Access.RoleBased.Internal.Types
, RuleChecker(..)
) where

------------------------------------------------------------------------------
import Control.Applicative
import Control.Monad.Reader
import Control.Monad.Logic

------------------------------------------------------------------------------
import Control.Access.RoleBased.Internal.Role
import Control.Access.RoleBased.Internal.Rule

Expand Down
1 change: 1 addition & 0 deletions src/Control/Access/RoleBased/Role.hs
@@ -1,5 +1,6 @@
module Control.Access.RoleBased.Role where

------------------------------------------------------------------------------
import qualified Data.HashMap.Strict as M
import Control.Access.RoleBased.Internal.Types
import Data.Text (Text)
Expand Down

0 comments on commit 424e356

Please sign in to comment.