- Create a new project, static-contents-in-snap, with the simple stack template:
stack --resolver lts-9.1 new static-contents-in-snap simple
- Add a dependency on the following libraries in the build-depends subsection of the executable section:
executable static-contents-in-snap hs-source-dirs: src main-is: Main.hs default-language: Haskell2010 build-depends: base >= 4.7 && < 5 , snap-server , snap-core , snap , lens , bytestring , text
- Add a directory, static, in the project folder. Add index.html in this directory, as follows:
<!DOCTYPE HTML5> <html> <body> <p> This file is served as a static content. You may add
links to subfolder as well. But the folder ".." and absolute
path are not honoured while serving the directory. </p> <p> This is a link to <a href="subfolder">subfolder</a> </p> </body> </html>
- Also add a subfolder named subfolder, and add the following contents to the file subfolder/example.html:
<!DOCTYPE HTML5> <html> <body> <p> This content is served from the folder <em>subfolder</em> </p> </body> </html>
- Open src/Main.hs. We will add our source here. Add the Main module and the necessary imports:
{-# LANGUAGE OverloadedStrings #-} module Main where import Data.Monoid import Control.Applicative import Snap import Snap.Core import Snap.Http.Server import Snap.Util.FileServe
- Serve the static contents using the serveDirectory function. The directory listing will be stylized by fancyDirectoryConfig:
main :: IO () main = quickHttpServe $ serveDirectoryWith fancyDirectoryConfig "static"
- Build and execute the project:
stack build stack exec -- static-contents-in-snap
- Point the browser to http://localhost:8000/subfolder. You should see the following output:
