38

I want to create a website using the Warp webserver in Haskell.

As I'm a Haskell beginner, examples like this one are too complex for me.

Can anyone show me a simple, minimal example of how to use Warp?

Note: This question intentionally shows no research effort as it was answered Q&A-style.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Uli Köhler
  • 13,012
  • 16
  • 70
  • 120

1 Answers1

61

Here's a minimal Hello World application using Warp 3.0+. Run it, then navigate to http://localhost:3000. This example will show Hello world.

In order to keep this example minimal, URL paths are not handled at all (the same content is delivered for any path). For a slightly longer example incorporating URL path handling, see the Haskell Wiki

{-# LANGUAGE OverloadedStrings #-}

import Network.Wai (responseLBS, Application)
import Network.Wai.Handler.Warp (run)
import Network.HTTP.Types (status200)
import Network.HTTP.Types.Header (hContentType)

main = do
  let port = 3000
  putStrLn $ "Listening on port " <> show port
  run port app

app :: Application
app _req f =
  f $ responseLBS status200 [(hContentType, "text/plain")] "Hello world!"
Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
Uli Köhler
  • 13,012
  • 16
  • 70
  • 120
  • 8
    Please explain the downvote, just downvoting this won't make the post any better! – Uli Köhler Mar 25 '14 at 15:03
  • 5
    Note that the first line including `OverloadedString` is significant, because it allows the conversion of normal strings to `ByteString`s. Otherwise this example would need to be modified to explicitly convert strings like `"text/plain"` and `"Hello world!"` to `ByteString`s via `Blaze.ByteString.Builder.Char8` and/or `Data.ByteString.Char8`. – elliot42 Jun 28 '15 at 10:23