Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added a quick start and restructured the code.

  • Loading branch information...
commit 41e8bef807f979b3e7fbf56ea845d7e8a6945df0 1 parent a5b008e
@mightybyte mightybyte authored
View
1  snap-website.cabal
@@ -19,6 +19,7 @@ Executable snap-website
base >= 4,
haskell98,
transformers,
+ bytestring,
snap-core,
snap-server,
heist,
View
77 src/Main.hs
@@ -1,53 +1,39 @@
module Main where
-import Directory
-import System
+import Directory
+import System
+import Data.ByteString.Char8 (ByteString)
+import qualified Data.ByteString.Char8 as B
+import Control.Applicative
+import Control.Concurrent.MVar
+import Control.Monad.Trans
+import Snap.Http.Server
+import Snap.Types
+import Snap.Util.FileServe
+import System.FilePath
+import Text.Templating.Heist
-import Control.Applicative
-import Control.Monad.Trans
-import Snap.Http.Server
-import Snap.Types
-import Snap.Util.FileServe
-import System.FilePath
-import Text.Templating.Heist
+renderTmpl tsMVar n = do
+ ts <- liftIO $ readMVar tsMVar
+ maybe pass writeBS =<< liftIO (renderTemplate ts n)
---templateServe :: TemplateState IO
--- -> Snap ()
---templateServe ts = do
--- req <- getRequest
--- let pInfo = S.unpack $ rqPathInfo req
--- fp <- resolvePath pInfo
--- let fn = takeFileName fp
--- where
--- resolvePath p = do
--- -- relative paths only!
--- when (not $ isRelative p) pass
---
--- -- check that we don't have any sneaky .. paths
--- let dirs = splitDirectories p
--- when (elem ".." dirs) pass
---
--- let f = root </> p
---
--- -- check that the file exists
--- liftIO (doesFileExist f) >>= flip unless pass
---
--- return f
+templateServe :: MVar (TemplateState IO)
+ -> Snap ()
+templateServe ts = renderTmpl ts . B.pack =<< getSafePath
-site :: TemplateState IO -> Snap ()
-site ts =
- ifTop (maybe pass writeBS =<< liftIO (renderTemplate ts "index")) <|>
- template "about" <|>
- template "download" <|>
- template "docs" <|>
- template "contribute" <|>
- template "news" <|>
- template "heist-tutorial" <|>
- template "style-guide" <|>
- template "tutorials/heist" <|>
+reloadTemplates :: MVar (TemplateState IO)
+ -> Snap ()
+reloadTemplates tsMVar = do
+ liftIO $ modifyMVar_ tsMVar (const $ loadTemplates "templates")
+
+site :: MVar (TemplateState IO) -> Snap ()
+site tsMVar =
+ ifTop (renderTmpl tsMVar "index") <|>
+ path "admin/reload" (reloadTemplates tsMVar) <|>
+ templateServe tsMVar <|>
fileServe "static"
where
- template n = path n (maybe pass writeBS =<< liftIO (renderTemplate' "templates" n))
+ tmplPair n = (n, renderTmpl tsMVar n)
main :: IO ()
main = do
@@ -56,9 +42,10 @@ main = do
[] -> error "You must specify a port!" >> exitFailure
port:_ -> return $ read port
- templateState <- loadTemplates "templates"
+ ts <- loadTemplates "templates"
+ tsMVar <- newMVar ts
httpServe "*" port "achilles"
(Just "access.log")
(Just "error.log")
- (site templateState)
+ (site tsMVar)
View
2  templates/contribute.tpl
@@ -6,4 +6,6 @@ coding, there are a few things you should read.</p>
<li><a href="/about">philosophy and roadmap</a></li>
<li><a href="/style-guide">style guide</a></li>
</ol>
+ <p>You can discuss snap on #snapframework on freenode IRC.</p>
+ <p>Also be sure to join the <a href="http://mailman-mail5.webfaction.com/listinfo/snap">mailing list.</a></p>
</snap:apply>
View
2  templates/docs.tpl
@@ -1,7 +1,7 @@
<snap:apply template="page">
<h2>Tutorials</h2>
<ul>
- <li>Quick Start</li>
+ <li><a href="quickstart">Quick Start</a></li>
<li><a href="tutorials/heist">Heist Template Tutorial</a></li>
</ul>
<h2>API Documentation</h2>
View
31 templates/quickstart.tpl
@@ -0,0 +1,31 @@
+<snap:apply template="page">
+<h2>Snap Quick Start</h2>
+<p>First, go to the <a href="/download">download</a> page to find
+out how to install Snap. Next, try running the following sample
+app.</p>
+
+<pre>
+{-# LANGUAGE OverloadedStrings #-}
+module Main where
+import Snap.Http.Server
+import Snap.Types
+site = writeBS "hello world"
+main = do
+ httpServe "*" 8000 "servername"
+ (Just "access.log")
+ (Just "error.log")
+ site
+</pre>
+
+<p>Compile and run this program, then point your web browser to
+localhost:8000.</p>
+
+<p>Now, add another import for Snap.Util.FileServe and change the
+definition of <i>site</i> to</p>
+
+<pre>site = fileServe "."</pre>
+
+<p>This will serve all all files in the current directory.</p>
+
+</snap:apply>
+
Please sign in to comment.
Something went wrong with that request. Please try again.