UP | HOME

Snap framework notes

1 Snap framework

Snap is a Haskell web framework. It's relatively simple, mature enough, and, unlike Yesod, is easy to install on haskell-platform, which, in turn, is helpful when a program should be deployed on different systems: nothing is messed up with versions, at least if building in a sandbox; and haskell-platform is easy to install by itself, too.

I've used it for a few projects by now, though not using often, and hence forgetting some things. This is a bunch of notes/reminders.

2 Scripts

Additional script examples, to build and to run a snap-powered site.

2.1 A systemd service

Despite the systemd controversy, it's much easier to manage than various system-specific init scripts, so here's a sample service file for a snap application:

[Unit]
Description=My program
After=syslog.target

[Service]
Type=simple
ExecStart=/usr/local/bin/myprogram
User=mmlogview
WorkingDirectory=/usr/local/share/myprogram

[Install]
WantedBy=multi-user.target

2.2 Makefile

Builds an application in a sandbox, installs everything, creates a user, sets access rights, enables the service. Uses (and hence requires) sudo and systemd.

PROG=myprogram
FILES=src snaplets static Makefile $(PROG).cabal $(PROG).service

all: build

sdist:
        mkdir -p _dist
        mkdir -p _dist/$(PROG)
        cp -r $(FILES) _dist/$(PROG)
        tar CcfJ _dist $(PROG).tar.xz $(PROG)

sandbox:
        test -d .cabal-sandbox || cabal sandbox init

build-binary:
        cabal install --force-reinstalls

build: sandbox build-binary

install-files:
        sudo cp .cabal-sandbox/bin/$(PROG) /usr/local/bin/
        sudo cp $(PROG).service /usr/lib/systemd/system/
        sudo mkdir -p /usr/local/share/$(PROG)
        sudo cp -r static snaplets /usr/local/share/$(PROG)
        id -u $(PROG) || sudo useradd $(PROG)
        sudo chown -R "$(PROG):$(PROG)" /usr/local/share/$(PROG)
        sudo mkdir -p /var/log/$(PROG)
        sudo chown "$(PROG):$(PROG)" /var/log/$(PROG)
        sudo systemctl enable $(PROG)

uninstall:
        sudo systemctl disable $(PROG)
        sudo rm -rf /usr/local/bin/$(PROG) /usr/lib/systemd/system/$(PROG).service /usr/local/share/$(PROG)

install: build install-files

3 Programming

3.1 Heist and blaze-html

There is renderHtmlNodes function, which could be used for what its name says, allowing to embed blaze-html-generated HTML into Heist templates.

3.2 Heist and splices

Splices are used to bind values (as <bind> does), and it goes like this:

import qualified Heist.Interpreted as I

handleSomething :: Handler App (AuthManager App) ()
handleSomething = heistLocal (I.bindSplices splices) $ render "template"
  where
    splices = do
      "splice1" ## splice1
      "splice2" ## splice2
    splice1 = I.textSplice "first"
    splice2 :: I.Splice (Handler App App)
    splice2 = return $ renderHtmlNodes $ i $ toHtml $ show 42