Skip to content

Instantly share code, notes, and snippets.

@noteed
Last active November 22, 2015 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noteed/391b3be8f9e86eadc9a4 to your computer and use it in GitHub Desktop.
Save noteed/391b3be8f9e86eadc9a4 to your computer and use it in GitHub Desktop.
Snap Notes
{-# LANGUAGE OverloadedStrings #-}
------------------------------------------------------------------------------
-- | This example shows 'rawHttpServe', which use "raw" handlers, instead of
-- 'Snap' handlers.
--
-- (It is built using the "commandline" branch from the `snap-server`
-- repository.)
--
-- 'ServerConfig' is a record containing logging functions, users hooks and
-- some low-level HTTP server options.
--
-- 'CmdlineConfig' is a record containing things such as hostname, listen
-- address and port, whether to use SSL or compression.
--
-- Some values in 'ServerConfig' are derived from 'CmdlineConfig' using the
-- 'toServerConfig' function. That function also starts to listen for
-- incoming connections and returns an `accept` function.
--
-- 'rawHttpServe' uses a given handler, the derived 'ServerConfig' together
-- with the `accept` function.
module Main (main) where
------------------------------------------------------------------------------
import Snap.Core (Snap, emptyResponse)
import qualified Snap.Internal.Http.Server.Cleanup as Cleanup
import Snap.Internal.Http.Server.Types (ServerHandler)
import Snap.Http.Server (emptyServerConfig, rawHttpServe)
import Snap.Http.Server.CmdlineConfig (CmdlineConfig, defaultCmdlineConfig, setBind, setPort, toServerConfig)
------------------------------------------------------------------------------
main :: IO ()
main = Cleanup.runCleanup $ do
[(_, config, accept)] <- toServerConfig emptyServerConfig cmdlineConfig
rawHttpServe handler [(config, accept)]
where
cmdlineConfig :: CmdlineConfig Snap a
cmdlineConfig = (setBind "0.0.0.0" . setPort 8000) defaultCmdlineConfig
handler :: ServerHandler ()
handler config session request = return (request, emptyResponse)

The Snap Framework is divided in a few libraries:

  • snap-core provides the core types and functions to write handlers, i.e. read requests and produce responses.
  • snap-server is a library providing a HTTP server, using handlers written with snap-core to serve clients.
  • snap provides a convenience module re-exporting snap-core and snap-server, but also the Snaplet API to build Snap applications in a modular way. Notet that snap-server alone (without snap), is enough to write an application.
  • io-streams is a streaming IO library. It is not strictly part of Snap Framework but is central to it. For instance you can consume a request's body as an InputStream ByteString.
  • io-streams-haproxy implements the PROXY protocol useful to place a HTTP server being proxies (e.g. HAProxy or Stunnel). Just like openssl-streams, you don't use it directly.
  • openssl-streams allows snap-server to use HTTPS instead of HTTP.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment