(This post refers to Snap 0.2.6.)
There’s been a lot of buzz about the Snap framework, so I thought I’d give it a look. My personal website doesn’t have anything dynamic going on, so arguably Snap is “overkill,” but then again so is Apache, so what the heck. Snap is entirely experimental at this time: in their own words, “it is early-stage software,” so every single critique given here should be read with an implied expiration date.
So the question is: how does one host a static site on Snap? At present time there’s no tutorial for this, so I fumbled around until I got something working. Here’s my code:
main = do putStrLn "ninj4net online" quickServer config site site :: Snap () site = route [ ("kinetic", static "kinetic") , ("math1010", static "math1010") , ("math1030", static "math1030") , ("math1100", static "math1100") , ("math1210", static "math1210") , ("", static "") ] (writeBS "general error") static d = do let html_file = "static/" ++ d ++ "/index.html" xml_file = "static/" ++ d ++ "/index.xml" html <- liftIO $ doesFileExist html_file xml <- liftIO $ doesFileExist xml_file ( (ifTop (fileServeSingle $ if html then html_file else xml_file)) (fileServe $ "static/" ++ d) )
Some of my directories use an
index.html file, while others use an
index.xml file. I need to use
System.Directory.doesFileExist function to determine whether or not these files exist — trying
ifTop (fileServeSingle "something that doesn't exist") will not switch to the alternative using
<|>, so an explicit check is needed (it will whine about an exception being thrown, completely undermining the choice operator!). This is presumably a bug (I submitted it to their github).
I’m sure there is a much more elegant approach, but this was the best I could muster during lunch.
A thing Snap is missing
I came across a design decision that makes me nervous. As with any web framework, there are facilities for getting strings from the user. Unfortunately, Snap does not use types to distinguish between user-provided strings (dirty) and programmer-provided strings (clean).
Why does this matter? Segregating user input into its own type is a formidable defense against (say) SQL injection, since it obviates that
"select * from myData where foo='" ++ userInput ++ "'" isn’t well-typed (presumably SQL code should be its own type, say,
SQLString, and the function
UserString -> SQLString should be some kind of escape routine). It would be nice to see framework support for this types-based defense.
The most obvious example of this is in getParam, which simply returns a
Another example is provided by
fileServeSingle. The former returns a
FilePath provided by the user (a “safe” path, which — looking at the source — means that the “/../”‘s get removed), and the latter takes a
FilePath and opens the corresponding local file. I suppose the idea is that the code
do p <- getSafePath fileServeSingle p
shouldn’t escape the implied sandbox of the file system. Of course, if the application has tighter requirements than this, the type system isn’t there to help out. (For instance, perhaps a path is considered “safe” if it excludes certain keywords in addition to the constraints imposed by
A natural work-around is to build an application-specific wrapper around Snap, and perhaps this is the better approach; I’m not yet sure.
I’m glad that Snap has been announced, as it has proven interesting to look at. Of course, Haskell already has (at least) two other web frameworks (yesod and happstack) and it’s not clear which will win-out in mindshare, nor is it obvious (to me, anyway) which one would be the best choice for someone wanting to sit down and make a site. Of course, it’s possible that some mix-and-match might be the best approach: the web server of one project, the HTML generation of another, and the persistent storage of the third. (This possibility deserves some consideration, especially as projects like BlazeHTML really take off.) Hopefully in the coming months we’ll see more high-powered applications of these frameworks, giving us a fountain of lessons we can capitalize on, and providing some compelling show cases of Haskell’s power as a web development language.