MyNixOS website logo
Description

A simple example using Firefly.

Firefly

Firefly is dead simple http framework written in Haskell.

It strives for simplicity in implementation (and in use). It's great for people learning Haskell, fiddling with Monads, or who just need a really simple server for something.

Here's the minimal app:

{-# language OverloadedStrings #-}
import Web.Firefly
import qualified Data.Text as T

main :: IO ()
main = run 3000 app

app :: App ()
app = do
  route "/hello" (return "hello" :: Handler T.Text)

Just that easy!

Check out the Example App for more!

Specify your routes using regex patterns, the first one which matches will run.

Handler is a monad with access to the incoming request. You can access parts of it using helpers, then return a response.

Here are some valid response types and their inferred Content-Type

  • Data.Text.Text: text/plain
  • Data.Aeson.Value: application/json
  • Blaze.Html.Html: text/html

There are more in Web.Firefly.Response.

You can specify your status code using (body, Status) where body is any of the above types and Status is an Integer status code.

Or, add headers too with (body, Status, HeaderMap) where HeaderMap is a map of names to values.

Examples

Let's write some more interesting handlers:

hello :: App T.Text
hello = do
  -- | Get the 'name' query param from the url, if it doesn't exist use 'Stranger'
  name <- getQuery "name"
  -- If we just return some Text the response will be status 200 with a Content-Type of "text/plain"
  return $ "Hello " <> fromMaybe "Stranger" name

Here's an example of responding with JSON:

import Data.Aeson (ToJSON, FromJSON)
import GHC.Generics (Generic)
import qualified Data.Text as T
import qualified Network.Wai as W
import Web.Firefly

data User = User
  { username::T.Text
  , age::Int
  } deriving (Generic, ToJSON, FromJSON) -- Derive JSON instances

-- A reguler 'ol user
steve :: User
steve = User{username="Steve", age=26}

-- | Get a user by username
getUser :: App W.Response
getUser = do
  uname <- getQuery "username"
  return $ case uname of
             -- The Json constructor signals to serialize the value and respond as "application/json"
             Just "steve" -> toResponse $ Json steve 
             Just name -> toResponse ("Couldn't find user: " `mappend` name, notFound404)
             Nothing -> toResponse ("Please provide a 'username' parameter" :: T.Text, badRequest400)

Should I/Shouldn't I use Firefly?

You should use Firefly if:

  • You're intimidated by monads and want to learn more!
  • You want to write a hobby project
  • You like understanding the stack you're working with (The whole lib is ~300 lines without docs/imports)

Don't use Firefly if:

  • You'll have thousands of users
  • You want the most performant server possible
  • You want to have lots of helper libs available.
Metadata

Version

0.1.0.0

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows