Skip to content

Commit

Permalink
building with new yesod:
Browse files Browse the repository at this point in the history
- also launches, but is unable to resolve static files (e.g., js and css)
  • Loading branch information
Thorsten Lorenz committed Apr 29, 2012
1 parent 0360515 commit 3f01b07
Show file tree
Hide file tree
Showing 30 changed files with 4,249 additions and 588 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@ bin
report.html
*.sqlite3
dist
webtoink_old
1 change: 0 additions & 1 deletion webtoink/.gitignore

This file was deleted.

40 changes: 18 additions & 22 deletions webtoink/Application.hs
@@ -1,7 +1,8 @@
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Application
( getApplication
( makeApplication
, getApplicationDev
, makeFoundation
) where

import Import
Expand All @@ -10,13 +11,8 @@ import Yesod.Auth
import Yesod.Default.Config
import Yesod.Default.Main
import Yesod.Default.Handlers
#if DEVELOPMENT
import Yesod.Logger (Logger, logBS)
import Network.Wai.Middleware.RequestLogger (logCallbackDev)
#else
import Yesod.Logger (Logger, logBS, toProduction)
import Network.Wai.Middleware.RequestLogger (logCallback)
#endif
import Network.Wai.Middleware.RequestLogger (logCallback, logCallbackDev)
import qualified Database.Persist.Store
import Database.Persist.GenericSql (runMigration)
import Network.HTTP.Conduit (newManager, def)
Expand All @@ -31,37 +27,37 @@ import Handler.Convert
-- This line actually creates our YesodSite instance. It is the second half
-- of the call to mkYesodData which occurs in Foundation.hs. Please see
-- the comments there for more details.
mkYesodDispatch "WebToInk" resourcesWebToInk
mkYesodDispatch "App" resourcesApp

-- This function allocates resources (such as a database connection pool),
-- performs initialization and creates a WAI application. This is also the
-- place to put your migrate statements to have automatic database
-- migrations handled by Yesod.
getApplication :: AppConfig DefaultEnv Extra -> Logger -> IO Application
getApplication conf logger = do
makeApplication :: AppConfig DefaultEnv Extra -> Logger -> IO Application
makeApplication conf logger = do
foundation <- makeFoundation conf setLogger
app <- toWaiAppPlain foundation
return $ logWare app
where
setLogger = if development then logger else toProduction logger
logWare = if development then logCallbackDev (logBS setLogger)
else logCallback (logBS setLogger)

makeFoundation :: AppConfig DefaultEnv Extra -> Logger -> IO App
makeFoundation conf setLogger = do
manager <- newManager def
s <- staticSite
dbconf <- withYamlEnvironment "config/sqlite.yml" (appEnv conf)
Database.Persist.Store.loadConfig >>=
Database.Persist.Store.applyEnv
p <- Database.Persist.Store.createPoolConfig (dbconf :: Settings.PersistConfig)
Database.Persist.Store.runPool dbconf (runMigration migrateAll) p
let foundation = WebToInk conf setLogger s p manager dbconf
app <- toWaiAppPlain foundation
return $ logWare app
where
#ifdef DEVELOPMENT
logWare = logCallbackDev (logBS setLogger)
setLogger = logger
#else
setLogger = toProduction logger -- by default the logger is set for development
logWare = logCallback (logBS setLogger)
#endif
return $ App conf setLogger s p manager dbconf

-- for yesod devel
getApplicationDev :: IO (Int, Application)
getApplicationDev =
defaultDevelApp loader getApplication
defaultDevelApp loader makeApplication
where
loader = loadConfig (configSettings Development)
{ csParseExtra = parseExtra
Expand Down
69 changes: 32 additions & 37 deletions webtoink/Foundation.hs
@@ -1,8 +1,8 @@
module Foundation
( WebToInk (..)
( App (..)
, Route (..)
, WebToInkMessage (..)
, resourcesWebToInk
, AppMessage (..)
, resourcesApp
, Handler
, Widget
, Form
Expand All @@ -22,29 +22,21 @@ import Yesod.Default.Config
import Yesod.Default.Util (addStaticContentExternal)
import Yesod.Logger (Logger, logMsg, formatLogText)
import Network.HTTP.Conduit (Manager)
#ifdef DEVELOPMENT
import Yesod.Logger (logLazyText)
#endif
import qualified Settings
import qualified Data.ByteString.Lazy as L
import qualified Database.Persist.Store
import Settings.StaticFiles
import Database.Persist.GenericSql
import Settings (widgetFile, Extra (..))
import Model
import Text.Jasmine (minifym)
import Web.ClientSession (getKey)
import Text.Hamlet (hamletFile)
#if DEVELOPMENT
import qualified Data.Text.Lazy.Encoding
#else
import Network.Mail.Mime (sendmail)
#endif

-- | The site argument for your application. This can be a good place to
-- keep settings and values requiring initialization before your application
-- starts running, such as database connections. Every handler will have
-- access to the data present here.
data WebToInk = WebToInk
data App = App
{ settings :: AppConfig DefaultEnv Extra
, getLogger :: Logger
, getStatic :: Static -- ^ Settings for static file serving.
Expand All @@ -54,38 +46,41 @@ data WebToInk = WebToInk
}

-- Set up i18n messages. See the message folder.
mkMessage "WebToInk" "messages" "en"
mkMessage "App" "messages" "en"

-- This is where we define all of the routes in our application. For a full
-- explanation of the syntax, please see:
-- http://www.yesodweb.com/book/handler
--
-- This function does three things:
--
-- * Creates the route datatype WebToInkRoute. Every valid URL in your
-- * Creates the route datatype AppRoute. Every valid URL in your
-- application can be represented as a value of this type.
-- * Creates the associated type:
-- type instance Route WebToInk = WebToInkRoute
-- * Creates the value resourcesWebToInk which contains information on the
-- type instance Route App = AppRoute
-- * Creates the value resourcesApp which contains information on the
-- resources declared below. This is used in Handler.hs by the call to
-- mkYesodDispatch
--
-- What this function does *not* do is create a YesodSite instance for
-- WebToInk. Creating that instance requires all of the handler functions
-- App. Creating that instance requires all of the handler functions
-- for our application to be in scope. However, the handler functions
-- usually require access to the WebToInkRoute datatype. Therefore, we
-- usually require access to the AppRoute datatype. Therefore, we
-- split these actions into two functions and place them in separate files.
mkYesodData "WebToInk" $(parseRoutesFile "config/routes")
mkYesodData "App" $(parseRoutesFile "config/routes")

type Form x = Html -> MForm WebToInk WebToInk (FormResult x, Widget)
type Form x = Html -> MForm App App (FormResult x, Widget)

-- Please see the documentation for the Yesod typeclass. There are a number
-- of settings which can be configured by overriding methods here.
instance Yesod WebToInk where
instance Yesod App where
approot = ApprootMaster $ appRoot . settings

-- Place the session key file in the config folder
-- encryptKey _ = fmap Just $ getKey "config/client_session_key.aes"
-- Store session data on the client in encrypted cookies,
-- default session idle timeout is 120 minutes
makeSessionBackend _ = do
key <- getKey "config/client_session_key.aes"
return . Just $ clientSessionBackend key 120

defaultLayout widget = do
master <- getYesod
Expand All @@ -99,6 +94,7 @@ instance Yesod WebToInk where

pc <- widgetToPageContent $ do
$(widgetFile "normalize")
addStylesheet $ StaticR css_bootstrap_css
$(widgetFile "default-layout")
hamletToRepHtml $(hamletFile "templates/default-layout-wrapper.hamlet")

Expand Down Expand Up @@ -128,17 +124,17 @@ minifyOrNotm :: b -> Either a b
minifyOrNotm = Right

-- How to run database actions.
instance YesodPersist WebToInk where
type YesodPersistBackend WebToInk = SqlPersist
instance YesodPersist App where
type YesodPersistBackend App = SqlPersist
runDB f = do
master <- getYesod
Database.Persist.Store.runPool
(persistConfig master)
f
(connPool master)

instance YesodAuth WebToInk where
type AuthId WebToInk = UserId
instance YesodAuth App where
type AuthId App = UserId

-- Where to send a user after successful login
loginDest _ = RootR
Expand All @@ -157,15 +153,14 @@ instance YesodAuth WebToInk where

authHttpManager = httpManager

-- Sends off your mail. Requires sendmail in production!
deliver :: WebToInk -> L.ByteString -> IO ()
#ifdef DEVELOPMENT
deliver y = logLazyText (getLogger y) . Data.Text.Lazy.Encoding.decodeUtf8
#else
deliver _ = sendmail
#endif

-- This instance is required to use forms. You can modify renderMessage to
-- achieve customized and internationalized form validation messages.
instance RenderMessage WebToInk FormMessage where
instance RenderMessage App FormMessage where
renderMessage _ _ = defaultFormMessage

-- Note: previous versions of the scaffolding included a deliver function to
-- send emails. Unfortunately, there are too many different options for us to
-- give a reasonable default. Instead, the information is available on the
-- wiki:
--
-- https://github.com/yesodweb/yesod/wiki/Sending-email
2 changes: 1 addition & 1 deletion webtoink/Handler/Root.hs
Expand Up @@ -11,7 +11,7 @@ import Import
-- inclined, or create a single monolithic file.
getRootR :: Handler RepHtml
getRootR = defaultLayout $ do
h2id <- lift newIdent
aDomId <- lift newIdent
setTitle "WebToInk homepage"
$(widgetFile "homepage")
addScriptRemote "https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"
Expand Down
2 changes: 1 addition & 1 deletion webtoink/Handler/Utils.hs
Expand Up @@ -8,5 +8,5 @@ toTextPairs :: [(String, String)] -> [(Text, Text)]
toTextPairs = map toTextPair
where toTextPair (a, b) = (pack a, pack b)

getStringFromField :: Text -> GHandler sub WebToInk String
getStringFromField :: Text -> GHandler sub App String
getStringFromField = fmap unpack . runInputGet . ireq textField
6 changes: 4 additions & 2 deletions webtoink/Import.hs
Expand Up @@ -3,10 +3,11 @@ module Import
, module Yesod
, module Foundation
, module Settings.StaticFiles
, module Settings.Development
, module Data.Monoid
, module Control.Applicative
, Text
#if __GLASGOW_HASKELL__ < 740
#if __GLASGOW_HASKELL__ < 704
, (<>)
#endif
) where
Expand All @@ -18,8 +19,9 @@ import Data.Monoid (Monoid (mappend, mempty, mconcat))
import Control.Applicative ((<$>), (<*>), pure)
import Data.Text (Text)
import Settings.StaticFiles
import Settings.Development

#if __GLASGOW_HASKELL__ < 740
#if __GLASGOW_HASKELL__ < 704
infixr 5 <>
(<>) :: Monoid m => m -> m -> m
(<>) = mappend
Expand Down
8 changes: 3 additions & 5 deletions webtoink/Settings.hs
Expand Up @@ -23,6 +23,7 @@ import Data.Text (Text)
import Data.Yaml
import Control.Applicative
import System.FilePath (combine)
import Settings.Development

-- | Which Persistent backend this site is using.
type PersistConfig = SqliteConf
Expand Down Expand Up @@ -58,11 +59,8 @@ staticRoot conf = [st|#{appRoot conf}/static|]
-- user.

widgetFile :: String -> Q Exp
#if DEVELOPMENT
widgetFile = Yesod.Default.Util.widgetFileReload
#else
widgetFile = Yesod.Default.Util.widgetFileNoReload
#endif
widgetFile = if development then Yesod.Default.Util.widgetFileReload
else Yesod.Default.Util.widgetFileNoReload

data Extra = Extra
{ extraCopyright :: Text
Expand Down
14 changes: 14 additions & 0 deletions webtoink/Settings/Development.hs
@@ -0,0 +1,14 @@
module Settings.Development where

import Prelude

development :: Bool
development =
#if DEVELOPMENT
True
#else
False
#endif

production :: Bool
production = not development
9 changes: 3 additions & 6 deletions webtoink/Settings/StaticFiles.hs
Expand Up @@ -4,15 +4,12 @@ import Prelude (IO)
import Yesod.Static
import qualified Yesod.Static as Static
import Settings (staticDir)
import Settings.Development

-- | use this to create your static file serving site
staticSite :: IO Static.Static
staticSite =
#ifdef DEVELOPMENT
Static.staticDevel staticDir
#else
Static.static staticDir
#endif
staticSite = if development then Static.staticDevel staticDir
else Static.static staticDir

-- | This generates easy references to files in the static directory at compile time,
-- giving you compile-time verification that referenced files exist.
Expand Down

0 comments on commit 3f01b07

Please sign in to comment.