epsilonhalbe
epsilonhalbe

Reputation: 15967

Automatically restart Snap server after changing a file

I have started learning a bit about the snap framework, I found some tutorial on blaze snap and want to build a little web-app.

The tedious thing when changing code in the html section is that I have to Ctrl+C the existing Snap server, then cabal run to restart it again is there an easier way to do that.

I found the following util watchr which allows for running a command after a certain file is being changed - which is definitely useful but I don't quite know how to apply it in this situation.

Get a minimal working example - use the snap init barebone command and substitute the src/Main.hs with

{-# LANGUAGE OverloadedStrings #-}
module Main where

import           Control.Monad (forM_)
import           Control.Applicative ((<|>))
import           Snap.Core
import           Snap.Util.FileServe
import           Snap.Http.Server
import           Snap.Blaze (blaze)
import qualified Data.Text as T
import qualified Text.Blaze.Html5 as H

main :: IO ()
main = quickHttpServe site

site :: Snap ()
site =
    ifTop testHandler <|>
    dir "static" (serveDirectory ".")

testHandler :: Snap ()
testHandler = blaze $ H.docTypeHtml $
    do H.head $ H.title "SnaptestBlaze"
       H.body $ do H.p "Blaze makes Html"
                   H.ul $ forM_ [1..10::Int] (H.li . H.toHtml)

and make sure to have snap-blaze and blaze-html in the corresponding *.cabal file.

I also saw that there is a package snap-loader-dynamic which sounds promising, but I could not build the application for the dependencies required and the dependencies in the cabal sandbox had different hash values.

I saw that there exists stack to eventually replace cabal in the long run, but I have not had enough time to check wether stack could do automatic rebuild & restart.

If the environment where I am developing is relevant: Linux (Mint) + cabal-sandbox.

Upvotes: 0

Views: 231

Answers (2)

mightybyte
mightybyte

Reputation: 7272

Daniel's answer is definitely the right one. However, if you want reloading that doesn't involve code changes, that is also possible too. See this blog post for an example.

Upvotes: 1

Daniel Wagner
Daniel Wagner

Reputation: 152707

Snap itself comes with this capability built-in. Assuming you initialized your project with snap init, just build your project by

cabal install -fdevelopment

and it will reload itself on the fly as necessary.

Upvotes: 4

Related Questions