happstack-foundation-0.5.8: Glue code for using Happstack with acid-state, web-routes, reform, and HSP

Safe HaskellNone
LanguageHaskell98

Happstack.Foundation

Contents

Description

happstack-foundation provides a type-safe environment for Haskell web development. It builds on top of:

  • happstack-server - an HTTP server
  • HSP - HTML Templating
  • web-routes - type-safe URL routing
  • reform - type-safe form composition and validation
  • acid-state - native Haskell persistent database

An example application can be found here:

http://patch-tag.com/r/mae/happstack/snapshot/current/content/pretty/happstack-foundation/examples/ControlV/Main.hs

A screencast can be found here:

http://www.youtube.com/watch?v=7Wmszk4wZxQ

happstack-foundation itself is not yet documented in the Happstack Crash Course. However, all of the components that it uses are:

http://www.happstack.com/docs/crashcourse/index.html

Synopsis

Configuration

data AcidConfig st where Source

configuration information for our acid-state database

Constructors

AcidLocal :: (IsAcidic st, Typeable st) => Maybe FilePath -> st -> AcidConfig (AcidState st) 
AcidUsing :: st -> AcidConfig st 

data FoundationConf Source

configuration for server

Constructors

FoundationConf 

Type Aliases

type FoundationT url acidState requestState m = XMLGenT (FoundationT' url acidState requestState m) Source

the FoundationT monad

  • url - the type-safe URL route type
  • acidState - the type of the state value stored in acid-state
  • requestState - a per-request state value that the developer can getsetmodify
  • m - inner monad

see also: whereami, getRequestState, setRequestState, modifyRequestState, simpleApp

type FoundationT' url acidState requestState m = RouteT url (StateT (AppState url acidState requestState) (ServerPartT m)) Source

similar to the FoundationT' type alias, but without the XMLGenT wrapper. This variant is most often used in class constraints.

type FoundationForm url acidState requestState m = Form (FoundationT url acidState requestState m) [Input] AppError [FoundationT url acidState requestState m XML] () Source

FoundationForm is an alias for working with reform based Forms

FoundationT functions

whereami :: (Functor m, Monad m) => FoundationT url acidState requestState m url Source

returns the decoded url from the Request

getRequestState :: (Functor m, MonadState (AppState url acidState requestState) m) => m requestState Source

get the requestState value

setRequestState :: (Functor m, MonadState (AppState url acidState requestState) m) => requestState -> m () Source

set the requestState value

modifyRequestState :: MonadState (AppState url acidState requestState) m => (requestState -> requestState) -> m () Source

set the requestState value

HTML Template

defaultTemplate :: (Functor m, Monad m, XMLGenerator (FoundationT' url acidState requestState m), EmbedAsChild (FoundationT' url acidState requestState m) body, EmbedAsChild (FoundationT' url acidState requestState m) headers, XMLType (FoundationT' url acidState requestState m) ~ XML) => Text -> headers -> body -> FoundationT url acidState requestState m XML Source

default page template

acid-state

class HasAcidState m st where Source

HasAcidState provides a single method getAcidState which can be used to retrieve an AcidState handle from the current monad.

Methods

getAcidState :: m (AcidState st) Source

Instances

HasAcidState (FoundationT' url acid reqSt m) acidSt => HasAcidState (XMLGenT (FoundationT' url acid reqSt m)) acidSt 
(Functor m, Monad m) => HasAcidState (FoundationT url (AcidState acidState) requestState m) acidState 

query :: forall event m. (Functor m, MonadIO m, QueryEvent event, HasAcidState m (EventState event)) => event -> m (EventResult event) Source

wrapper around query from acid-state

This variant automatically gets the AcidState handle from the monad

update :: forall event m. (Functor m, MonadIO m, UpdateEvent event, HasAcidState m (EventState event)) => event -> m (EventResult event) Source

wrapper around update from acid-state

This variant automatically gets the AcidState handle from the monad

getAcidSt :: (Functor m, MonadState (AppState url acidState requestState) m) => m acidState Source

running

simpleApp Source

Arguments

:: (ToMessage a, PathInfo url, Monad m) 
=> (forall r. m r -> IO r)

function to flatten inner monad

-> FoundationConf

Conf to pass onto simpleHTTP

-> AcidConfig acidState

AcidState configuration

-> requestState

initial requestState value

-> url

default URL (ie, what does / map to)

-> Text

the base URL for the site as seen by the outside world (or, at least, by your openid provider) (e.g. "http://example.org:8000", no trailing slash)

-> (url -> FoundationT url acidState requestState m a)

handler

-> IO () 

run the application

starts the database, listens for requests, etc.

re-exports

class Typeable * a => Data a where

The Data class comprehends a fundamental primitive gfoldl for folding over constructor applications, say terms. This primitive can be instantiated in several ways to map over the immediate subterms of a term; see the gmap combinators later in this class. Indeed, a generic programmer does not necessarily need to use the ingenious gfoldl primitive but rather the intuitive gmap combinators. The gfoldl primitive is completed by means to query top-level constructors, to turn constructor representations into proper terms, and to list all possible datatype constructors. This completion allows us to serve generic programming scenarios like read, show, equality, term generation.

The combinators gmapT, gmapQ, gmapM, etc are all provided with default definitions in terms of gfoldl, leaving open the opportunity to provide datatype-specific definitions. (The inclusion of the gmap combinators as members of class Data allows the programmer or the compiler to derive specialised, and maybe more efficient code per datatype. Note: gfoldl is more higher-order than the gmap combinators. This is subject to ongoing benchmarking experiments. It might turn out that the gmap combinators will be moved out of the class Data.)

Conceptually, the definition of the gmap combinators in terms of the primitive gfoldl requires the identification of the gfoldl function arguments. Technically, we also need to identify the type constructor c for the construction of the result type from the folded term type.

In the definition of gmapQx combinators, we use phantom type constructors for the c in the type of gfoldl because the result type of a query does not involve the (polymorphic) type of the term argument. In the definition of gmapQl we simply use the plain constant type constructor because gfoldl is left-associative anyway and so it is readily suited to fold a left-associative binary operation over the immediate subterms. In the definition of gmapQr, extra effort is needed. We use a higher-order accumulation trick to mediate between left-associative constructor application vs. right-associative binary operation (e.g., (:)). When the query is meant to compute a value of type r, then the result type withing generic folding is r -> r. So the result of folding is a function to which we finally pass the right unit.

With the -XDeriveDataTypeable option, GHC can generate instances of the Data class automatically. For example, given the declaration

data T a b = C1 a b | C2 deriving (Typeable, Data)

GHC will generate an instance that is equivalent to

instance (Data a, Data b) => Data (T a b) where
    gfoldl k z (C1 a b) = z C1 `k` a `k` b
    gfoldl k z C2       = z C2

    gunfold k z c = case constrIndex c of
                        1 -> k (k (z C1))
                        2 -> z C2

    toConstr (C1 _ _) = con_C1
    toConstr C2       = con_C2

    dataTypeOf _ = ty_T

con_C1 = mkConstr ty_T "C1" [] Prefix
con_C2 = mkConstr ty_T "C2" [] Prefix
ty_T   = mkDataType "Module.T" [con_C1, con_C2]

This is suitable for datatypes that are exported transparently.

Minimal complete definition

gunfold, toConstr, dataTypeOf

Methods

gfoldl

Arguments

:: forall (c :: * -> *). (forall d b. Data d => c (d -> b) -> d -> c b)

defines how nonempty constructor applications are folded. It takes the folded tail of the constructor application and its head, i.e., an immediate subterm, and combines them in some way.

-> (forall g. g -> c g)

defines how the empty constructor application is folded, like the neutral / start element for list folding.

-> a

structure to be folded.

-> c a

result, with a type defined in terms of a, but variability is achieved by means of type constructor c for the construction of the actual result type.

Left-associative fold operation for constructor applications.

The type of gfoldl is a headache, but operationally it is a simple generalisation of a list fold.

The default definition for gfoldl is const id, which is suitable for abstract datatypes with no substructures.

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c a

Unfolding constructor applications

toConstr :: a -> Constr

Obtaining the constructor from a given datum. For proper terms, this is meant to be the top-level constructor. Primitive datatypes are here viewed as potentially infinite sets of values (i.e., constructors).

dataTypeOf :: a -> DataType

The outer type constructor of the type

dataCast1 :: Typeable (* -> *) t => (forall d. Data d => c (t d)) -> Maybe (c a)

Mediate types and unary type constructors. In Data instances of the form T a, dataCast1 should be defined as gcast1.

The default definition is const Nothing, which is appropriate for non-unary type constructors.

dataCast2 :: Typeable (* -> * -> *) t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a)

Mediate types and binary type constructors. In Data instances of the form T a b, dataCast2 should be defined as gcast2.

The default definition is const Nothing, which is appropriate for non-binary type constructors.

gmapT :: (forall b. Data b => b -> b) -> a -> a

A generic transformation that maps over the immediate subterms

The default definition instantiates the type constructor c in the type of gfoldl to an identity datatype constructor, using the isomorphism pair as injection and projection.

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r

A generic query with a left-associative binary operator

gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r

A generic query with a right-associative binary operator

gmapQ :: (forall d. Data d => d -> u) -> a -> [u]

A generic query that processes the immediate subterms and returns a list of results. The list is given in the same order as originally specified in the declaratoin of the data constructors.

gmapQi :: Int -> (forall d. Data d => d -> u) -> a -> u

A generic query that processes one child by index (zero-based)

gmapM :: Monad m => (forall d. Data d => d -> m d) -> a -> m a

A generic monadic transformation that maps over the immediate subterms

The default definition instantiates the type constructor c in the type of gfoldl to the monad datatype constructor, defining injection and projection using return and >>=.

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> a -> m a

Transformation of at least one immediate subterm does not fail

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> a -> m a

Transformation of one immediate subterm with success

Instances

Data Bool 
Data Char 
Data Double 
Data Float 
Data Int 
Data Int8 
Data Int16 
Data Int32 
Data Int64 
Data Integer 
Data Ordering 
Data Word 
Data Word8 
Data Word16 
Data Word32 
Data Word64 
Data () 
Data Handle 
Data SpecConstrAnnotation 
Data DataType 
Data Version 
Data ThreadId 
Data TypeRep 
Data TyCon 
Data Text

This instance preserves data abstraction at the cost of inefficiency. We omit reflection services for the sake of data abstraction.

This instance was created by copying the updated behavior of Data.Text.Text

Data Text

This instance preserves data abstraction at the cost of inefficiency. We omit reflection services for the sake of data abstraction.

This instance was created by copying the updated behavior of Data.Set.Set and Data.Map.Map. If you feel a mistake has been made, please feel free to submit improvements.

The original discussion is archived here: could we get a Data instance for Data.Text.Text?

The followup discussion that changed the behavior of Set and Map is archived here: Proposal: Allow gunfold for Data.Map, ...

Data Method 
Data ModuleName 
Data SpecialCon 
Data QName 
Data Name 
Data IPName 
Data QOp 
Data Op 
Data CName 
Data Module 
Data ExportSpec 
Data Namespace 
Data ImportDecl 
Data ImportSpec 
Data Assoc 
Data Decl 
Data TypeEqn 
Data Annotation 
Data BooleanFormula 
Data DataOrNew 
Data Binds 
Data IPBind 
Data Match 
Data QualConDecl 
Data ConDecl 
Data GadtDecl 
Data ClassDecl 
Data InstDecl 
Data BangType 
Data Rhs 
Data GuardedRhs 
Data Type 
Data Promoted 
Data TyVarBind 
Data Kind 
Data FunDep 
Data Asst 
Data Literal 
Data Sign 
Data Exp 
Data XName 
Data XAttr 
Data Bracket 
Data Splice 
Data Safety 
Data CallConv 
Data ModulePragma 
Data Overlap 
Data Activation 
Data Rule 
Data RuleVar 
Data WarningText 
Data Pat 
Data PXAttr 
Data RPatOp 
Data RPat 
Data PatField 
Data Stmt 
Data QualStmt 
Data FieldUpdate 
Data Alt 
Data SrcLoc 
Data SrcSpan 
Data SrcSpanInfo 
Data Boxed 
Data Tool 
Data URI 
Data URIAuth 
Data LocalTime 
Data ZonedTime 
Data a => Data [a] 
(Data a, Integral a) => Data (Ratio a) 
Typeable * a => Data (StablePtr a) 
Typeable * a => Data (IO a) 
(Data a, Typeable * a) => Data (Ptr a) 
(Data a, Typeable * a) => Data (ForeignPtr a) 
Data a => Data (Complex a) 
Typeable * a => Data (STM a) 
Typeable * a => Data (TVar a) 
Typeable * a => Data (MVar a) 
Typeable * a => Data (IORef a) 
Data a => Data (Maybe a) 
Data a => Data (Errors a) 
Data l => Data (ModuleName l) 
Data l => Data (SpecialCon l) 
Data l => Data (QName l) 
Data l => Data (Name l) 
Data l => Data (IPName l) 
Data l => Data (QOp l) 
Data l => Data (Op l) 
Data l => Data (CName l) 
Data l => Data (Module l) 
Data l => Data (ModuleHead l) 
Data l => Data (ExportSpecList l) 
Data l => Data (ExportSpec l) 
Data l => Data (Namespace l) 
Data l => Data (ImportDecl l) 
Data l => Data (ImportSpecList l) 
Data l => Data (ImportSpec l) 
Data l => Data (Assoc l) 
Data l => Data (Decl l) 
Data l => Data (TypeEqn l) 
Data l => Data (Annotation l) 
Data l => Data (BooleanFormula l) 
Data l => Data (DataOrNew l) 
Data l => Data (DeclHead l) 
Data l => Data (InstRule l) 
Data l => Data (InstHead l) 
Data l => Data (Deriving l) 
Data l => Data (Binds l) 
Data l => Data (IPBind l) 
Data l => Data (Match l) 
Data l => Data (QualConDecl l) 
Data l => Data (ConDecl l) 
Data l => Data (FieldDecl l) 
Data l => Data (GadtDecl l) 
Data l => Data (ClassDecl l) 
Data l => Data (InstDecl l) 
Data l => Data (BangType l) 
Data l => Data (Rhs l) 
Data l => Data (GuardedRhs l) 
Data l => Data (Type l) 
Data l => Data (Promoted l) 
Data l => Data (TyVarBind l) 
Data l => Data (Kind l) 
Data l => Data (FunDep l) 
Data l => Data (Context l) 
Data l => Data (Asst l) 
Data l => Data (Literal l) 
Data l => Data (Sign l) 
Data l => Data (Exp l) 
Data l => Data (XName l) 
Data l => Data (XAttr l) 
Data l => Data (Bracket l) 
Data l => Data (Splice l) 
Data l => Data (Safety l) 
Data l => Data (CallConv l) 
Data l => Data (ModulePragma l) 
Data l => Data (Overlap l) 
Data l => Data (Activation l) 
Data l => Data (Rule l) 
Data l => Data (RuleVar l) 
Data l => Data (WarningText l) 
Data l => Data (Pat l) 
Data l => Data (PXAttr l) 
Data l => Data (RPatOp l) 
Data l => Data (RPat l) 
Data l => Data (PatField l) 
Data l => Data (Stmt l) 
Data l => Data (QualStmt l) 
Data l => Data (FieldUpdate l) 
Data l => Data (Alt l) 
Data a => Data (Vector a) 
(Data a, Unbox a) => Data (Vector a) 
(Data a, Storable a) => Data (Vector a) 
(Data a, Prim a) => Data (Vector a) 
(Data a, Data b) => Data (a -> b) 
(Data a, Data b) => Data (Either a b) 
(Data a, Data b) => Data (a, b) 
(Typeable * s, Typeable * a) => Data (ST s a) 
(Typeable * a, Data a, Data b, Ix a) => Data (Array a b) 
Data t => Data (Proxy * t) 
(Data a, Data b, Data c) => Data (a, b, c) 
(Coercible * a b, Data a, Data b) => Data (Coercion * a b) 
((~) * a b, Data a) => Data ((:~:) * a b) 
(Data a, Data b, Data c, Data d) => Data (a, b, c, d) 
(Data a, Data b, Data c, Data d, Data e) => Data (a, b, c, d, e) 
(Data a, Data b, Data c, Data d, Data e, Data f) => Data (a, b, c, d, e, f) 
(Data a, Data b, Data c, Data d, Data e, Data f, Data g) => Data (a, b, c, d, e, f, g) 

class Typeable a

The class Typeable allows a concrete representation of a type to be calculated.

Minimal complete definition

typeRep#

Instances

Typeable * Bool 
Typeable * Char 
Typeable * Double 
Typeable * Float 
Typeable * Int 
Typeable * Int8 
Typeable * Int16 
Typeable * Int32 
Typeable * Int64 
Typeable * Integer 
Typeable * Ordering 
Typeable * RealWorld 
Typeable * Word 
Typeable * Word8 
Typeable * Word16 
Typeable * Word32 
Typeable * Word64 
Typeable * () 
Typeable * Socket 
Typeable * SpecConstrAnnotation 
Typeable * DataType 
Typeable * ThreadId 
Typeable * TypeRep 
Typeable * TyCon 
Typeable * Text 
Typeable * Text 
Typeable * Method 
Typeable * RsFlags 
Typeable * Input 
Typeable * Response 
Typeable * Request 
Typeable * RqBody 
Typeable * ModuleName 
Typeable * SpecialCon 
Typeable * QName 
Typeable * Name 
Typeable * IPName 
Typeable * QOp 
Typeable * Op 
Typeable * CName 
Typeable * Module 
Typeable * ExportSpec 
Typeable * Namespace 
Typeable * ImportDecl 
Typeable * ImportSpec 
Typeable * Assoc 
Typeable * Decl 
Typeable * TypeEqn 
Typeable * Annotation 
Typeable * BooleanFormula 
Typeable * DataOrNew 
Typeable * Binds 
Typeable * IPBind 
Typeable * Match 
Typeable * QualConDecl 
Typeable * ConDecl 
Typeable * GadtDecl 
Typeable * ClassDecl 
Typeable * InstDecl 
Typeable * BangType 
Typeable * Rhs 
Typeable * GuardedRhs 
Typeable * Type 
Typeable * Promoted 
Typeable * TyVarBind 
Typeable * Kind 
Typeable * FunDep 
Typeable * Asst 
Typeable * Literal 
Typeable * Sign 
Typeable * Exp 
Typeable * XName 
Typeable * XAttr 
Typeable * Bracket 
Typeable * Splice 
Typeable * Safety 
Typeable * CallConv 
Typeable * ModulePragma 
Typeable * Overlap 
Typeable * Activation 
Typeable * Rule 
Typeable * RuleVar 
Typeable * WarningText 
Typeable * Pat 
Typeable * PXAttr 
Typeable * RPatOp 
Typeable * RPat 
Typeable * PatField 
Typeable * Stmt 
Typeable * QualStmt 
Typeable * FieldUpdate 
Typeable * Alt 
Typeable * SrcLoc 
Typeable * SrcSpan 
Typeable * SrcSpanInfo 
Typeable * Boxed 
Typeable * Tool 
Typeable * SocketOption 
Typeable * ShutdownCmd 
Typeable * AddrInfoFlag 
Typeable * AddrInfo 
Typeable * NameInfoFlag 
Typeable * SocketStatus 
Typeable * SocketType 
Typeable * PortNumber 
Typeable * SockAddr 
Typeable * URI 
Typeable * URIAuth 
Typeable * LocalTime 
Typeable * ZonedTime 
Typeable * Format 
Typeable * Method 
Typeable * CompressionLevel 
Typeable * WindowBits 
Typeable * MemoryLevel 
Typeable * CompressionStrategy 
(Typeable (k1 -> k) s, Typeable k1 a) => Typeable k (s a)

Kind-polymorphic Typeable instance for type application

Typeable ((* -> *) -> Constraint) Alternative 
Typeable ((* -> *) -> Constraint) Applicative 
Typeable (* -> * -> * -> * -> * -> * -> * -> *) (,,,,,,) 
Typeable (* -> * -> * -> * -> * -> * -> *) (,,,,,) 
Typeable (* -> * -> * -> * -> * -> *) (,,,,) 
Typeable (* -> * -> * -> * -> *) (,,,) 
Typeable (* -> * -> * -> *) (,,) 
Typeable (* -> * -> * -> *) STArray 
Typeable (* -> * -> *) (->) 
Typeable (* -> * -> *) Either 
Typeable (* -> * -> *) (,) 
Typeable (* -> * -> *) ST 
Typeable (* -> * -> *) Array 
Typeable (* -> * -> *) STRef 
Typeable (* -> * -> *) MVector 
Typeable (* -> * -> *) MVector 
Typeable (* -> * -> *) MVector 
Typeable (* -> * -> *) MVector 
Typeable (* -> *) [] 
Typeable (* -> *) Ratio 
Typeable (* -> *) IO 
Typeable (* -> *) Ptr 
Typeable (* -> *) FunPtr 
Typeable (* -> *) LocalState 
Typeable (* -> *) Complex 
Typeable (* -> *) STM 
Typeable (* -> *) TVar 
Typeable (* -> *) Maybe 
Typeable (* -> *) Errors 
Typeable (* -> *) ModuleName 
Typeable (* -> *) SpecialCon 
Typeable (* -> *) QName 
Typeable (* -> *) Name 
Typeable (* -> *) IPName 
Typeable (* -> *) QOp 
Typeable (* -> *) Op 
Typeable (* -> *) CName 
Typeable (* -> *) Module 
Typeable (* -> *) ModuleHead 
Typeable (* -> *) ExportSpecList 
Typeable (* -> *) ExportSpec 
Typeable (* -> *) Namespace 
Typeable (* -> *) ImportDecl 
Typeable (* -> *) ImportSpecList 
Typeable (* -> *) ImportSpec 
Typeable (* -> *) Assoc 
Typeable (* -> *) Decl 
Typeable (* -> *) TypeEqn 
Typeable (* -> *) Annotation 
Typeable (* -> *) BooleanFormula 
Typeable (* -> *) DataOrNew 
Typeable (* -> *) DeclHead 
Typeable (* -> *) InstRule 
Typeable (* -> *) InstHead 
Typeable (* -> *) Deriving 
Typeable (* -> *) Binds 
Typeable (* -> *) IPBind 
Typeable (* -> *) Match 
Typeable (* -> *) QualConDecl 
Typeable (* -> *) ConDecl 
Typeable (* -> *) FieldDecl 
Typeable (* -> *) GadtDecl 
Typeable (* -> *) ClassDecl 
Typeable (* -> *) InstDecl 
Typeable (* -> *) BangType 
Typeable (* -> *) Rhs 
Typeable (* -> *) GuardedRhs 
Typeable (* -> *) Type 
Typeable (* -> *) Promoted 
Typeable (* -> *) TyVarBind 
Typeable (* -> *) Kind 
Typeable (* -> *) FunDep 
Typeable (* -> *) Context 
Typeable (* -> *) Asst 
Typeable (* -> *) Literal 
Typeable (* -> *) Sign 
Typeable (* -> *) Exp 
Typeable (* -> *) XName 
Typeable (* -> *) XAttr 
Typeable (* -> *) Bracket 
Typeable (* -> *) Splice 
Typeable (* -> *) Safety 
Typeable (* -> *) CallConv 
Typeable (* -> *) ModulePragma 
Typeable (* -> *) Overlap 
Typeable (* -> *) Activation 
Typeable (* -> *) Rule 
Typeable (* -> *) RuleVar 
Typeable (* -> *) WarningText 
Typeable (* -> *) Pat 
Typeable (* -> *) PXAttr 
Typeable (* -> *) RPatOp 
Typeable (* -> *) RPat 
Typeable (* -> *) PatField 
Typeable (* -> *) Stmt 
Typeable (* -> *) QualStmt 
Typeable (* -> *) FieldUpdate 
Typeable (* -> *) Alt 
Typeable (* -> *) Vector 
Typeable (* -> *) Vector 
Typeable (* -> *) Vector 
Typeable (* -> *) Vector 
Typeable (* -> Constraint) Monoid 
Typeable (k -> *) (Proxy k) 
Typeable (k -> k -> *) (Coercion k) 
Typeable (k -> k -> *) ((:~:) k) 

module Data.Acid

module HSP

module Web.Routes

Internals

data AppError Source

an error type used with reform forms

Instances